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);
}
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.
If the redirect is to a different server, simply using cy.url does not work.
We solved the problem with the following code:
cy.origin('https://localhost:7256/',() => {
cy.url().should(
'contains',
'account/login');
})
where https://localhost:7256/
is the base URL of the server to which we are redirected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.