javascript - Cypress 捕获所有请求 cy.Route()
问题描述
我想捕获所有请求,但cy.Route()
似乎不接受通配符。因此,例如,我想导航到 Reddit”并捕获所有请求,但是,我还希望代码可重用,以便我可以导航到堆栈溢出并捕获所有请求。
这可能吗?
我试过 * 通配符,但它不起作用
cy.route('*').as('GETS');
cy.route(GET, '*').as('GETS');
解决方案
Cypress自动包含 minimatch 并将其公开为 Cypress.minimatch。根据minimatch 文档,您需要使用"Globstar" ** matching
get
所有和post
请求的正确方法:
cy.route('GET', '**').as('gets');
cy.route('POST', '**').as('posts');
或者,
cy.route({
method: 'GET',
url: '**'
}).as('gets');
cy.route({
method: 'POST',
url: '**'
}).as('posts');
注意:cy.route() should be set before cy.visit()
。要阅读响应,请使用cy.wait('@gets').then
和cy.wait('@posts').then
cy.wait('@posts').then((xhr) => {
cy.log('Intercepted: ' + xhr.url);
cy.log('Intercepted: ' + JSON.stringify(xhr.response.body));
});
测试www.google.com
:
推荐阅读
- android - 无法在 recyclerview Android 中启动新活动 onItemClick
- vb.net - DynamicDatabaseFactory.CreateDatabase 连接字符串解析出错
- amazon-dynamodb - 生成不重复的数据并保存在 DynamoDB 中
- python - 在python中将布尔值归因于变量和将字符串归因于变量之间是否存在显着的速度差异?
- powershell - 使用 for 循环复制具有递增值的属性
- javascript - 尝试使用reduce在Javascript中添加路由父URL
- tensorflow - 我可以在图像分类场景中为单个图像设置多个标签吗
- angular - 使用 TranslateService 的服务的 Angular 单元测试
- c# - c#如何从包含格式的字符串中提取格式(例如string =“Printed on {0:dd MMM yyyy}”,我想要dd MMM yyyy
- python - 使用 pyyaml 生成带有覆盖的 yaml 锚点/引用