Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for multiple XHR requests to the same url or endpoint

Tags:

cypress

In Cypress, I have multiple requests that will match my cy.route() declaration. I want to make sure more than one request is made on the route. How do I tell Cypress to wait for multiple XHR requests to the same url?

like image 668
bkucera Avatar asked May 22 '18 16:05

bkucera


People also ask

How do you wait for an API call to finish in Cypress?

Cypress automatically waits for the network call to complete before proceeding to the next command. // Anti-pattern: placing Cypress commands inside . then callbacks cy. wait('@alias') .

What is the default wait time in Cypress?

Response timeout Once Cypress detects a match request has started, it switches to a second wait. By default, 30000 milliseconds duration set. This means Cypress will wait 30 seconds for the remote server to respond to this request.

What is intercept in Cypress?

cy.intercept() is the successor to cy.route() as of Cypress 6.0.0. See Comparison to cy.route . All intercepts are automatically cleared before every test.


2 Answers

From the Cypress Docs:

You should set up an alias (using .as()) to a single cy.route() that matches all of the XHRs. You can then cy.wait() on it multiple times. Cypress keeps track of how many matching XHR requests there are.

cy.server()
cy.route('users').as('getUsers')
cy.wait('@getUsers')  // Wait for first GET to /users/
cy.get('#list>li').should('have.length', 10)
cy.get('#load-more-btn').click()
cy.wait('@getUsers')  // Wait for second GET to /users/
cy.get('#list>li').should('have.length', 20)
like image 86
bkucera Avatar answered Jan 01 '23 01:01

bkucera


Another alternative is to pass in an array of aliases.

cy.wait(["@graphql", "@graphql"]);
like image 20
HRVHackers Avatar answered Jan 01 '23 02:01

HRVHackers