赛普拉斯捕获所有请求cy.Route() [英] Cypress capture all requests cy.Route()

查看:34
本文介绍了赛普拉斯捕获所有请求cy.Route()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获所有请求,但是 cy.Route()似乎不接受通配符.因此,例如,我想导航到Reddit"并捕获所有请求,但是,我还希望代码可重用,这样我就可以导航到堆栈溢出并捕获所有请求.

这可能吗?

我尝试过*通配符,但不起作用

  cy.route('*').as('GETS');cy.route(GET,'*').as('GETS'); 

解决方案

www.instagram.com 上进行测试:

I want to capture all requests, however cy.Route() does not seem to accept wildcards. So, for example, I want to navigate to Reddit" and capture all the requests, however, I also want the code to be reusable so I can navigate to stack overflow and also capture all requests.

Is this possible?

I have tried * wildcard but it does not work

cy.route('*').as('GETS');

cy.route(GET, '*').as('GETS');

解决方案

Cypress automatically includes minimatch and exposes it as Cypress.minimatch. According to minimatch documentation, you need to use "Globstar" ** matching

Correct ways for all get and post requests:

cy.route('GET', '**').as('gets'); cy.route('POST', '**').as('posts');

Or,

cy.route({
    method: 'GET',
    url: '**'
}).as('gets');


cy.route({
    method: 'POST',
    url: '**'
}).as('posts');

Note: cy.route() should be set before cy.visit(). To read the response use cy.wait('@gets').then and cy.wait('@posts').then

cy.wait('@posts').then((xhr) => {
    cy.log('Intercepted: ' + xhr.url);
    cy.log('Intercepted: ' + JSON.stringify(xhr.response.body));
});

Test on www.google.com:

Test on www.instagram.com:

这篇关于赛普拉斯捕获所有请求cy.Route()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆