Reputation: 47
How to wait until page reload after click a button? I used cy.wait(3000) but now I need some non-static command.
My code was:
cy.get(button).click()
cy.wait(3000) //because after first click page is reloading
cy.get(button2).click()
I need something like wait to wait some time until page is reload. But I need some non-static command.
Upvotes: 2
Views: 3133
Reputation: 1427
If you are sure the page will reload, you can add an assertion that the initial button has disappeared and after that click. If the second is apparent on the page before reload - it will mess the test if you just put an assertion for it:
cy.get(button).click().should('not.exist') //Assertion that the page has started reloading
cy.get(button2).click() //Here you do not need an assertion for visibility, it is included in cy.get
Upvotes: 1
Reputation: 7777
It is usual that pages load progressively, ie they might paint the menus first, and then load some kind of skeletons on the page, call the api's, and when the data arrives do a final render.
The time to do all this is quite variable, especially if you have not optimised your page loading. The best way to tackle it is for your cypress script to look for one thing at a time, so it keeps step with page loading, and each one is within the normal timeout. You can also do a cy.intercept
to wait for an API request to complete - this is usually one of the longest and most variable operations.
Upvotes: 1
Reputation: 18650
You can add a timeout for button2 to be visible and clickable.
cy.get(button).click()
cy.get(button2, {timeout: 3000}).should('be.visible').click()
Now the above code will wait a maximum of 3 seconds for the button2 to be visible. But in case it finds button2 sooner, then it will click it and not wait till the end of 3 seconds.
Upvotes: 1