How to capture all API calls in cypress?

点点圈 提交于 2020-01-25 09:15:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!