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.

asserting the response of ajax requests

See original GitHub issue

Is this a Feature or Bug?

bug, cant find docs on https://docs.cypress.io/guides/guides/network-requests.html#Testing-Strategies need to know how to assert on responses I get from the server.

Probably its me, but maybe others cant find how as well 😕

Test code:

cy.get('#contactID').type('email@gmail.com')
cy.contains('Login').click()
// I have an ajax call here, want to assert on the reponse

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tzookbcommented, Nov 20, 2017

Got it 😃

just needed to use regex in the url

instead of ‘contact.php’ used /contact.php/

1reaction
jennifer-shehanecommented, Nov 20, 2017

In order to assert on the response of an XHR request, you’ll need to setup a route to listen to the request you want to assert on.

I like to alias the route so that I can do some things in between listening and waiting for the response to assert on.

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

Please note that there is limited support for ‘fetch’ - see this issue if you are using fetch.

I agree, the docs could have better actual examples with the testing strategies - or at least link to them. I suggest you open an issue in our docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

QUnit test ajax success function - javascript - Stack Overflow
ajax = function(request) { assert.equal( request.url, 'test_response.html', 'request url is correct' ); assert.
Read more >
How to Write Unit Tests for Ajax Requests using Mocha
Handling the response includes pairing the received data within an object. Providing that you already prepared the data and dataJson variables ...
Read more >
Asserting on payloads · GitBook - GitHub Pages
the AJAX call has the wrong request payload ... the response payload contains both the email and the username , the token is...
Read more >
How to Handle AJAX Call in Selenium Webdriver - Guru99
AJAX sends HTTP requests from the client to server and then process the server's response, without reloading the entire page.
Read more >
Mocking AJAX Requests | Testing Your JavaScript ... - InformIT
The answer should be obvious immediately: The @collection object isn't receiving a response from the server when the fetch function is called, ...
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