Reputation: 1159
I have the following working code to log the response. Now, trying to figure out a way to log the request. How can we log it? It just returns undefined.
I tried to have log=true on cy.request, but it's logging only on the browser console. Would be best to write to a file.
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)
})
})
Any help is appreciated.
Upvotes: 3
Views: 5223
Reputation: 12009
You might need to use .then
instead of should
and the request
should resolve inside promise. It is writing a file for me inside the fixture folder. But in the answer I left as the same path
as given in your question.
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
})
})
Upvotes: 4