Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How wait for page finish reloading in cypress after click?

Tags:

cypress

I got form and when I trigger click on submit button this cause to refresh current page. After that I would like to do some assertions. How I know that page finished refreshing and I can start to searching an element?

cy.get('#formButton').click() // adds new item and refresh page
// do assertions on page
like image 299
mardok Avatar asked Nov 26 '22 13:11

mardok


1 Answers

This solution was posted by @msty on a similar GitHub issues:

// Navigate / submit form
cy.click('#someButtonToNavigateOnNewPage');

cy.location('pathname', {timeout: 10000})
  .should('include', '/newPage');

// Do assertions here
cy.contains('h1', 'success')

Another solution from the Cypress Docs is:

// Wait for the route aliased as 'getAccount' to respond
// without changing or stubbing its response
cy.server()
cy.route('/accounts/*').as('getAccount')
cy.visit('/accounts/123')
cy.wait('@getAccount').then((xhr) => {
  // we can now access the low level xhr
  // that contains the request body,
  // response body, status, etc
})
like image 165
Allan of Sydney Avatar answered Nov 29 '22 02:11

Allan of Sydney