How to wait all similar requests in cypress?

Viewed 143

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.

2 Answers

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}`)
})


Related