-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asserting the response of ajax requests #937
Comments
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 |
Thanks @jennifer-shehane ! will surely try to add that to the docs myself :) but I still get timeout after 5000ms my code: describe("My First Test", function() {
it("Visits the Kitchen Sink", function() {
cy.visit("http://example.com/crisis.html");
cy.server();
//tried both versions, full url and relative
// cy.route("GET", "http://example.com/contact.php").as("identify");
cy.route("GET", "/contact.php").as("identify");
cy.get("#contactID").type("email@gmail.com");
cy.contains("submit").click();
cy.wait('@identify').then(function(xhr) {
//never gets here
});
});
}); this is the error:
any ideas ? :| |
Got it :) just needed to use regex in the url instead of |
It took me a long time to get this working and I didn't find any examples that included assertions, so I made this working gist: https://gist.github.com/axle07/758885e866987be96f8dbfd8b19a3d5e |
What if I need to call this |
@chalsxevior I like to use https://regex101.com/ to check regex against my routes. |
The gist above didn't work for me, but this did in case anyone is looking for help: xhr example. It would make a nice addition to the docs if it hasn't already been added somewhere I missed. |
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:
The text was updated successfully, but these errors were encountered: