mardi 28 novembre 2017

Tell Cypress to wait on an ajax request to a different origin than the app

The Cypress documentation is filled with examples where you can set an alias to a particular web request, and then you can instruct cypress to wait on it. Like for instance, this one:

cy.route('POST', '/login').as('postLogin')

cy.get('input[name=username]').type('jane.lae')
cy.get('input[name=password]').type('password123{enter}')

// we should always explicitly wait for
// the response for this POST to come back
// so our tests are not potentially flaky or brittle
cy.wait('@postLogin')

The thing is I'm trying to use this exact technique in my app's tests, but I need to wait on a request that is not made against the same server where the app is hosted. Therefore I am assuming that I can type in the full URL of the backend endpoint, so that cy.route does not prepend the baseUrl, which is the host of where the frontend app is hosted.

// The following URL points to a backend rails app
// The frontend app I'm testing with cypress is at http://localhost:8080
cy.route('POST', 'http:/localhost:3000/session').as('postLogin')
// ... fill login form
cy.wait('@postLogin')
// The test never reaches this point

However, when I run my test, I get a timeout because cypress never realizes the request was performed, it continues to wait on it until it timeouts. Turns out when I inspect the test script on the left sidebar in the browser where cypress is running, the route ignored the explicit host I set, and assumed only the path, which leads me to believe it was waiting for a request to my frontend app host, instead of the one I set explicitly.

The question is: can I make cypress aware of cross-origin requests so that it can wait for them? If so, how?

Aucun commentaire:

Enregistrer un commentaire