cypress wait for redirection after login

Viewed 16185

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);
}
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.

Related