问题
I have API call when loading a chat app, 30 calls/group (it's to load last 30 messages on each group). Let's say in a case, I test a user which has 2 groups only. So I expect to see 60 API calls for this.
I tried with following code.
it('Call 30 group messages APIs for every favorite group', () => {
cy.server()
cy.route(awsUrl + '/**').as('apiMessageContent')
for (let i = 0; i < 60; i++) {
cy.wait('@apiMessageContent', { timeout: 30000 }).then(res => {
expect(res.status).not.to.be.null
})
}
})
But the result cypress randomly only can capture 28-30 API calls, and other route waits after that are failing. In fact, in cypress left panel I can see the 60 XHR are all listed. What is the solution for this?
回答1:
cy.wait can accept an array of Aliases, so you might be able to wait for @apiMessageContent in this way, rather than looping and waiting 60 times. Although it's unclear why your solution does not work.
cy.wait(Array(60).fill('@apiMessageContent'), { timeout: 30000 }).then((xhrs) => {
xhrs.forEach((res) => {
expect(res.status).not.to.be.null
})
})
来源:https://stackoverflow.com/questions/57211968/how-to-capture-all-api-calls-in-cypress