Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cypress wait for redirection after login

I am testing a web app with cypress.

I sign in in my beforeEach function. Then, in my different tests, I begin with cy.visit('mysite.com/url').

My problem is that after sign in, the website redirects to a specific page of the website. This redirection happens after the cy.visit of my tests. Therefore, my tests run on the redirection page, they fail.

The redirection seems to be not linked with any request I could wait for.

I end up with cy.wait(3000) but it is not very satisfying. My tests failed sometimes because the redirection might take more than 3 seconds. I do not want to increase that time because it would take too long to run my tests.

Is there a way to do something like:

while (!cy.url().eq('mysite.com/redirection-url')) {
  cy.wait(300);
}
like image 544
Gaut Avatar asked Oct 27 '20 11:10

Gaut


1 Answers

Cypress provides retry abilities on assertion. You can resolve the waiting issue for the redirection URL with the below change

cy.url().should('contain', '/redirection-url')

OR

cy.url().should('eq', 'mysite.com/redirection-url')

Here should assertion will wait till 4 seconds by default and retries cy.url()

You can change the default timeout by updating parameter in cypress.json file

{
  "defaultCommandTimeout": 30000
}

Hope this will solve your issue.

like image 153
Krishna Avatar answered Sep 30 '22 12:09

Krishna