1

When I'm running cypress e2e tests, application makes XHR requests. How can I log all this requests and responses? I don't want to stub these requests. I with to get an artifact with all requests and responses made during test. Gitlab is used as CI.

Main test code looks like this. All these are user defined commands, interacting with the application. Interacting with the application causes different requests to be made (e.g. I click a button, this causes the request).

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() }) }) 

Here is how I tried to use regular expression to catch request (id is unique and I need to use regular expressions).

https://<mysite>/home/payments/<currency>/confirm/* - asterisk is payment id. 
2
  • 1
    Do you want to log these request and response to some location as file ? Is that what you need ? Commented Oct 22, 2019 at 22:48
  • Yes. Thats exactly what I need. Commented Oct 23, 2019 at 7:10

2 Answers 2

2

You could grab the request and response and write to a location as below. I have write the request and response to fixture folder as below: Try the below and let me know

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); }) }) 

// The below is for response:

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); }) }) 
Sign up to request clarification or add additional context in comments.

10 Comments

Where should I add my test steps? Or this is an example of a test just making one request? I need to log all requests and responses made during test. Is there a setting in Cypress to do this?
As per the above example, cypress will log the request and response only while running the above test.
I haven't find a general setting for that.
If I understand correctly, I can add my code after yours, and add url? Can I add regular expression as url?
Unable to tell until I see your test code, please post your test code and url reg pattern
|
0

The testrunner has such information on board: [1]

2 Comments

Sorry, forgot to mention that I run browser in a headless mode on CI server.
check, I can't help you with that one. I tried searching, but it doesn't give me a good solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.