how can i reliably wait for XHR requests after loading a page in a Cypress test?

You can do something like this

// Give an alias to request
cy.intercept({
  method: 'GET',
  url: '/odata/locations/**',
}).as('dataGetFirst');

// Visit site
cy.visit('admin/locations');

// Wait for response.status to be 200
cy.wait('@dataGetFirst').its('response.statusCode').should('equal', 200)

// Continue

So most of the answers are deprecated now. As of [email protected], you should use intercept().

Here's how I did mine:

cy.intercept({
      method: "GET",
      url: "http://my-api.biz/api/**",
    }).as("dataGetFirst");
cy.wait("@dataGetFirst");

And that's it. You could do more and do an assertion chain on the wait but it's already an assertion by itself.

Tags:

Cypress