Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In cypress, how do I wait for a page to load?

Tags:

Don't tell anyone, but our app is not yet single-page. I can wait on a given XHR request by giving the route an alias, but how do I wait until some navigation completes and the browser is safely on a new page?

like image 241
bbsimonbb Avatar asked Mar 29 '18 10:03

bbsimonbb


People also ask

How do I make Cypress wait for page load?

Wait for API response Cypress works great with http requests. If you are waiting for some resources to be loaded in your app, you can intercept a request and then create an alias for it. That alias will then be used with . wait() command.

What is the best way to wait and test the page in Cypress?

With cypress, cy. visit() can be used to wait for a page to load.

Does Cy Visit wait for page to load?

cy. visit() can time out waiting for the page to fire its load event.

What command can be used to wait in Cypress?

wait() , Cypress will wait for all requests to complete within the given requestTimeout and responseTimeout .


1 Answers

You can add some assert inside:

cy.click('#someButtonToNavigateOnNewPage');  cy.location('pathname', {timeout: 60000})   .should('include', '/newPage');  cy.click('#YouAreOnNewPage'); 

You can change default timeout - by default it's 4000 ms (4 secs) - to ensure that user navigated the page. I've put a big number here - 60,000 ms - because I'm sure that 99% of users will leave if they do not have page loaded after 1 min.

like image 189
Igor Shubovych Avatar answered Sep 25 '22 13:09

Igor Shubovych