-
Notifications
You must be signed in to change notification settings - Fork 25
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 until cy.get("selector") stops failing #75
Comments
Hi @dialex, you are welcome! 😊
cy.waitUntil(
() => {
cy.reload()
cy.get(activePackageSubtitle).should("exist")
}
) you should write // ATTENTION: this is just an example, it will not workl
cy.waitUntil(
() => {
cy.reload()
return cy.get(activePackageSubtitle).then($el => $el.length)
}
)
cy.get(activePackageSubtitle).should("exist") so this should not fail because you manually check for the element existence before asserting about that with Cypress
So,
cy.waitUntil(
() => {
cy.reload()
return Cypress.$(activePackageSubtitle).length;
}
)
cy.get(activePackageSubtitle).should("exist") Simply, we consume directly the Cypress' exposed jQuery. Or, more condensed cy.waitUntil(() => cy.reload().then(() => Cypress.$(activePackageSubtitle).length))
cy.get(activePackageSubtitle).should("exist") Let me know if it works as you expect 😉 |
Thank you so much for the fast reply and in-depth explanation on how to solve this problem 🙇 YES, it worked. I spend the whole yesterday trying a solution but I could workaround the " |
You're welcome 😊 |
Last but not least: if you have looked for a solution on Google/stackoverflow and you haven't found anything useful... Please help the community doing so
Doing so:
What do you think? 😊 |
Since this questions was specific to this plugin (and not Cypress) itself, I think I prefer to leave it here. Google will catch it anyway, so we don't lose anything. But it was a good idea nonetheless 😉 P.S: I just like having this personal notebook with my most used Cypress code snippets -- it's faster than searching online or going through bookmarks. It's also shared privately with teammates, my goal is to make it public after I finish my current project :P |
Hello @dialex, I am not sure if it's a lot to ask, but can you please share your "Cypress recipes" notebook. It will be of great help since it's created with all your experience of cypress and plugins. |
Of course @kalpeshchilka, glad to help 😉
|
after hours spent trying to use get (as I found it on stackoverflow) and it is hard to notice a small comment in readme since example of using cy.get is more visible: cy.waitUntil(() => cy.get("input[type=hidden]#recaptchatoken").then($el => $el.val())) |
@NoriSte Isn't using something like
the same as using |
Yes, it is! And in this case, using The above example is a bit different
because it include two operations, reloading the page and then waiting for the element. If you do not use
Instead, by using
Let me know if you need more help 😊 |
I am using the above wait function using to download the files but its not working form me every time it reloads the page. |
Can you share a reduced-to-the-bone project that I can play with? |
Cypress.Commands.add("waitUntilSpinnerFinished", () => { |
This is not a reduced-to-the-bone project that I can play with. What is the error you are receiving, for instance? To play with your code (play, not read), I must
I guess that the chance I have to match your use case is 0.01%... And I can spend hours there... Instead:
|
@NoriSte thank you so much for this thread. It works for me but I need to handle a slightly different scenario. In these examples, it seems that we reload regardless on if the first render has the element on the page. In my case, the element may update (driven by a socket push that makes an element render) or not (socket push timing not lined up and socket message came through while component still loading, so I would need to refresh to see it (it would come down on the reload's page fetch). I would like to avoid the initial reload if the element is on the page the first time, and reload if it's not. How can I achieve this? I've tried a bunch of stuff, including wrapping this like in the examples below Works but sometimes with an unnecessary reload:
Doesn't work:
but it doesn't seem to work, as it says it cannot find |
Uhm, something simpler like this could work? cy.waitUntil(
() => {
if (Cypress.$(`#${elemId}`).length) {
return true // the wait is over, a truthy stops cy.waitUntil retrying
}
return cy.reload().then(
() => false // After reloading, a falsy value continues cy.waitUntil trying
)
}, {
interval: 3000,
timeout: 5000,
errorMsg: `Unable to find element using selector "${elemId}"`,
}
) |
From the examples, I understood that
waitUntil
will wait until a condition becomes true:But what if my condition is Cypress assertion? Using the same example:
My context is, I have a page that will display data that is being updated by a cron job. The page does not update automatically, so the user must manually refresh the page. Sometimes the cron job takes a little longer, so I want to
waitUntil
a Cypress command or assertion stops failing. The problem is, as soon as the command fails, the test ends with a failure.How can I make this code work?
P.S: Thank you so much for writing this lib. It's so useful and so critical. I don't know why it doesn't come out-of-the-box with Cypress. Well done 😄
The text was updated successfully, but these errors were encountered: