Wait for certain number of http requests with Cypress

46 Views Asked by At

I'm writing a cypress test that tests a map application. When I click on a specific button, I want to wait for all resources to load on the map.

Currently, the button is making a request to a url: someurl.com/myData/** Where the ** represents dynamic data.

This is what I currently have:

describe('Map Data', () => {
  beforeEach(()=> {
  cy.intercept('GET', 'http://someurl.com/myData/**').as('loadData')  
  cy.visit('/')
})
it('checks for all resources to be loaded', () => {
  cy.get('#loadButton').click()
  cy.wait('@loadData')
})
})

This waits until the first resource was loaded. However, there are many resources from the url and I was wondering if it's possible to wait for at least a certain number of them to load?

1

There are 1 best solutions below

1
Ho'okano On

Intercepts are valid for any number of requests. They are listeners that keep listening, except when the test changes - then they are removed (but are re-setup when you have a beforeEach())

You can just wait for "a certain number"

cy.intercept(...).as('data')
cy.get('#loadButton').click()

let certainNumber = 10
for (let i = 0; i < certainNumber; i++) {
  cy.wait('@loadData')
}

Of course, you have to be certain about the number.