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?
Use timeout per command Prolonging the timeout for the whole test might not always be the best way. Sometimes, you simply want to wait until a certain element appears, but everything else on the page is pretty fast. For these cases, you can use the options object and change timeout for a certain command.
As Cypress internally retries commands, we don't need to add any wait clause to ensure the element is visible before verifying it. Make sure you use timeouts sparingly. Most of the time you will be fine with using the default timeout.
wait() , Cypress will wait for all requests to complete within the given requestTimeout and responseTimeout .
With cypress, cy. visit() can be used to wait for a page to load.
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.
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.
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With