我有以下工作代码来记录响应。现在,尝试找出一种记录请求的方法。我们如何记录它?它只是返回未定义。
我试图在 cy.request 上使用 log=true,但它只记录在浏览器控制台上。最好写入文件。
it('Query Endpoint', () => {
request = cy.request(
{
method: 'POST',
url: '/api/service',
auth: {
username: Cypress.env('username'),
password: Cypress.env('password')
},
headers: {
'Content-Type': 'application/json',
},
body: {},
log: true
})
.should((response) => {
cy.log(request.body) // <-- how can you log the request?
cy.writeFile('cypress/responses/api-service.json', request.body) // <-- how can you log the request?
expect(response.status).to.eq(200)
cy.writeFile('cypress/responses/api-service.json', response.body)
})
})
任何帮助,不胜感激。
您可能需要使用.then
而不是should
,并且request
应该在 promise 内部解决。它正在夹具文件夹中为我写入一个文件。但是在我留下的答案中,与您问题中给出的path
相同。
it('Query Endpoint', () => {
cy.request(
{
method: 'POST',
url: '/api/service',
auth: {
username: Cypress.env('username'),
password: Cypress.env('password')
},
headers: {
'Content-Type': 'application/json',
},
body: {}
})
.then((request) => {
const someRequest = JSON.stringify(request);
cy.writeFile('cypress/fixtures/api-service.json', someRequest)
//...rest of the code
})
})