question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No examples on how to validate XHR response (body, content-type and other)

See original GitHub issue

Like in a title. I have found only this: https://github.com/cypress-io/cypress/issues/937

But there is only a short info:

cy.route('GET', '/login').as('getLogin')
cy.get('#contactID').type('email@gmail.com')
cy.contains('Login').click()
cy.wait('@getLogin').then(function(xhr){
  // we can now access the low level xhr
  // that contains the request body,
  // response body, status, etc
})

How to access low level xhr? I would love to see some examples how to validate response body; for example: my response is:

[{
    "Key": 16176,
    "LineName": "Line A",
    "displayFullName": "A-0300/0350 801"
  },
  {
    "Key": 14567,
    "LineName": "Line B",
    "displayFullName": "C-876867"
  }
]

I know that one element name should be A-0300/0350 801, now I want to verify that and also I need to retrieve it’s Key.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
jennifer-shehanecommented, Jun 18, 2018

After accessing the array, it’s mostly a matter of doing some functional programming to assert on what you want. Some examples -

cy.wait('@getLogin').then(function(xhr){
  const response = xhr.responseBody
  expect(response[0]).to.have.property('LineName', 'Line A')
  
  const obj1Values = Cypress._.values(response[0])
  expect(obj1Values).to.deep.eq([16176, "Line A", "A-0300/0350 801"])
})

Hopefully this helps you get started!

1reaction
bahmutovcommented, Dec 13, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLHttpRequest.responseType - Web APIs | MDN
The XMLHttpRequest property responseType is an enumerated string value specifying the type of data contained in the response.
Read more >
How to get the response of XMLHttpRequest? - Stack Overflow
Here's an example (not compatible with IE6/7). var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.
Read more >
Asserting on payloads · GitBook - GitHub Pages
Since we have a list of all the things we need to assert about, writing all the assertions is easy: request payload assertions....
Read more >
Asserting Network Calls from Cypress Tests
The Cypress Test Runner can. ... We can see the outgoing XHR request and the server's response by looking at the DevTools Network...
Read more >
XMLHttpRequest - The Modern JavaScript Tutorial
Some request methods like GET do not have a body. ... searchParams.set('q', 'test me! ... For example, let's get the response as JSON:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found