Cypress: Surprise: cy.get(selector).contains(text) is not the same as cy.contains(selector, text)

Created on 19 Mar 2019  路  5Comments  路  Source: cypress-io/cypress

Current behavior:

I am trying to wait for a button with some text to appear in the app. But the app has many buttons available all the time. So if I do cy.get('button').contains('FooBar') this fails because cy.get('button') finds some buttons, but they do not contain text FooBar. Even if button is later on added, this still fails because cy.get seems to not be retried and the same initial set of buttons is provided to contains.

Desired behavior:

The whole chain should be retried until it succeeds.

Versions

Linux, Cypress 3.1.1.

unexpected behavior

Most helpful comment

You may try as alternatives:

cy.get('button:contains("FooBar")')
cy.contains('button', 'FooBar')
cy.get('button').should('contain', 'FooBar')

Please check out our retry-ability doc on specific about how this works today in Cypress.

All 5 comments

You may try as alternatives:

cy.get('button:contains("FooBar")')
cy.contains('button', 'FooBar')
cy.get('button').should('contain', 'FooBar')

Please check out our retry-ability doc on specific about how this works today in Cypress.

Yes, I understand other approaches to achieve this, but I think this behavior should at least be documented somewhere. I see that it is well documented here, but that is not linked from cy.get?

I created a new issue in our docs to document retry doc from other areas here: https://github.com/cypress-io/cypress-documentation/issues/1506. Our documentation is open source and contributions are welcome. 馃槃

@jennifer-shehane - My colleague and I found one of your suggestions helpful, but we can't find more documentation about the syntax. Can you say more about where this example is documented?

cy.get('button:contains("FooBar")')

The notation with the colon does not seem to utilize basic css selectors, but we found it helpful for finding a collection of buttons that all contained the same text. We also were not sure how this particular example related to retry-ability and if that was closely related to the syntax, we could not see how. The notation seems like it would be really useful. Thanks!

@kklamberty Cypress uses the sizzle engine under the hood, same as jQuery https://github.com/jquery/sizzle/wiki

Was this page helpful?
0 / 5 - 0 ratings