Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cypress.io waiting for same alias

Tags:

cypress

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?

like image 525
gruuuvy Avatar asked Oct 28 '19 04:10

gruuuvy


People also ask

How do I stop waiting in Cypress?

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.

How do you wait for an element to appear in Cypress?

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.

What command can be used to wait in Cypress?

wait() , Cypress will wait for all requests to complete within the given requestTimeout and responseTimeout .

What is the best way to wait and test the page in Cypress?

With cypress, cy. visit() can be used to wait for a page to load.


2 Answers

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.

like image 124
bbsimonbb Avatar answered Sep 22 '22 09:09

bbsimonbb


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.
});
like image 34
Andrii Zelenskyi Avatar answered Sep 25 '22 09:09

Andrii Zelenskyi