'Cypress get the data from cy.route
I am using Cypress to test a ReactJs application. I need to grab some data from this request:
cy.route('GET', '/user', 'fixture:USER/user').as('user');
Given('Check user', () => {
cy.route('GET', '/user', 'fixture:USER/user').as('user');
cy.wait('@user')
.then(({ requestBody, responseBody, status }) => {
expect(requestBody.name).to.eq('admin');
});
});
Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: user. No request ever occurred.Learn more
Is there a possibility to get the result of the above request. I need this to check if user has a specific role, but the information about the user role is returned by the request above.
Who can help with this?
Solution 1:[1]
Although deprecated, you have to begin with cy.server() somewhere before you call cy.route().
Also, you have to set your cy.route() or cy.intercept() before you anticipate the call to occur. Most of the times it will be around a cy.visit().
cy.intercept('GET', '/user', {fixture: 'user.json'}).as('user')
// some code that will make a request to /user
cy.wait('@user')
.then( response => {
// code to use fixture data
})
Solution 2:[2]
Something like this should work:
Given('Check user', () => {
cy.intercept('GET','/user', { fixture: 'user.json' }).as('user');
cy.wait('@user')
.then(({ request }) => {
expect(request.name).to.eq('admin');
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jjhelguero |
| Solution 2 |
