Skip to content
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

Closed
tzookb opened this issue Nov 20, 2017 · 7 comments
Closed

asserting the response of ajax requests #937

tzookb opened this issue Nov 20, 2017 · 7 comments

Comments

@tzookb
Copy link

tzookb commented Nov 20, 2017

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
@jennifer-shehane
Copy link
Member

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

@tzookb
Copy link
Author

tzookb commented Nov 20, 2017

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@gmail.com");
    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 ? :|

@tzookb
Copy link
Author

tzookb commented Nov 20, 2017

Got it :)

just needed to use regex in the url

instead of
'contact.php'
used
/contact.php/

@axle07
Copy link

axle07 commented Mar 15, 2018

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

@chalsxevior
Copy link

chalsxevior commented Oct 1, 2018

@tzookb

What if I need to call this /api/xyz/IN/data/ instead of contact.php . ?

@jennifer-shehane
Copy link
Member

@chalsxevior I like to use https://regex101.com/ to check regex against my routes.

@johnaschroeder
Copy link

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.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Apr 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants