cypress - 我可以登录以归档在运行 cypress 测试期间做出的所有响应吗?
问题描述
当我运行 cypress e2e 测试时,应用程序会发出 XHR 请求。如何记录所有这些请求和响应?我不想存根这些请求。我将获得一个包含测试期间提出的所有请求和响应的工件。Gitlab 用作 CI。
主要测试代码如下所示。所有这些都是用户定义的命令,与应用程序交互。与应用程序交互会导致发出不同的请求(例如,我单击一个按钮,这会导致请求)。
it('Log response to a file',function(){
cy.request({
method: 'GET',
url: 'https://<site>/home/payments/currency/confirm/*',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((response)=>{
const someResponse = response.body;
console.log("hhhh"+someResponse);
cy.writeFile('cypress/fixtures/testResponse.json', someResponse);
cy.login(login_name, pass)
cy.typeOTPpinpad(secret)
cy.makePayment('Currency', 'amount')
cy.typeToken(secret)
cy.logout()
})
})
这是我尝试使用正则表达式捕获请求的方式(id 是唯一的,我需要使用正则表达式)。
https://<mysite>/home/payments/<currency>/confirm/* - asterisk is payment id.
解决方案
您可以抓住request
andresponse
并写入如下位置。我已将请求和响应写入fixture
文件夹,如下所示:尝试以下内容并告诉我
it('Log request to a file',function(){
cy.request({
method: 'GET',
url: 'url_here',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((request)=>{
const someRequest = JSON.stringify(request);
console.log("hhhh"+someRequest);
cy.writeFile('cypress/fixtures/testRequest.json', someRequest);
})
})
// 以下是响应:
it('Log response to a file',function(){
cy.request({
method: 'GET',
url: 'url_here',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((response)=>{
const someResponse = response.body;
console.log("hhhh"+someResponse);
cy.writeFile('cypress/fixtures/testResponse.json', someResponse);
})
})
推荐阅读
- python - Celery SQS 任务失败后不会执行
- php - 由于奇怪的版本错误,无法在 macOS Catalina 上安装 PHP 7.3
- html - 通过 p 标签中的 xslt i 标签将 xml 转换为 html
- python - 无法使用 openpyxl 写入输出 excel 文件
- elasticsearch - 如何从 ElasticSearch 中检索脚本字段以及其他字段
- java - 如何通过路径在 LibGDX 中绘制自定义形状?
- c - 尝试拆分字符串并将它们放入数组时收到错误消息
- java - 如何通过 Android 应用程序发出 HTTP 请求
- token - Instagram 令牌在一些请求后被阻止
- java - Querydsl 本机 jpa 查询返回实体而不是对象数组