Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of environment #1101

Closed
kemalbayar opened this issue Dec 20, 2017 · 4 comments
Closed

Usage of environment #1101

kemalbayar opened this issue Dec 20, 2017 · 4 comments

Comments

@kemalbayar
Copy link

kemalbayar commented Dec 20, 2017

Test code:

I have some li or option values and i want to check if those exists so i use the following usage for this purpose.

cy.get(.myclassname li’).should(($lis) => {

        expect($lis).contain(‘Sugar’)
        expect($lis).contain(‘Juice’)
        expect($lis).contain(‘Bread’)
        expect($lis).contain(‘Ball’)

       })

as you see, i have to write every value i have one by one. Is there a way to define all the values i have in a variable and use that variable instead of writing all the values one by one like above

@bahmutov
Copy link
Contributor

You could find a chai assertion helper that does that (see how to bring additional chai assertions in this example https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions) or just create little helper function that you can reuse. I see something like this

const shouldContain = (...names) => ($lis) => {
  names.forEach(name =>
    expect($lis).contain(name)
  )
}
cy.get(.myclassname li’).then(shouldContain('Sugar', 'Juice', 'Bread', 'Ball'))

@jennifer-shehane
Copy link
Member

Seems as if you could use Cypress .each() to iterate through each li (or also just assert against the $lis in my example below if you did want to check the entire list against each value).

let names = ['Sugar', 'Juice', 'Bread', 'Ball']

cy.get(.myclassname li’).each(($li, i, $lis) => {
    expect($li).to.contain(names[i])
})

@kemalbayar
Copy link
Author

kemalbayar commented Dec 22, 2017

Thank you all of you for your answers.

@jennifer-shehane

im used to use the following code before your help ...('.myclassname li')... im used li not div but when i used your code with li it failed but with div it run successfuly

the code i used before your help .... ('.myclassname li').....

cy.get('div[id="12345"]').click()
          cy.get('.myclassname li').should(($lis) => {

            expect($lis).contain('Sugar')
            expect($lis).contain('Juice')
            expect($lis).contain('Bread')
            expect($lis).contain('Ball')
          })

I couldnt run your code with li but it runs with div ....('.myclassname div')....

cy.get('div[id="12345"]').click()
          var names = ["Sugar", "Juice", "Bread", "Ball"]
          cy.get('.myclassname div').each(($div, i, $lis) => {
              expect($li).to.contain(names[i])
          })

in the web page Im trying to test, all the li's which Im trying to check are in div tags seperatly
may it the reason? if so, i dont understand why the first code run successfuly and why your code doesnt run with li

@bahmutov
Copy link
Contributor

Can you make a reproducible small example, maybe even in the fork of https://github.com/cypress-io/cypress-example-kitchensink with your code that shows the problem? It is hard to see what is going on and what you expect to happen via code snippets in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants