-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Wait for method to exist #1164
Comments
You can already do this. There are lots of ways Cypress makes this much easier. First off you can always use a https://docs.cypress.io/api/commands/should.html#Function However you don't need this because https://docs.cypress.io/api/commands/its.html#Rules Also per your code you are mixing up sync and async code. it('window.app exists', function() {
cy.visit('http://localhost:3000/')
// should('exist') here is not necessary because thats the default assertion
cy.window().its('app').should('exist').then((window) => {
// dont call this window, call it win
// using .its('app') changes the subject to be that property
// which is why what you're doing below doesn't work
console.log("cy window:");
console.log(cy.window().app) // cy.window() is async it does not return a useful value
cy.wrap(cy.window().app).should('exist') // nope
console.log(window.app); // because the subject is already 'app', you're drilling into the 'app' property which is undefined
});
}) Here is the correct code: it('window.app exists', function() {
cy.visit('http://localhost:3000/')
cy.window().its('app').then((app) => {
debugger // please use these, don't use console.log(...)
app // is defined
})
}) |
Everything in Cypress is made to be easy - you hardly have to write any code or do any kinds of backflips. If you are - then something is probably wrong. I would suggest further reading our guides here about async and sync code - it shows you common patterns and idioms for dealing with Cypress commands. https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html As a final note - remember you're using Cypress not Selenium - which means that you can just drop |
Thanks for the tip about
Are there plans for an async/await compatible API? |
Streams and promises are not analogous - there is no way to make them behave the same. Cypress commands are more akin to streams of data where data is piped and in and through and thus affected by others. Commands must be linked together. The guides we suggested in that link explain it pretty well. |
Thanks. I will read them in more detail. |
// Works
cy.get('input[data-automation-id="gil"]').as('gil');
cy.get('@gil').should('have.value', '123');
// Works
cy.get('@gil').then(array => {
cy.wrap(array[0]).should('have.property', 'value', '123');
})
// Both of these error
// Timed out retrying: expected '<input#TextField29.ms-TextField-field.field_95da125e>' to have a property 'value'
cy.get('@gil').should('have.property', 'value', '123');
cy.get('@gil').first().should('have.property', 'value', '123'); @brian-mann Is this ^ user error or a legit bug in the API? I have read the docs and am not sure why one works and the other doesn't. |
User error. Not a bug.
|
Is this a Feature or Bug?
Feature
Current behavior:
It doesn't seem possible to wait for a method to exist.
Desired behavior:
I'd like a generic wait method that will retry a function until it evaluates to true, similar to WebDriver running execute script and checking the result. The wait function in cypress doesn't appear to allow waiting for methods to be defined.
How to reproduce:
Test a react app that dynamically defines methods based on component life cycle events.
Test code:
Additional Info (images, stack traces, etc)
The text was updated successfully, but these errors were encountered: