简体   繁体   中英

Capture all XHR Requests with Cypress.io

When I call the URL, an undefined number of requests are sent.

Now I try to find out if one of the requests contains a certain payload.

cy.server();
cy.visit(url);

cy.route({
    method: 'POST',
    url: '**/t/e/**',
}).as('xhrRequest');

I have found a similar approach on How to capture all API calls in cypress? so far. the problem here is that a fixed number of API calls is assumed.

cy.wait(Array(60).fill('@xhrRequest'), { timeout: 30000 }).then((xhrs) => {
    xhrs.forEach((res) => {
        expect(res.status).not.to.be.null
    })
})

How do I get it that all requests are intercepted and fail my test if there is not a single request containing the payload.

I already wrote something like this in puppeteer

let hasSpecialRequest = false; 
page.on('request', request => {
if (isSpecialRequest(request)) {
    hasSpecialRequest = true; 
}
request.continue();
});

await page.setRequestInterception(true);

expect(hasSpecialRequest).to.equal(true);

The system checks whether each request is one of the special requests and sets the variable accordingly. Something like this I try to recreate with Cypress.

您可能会考虑执行cy.exec并使用另一种语言运行脚本并从子进程返回状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM