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 :/
cy.get('#contactID').type('[email protected]')
cy.contains('Login').click()
// I have an ajax call here, want to assert on the reponse
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 protected]')
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
eventhough I see the request
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 protected]");
cy.contains("submit").click();
cy.wait('@identify').then(function(xhr) {
//never gets here
});
});
});
this is the error:
CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'identify'. No request ever occurred.
any ideas ? :|
Got it :)
just needed to use regex in the url
instead of
'contact.php'
used
/contact.php/
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
@tzookb
What if I need to call this /api/xyz/IN/data/ instead of contact.php . ?
@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.