Reputation: 2129
cy.server();
cy.route('POST', 'my/api').as('myApi');
...
cy.wait('@myApi');
...
cy.route('POST', 'my/api').as('myApi');
cy.wait('@myApi');
When my app calls the same API twice within the same test, from the above code, the 2nd cy.wait
finishes immediately since it sees that the first API is already finished. To get around this, I append a random number behind all my route aliases. Is this the correct way?
Upvotes: 10
Views: 3884
Reputation: 11
In this case, for the second wait, you can try the following.
cy.server();
cy.route('POST', 'my/api').as('myApi');
cy.wait('@myApi').then(() => {
// You probably want to add some assertions here
});
// Do your stuff here
cy.wait(['@myApi', '@myApi']).then(() => {
// Second assertion. Probably, something should be changed in the second request.
});
Upvotes: 1
Reputation: 316
Thank you for the question! I think the previous answer is totally right. By default, cypress routing is just aliasing. You could find a similar example in the cypress documentation here.
So, you code should be something like that:
cy.server();
cy.route('POST', 'my/api').as('myApi');
cy.wait('@myApi').then(() => {
// You probably want to add some assertions here
});
// Do your stuff here
cy.wait('@myApi').then(() => {
// Second assertion. Probably, something should be changed in the second request.
});
Upvotes: 1
Reputation: 29002
You might be able to do better. The cy.route() command is just a definition, so you should group all your routes at the top of the file. Routes only need to be defined once. Then try chaining your waits, as in cy.wait().otherStuff().wait()
or at least chaining your waits with other stuff that has to succeed first.
Upvotes: 2