Vadim Vishnevskiy
Vadim Vishnevskiy

Reputation: 41

How to wait all similar requests in cypress?

When start page of app are loading I have a lot of requests on locize api. requests count

When I try to write a test for it, I need to wait when all of them finished and then start to get an DOM elements. I do it like this:

    cy.intercept({
      method: "GET",
      url: "https://api.locize.app/**",
    }).as("languages");
    cy.visit("/");
    cy.wait("@languages")

but this code wait only 1st request. test log

I was try to do it with cy.get("@languages.all") but it's haven’t any effect.

So the question is, how can I get waiting for all requests before it's go further?

P.S. I'm pretty new in cypress, so I'll be really appreciated for any help.

Upvotes: 2

Views: 1187

Answers (2)

Vadim Vishnevskiy
Vadim Vishnevskiy

Reputation: 41

One of the solution that I found is library cypress-network-idle.

https://www.npmjs.com/package/cypress-network-idle

Basically it helped me solve the problem

Upvotes: 2

jjhelguero
jjhelguero

Reputation: 2565

If you know the calls for the language, then you can store them in an array and iterate over it to create unique intercepts and wait on them. However, this would be brittle to changes in your app calls.

const enGBItems = ['Objects', 'Locations', ... ]

Cypress._.forEach(enGBItems, function(item){
  cy.intercept({
    method: "GET",
    url: `https://api.locize.app/**/en-GB/${item}`,
  }).as(item)
})
cy.visit("/")
Cypress._.forEach(enGBItems, function(item){
  cy.wait(`@${item}`)
})


Upvotes: 1

Related Questions