Visited page leaks between tests
See original GitHub issueCurrent behavior
Between tests, if before/beforeEach
execute for a long time, the last visited page is still active, and behavior on that page might cause the next test to fail.
Explanation - real world context
We are developing a web app (Next.js) that requires users to log in. We use Auth0 for authentication, and then keep the user’s token in a cookie that is transmitted to the server. In order to make sure the current user’s session is not expired, our app periodically calls an API route that verifies the client’s access token is unexpired. If it is expired, the app redirects the user to the Auth0 login flow to re-login.
Because of Cypress cross-origin limitation, at the beginning of the tests we perform a login through cy.request
and keep a signed cookie for the duration of the tests. The cookies get cleared between tests, and we load them into the browser before visiting a page that requires authentication.
Now for the flow:
- We have a test that finished running. The browser is still open on the last visited page
- Cypress starts running
before
for the next test. It clears the browser’s cookies - The open web app decides to check if the user’s session is still valid. It sends a request, which fails (because cookies have been cleared). So the web app decides to redirect to Auth0 login form
- Cypress errors with a cross-origin problem
Here is a sequence of screenshots:
New test suite if running before
, but the previous browser state is still visible.
We got redirected to Auth0.
Desired behavior
I think that when a new test suite starts, the previous state of the browser shouldn’t still be active. And certainly browser actions shouldn’t impact the result of the new test suite.
Test code to reproduce
I created a reproduction in Cypress RWA that shows how the previous page stays loaded if the before
block takes a long time to execute.
You can run the test tests/ui/notifications.spec.ts
and see what happens when cypress moves from the first describe()
block to the second.
https://github.com/adarnon/cypress-before-wait
Versions
Cypress version: 6.9.1, 7.5.0. This error occurred on Ubuntu 20.04, Electron 87. But it also statistically reproduces locally on MacOS Big Sur & Chrome 91.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14 (6 by maintainers)
Top GitHub Comments
K, I’ve simplified the example where the application can still trigger side effects in the ‘before’ of the next spec.
Reproducible Example
spec.js
index.html
A workaround for this that I think would work today is a bit sideways, but with our new
experimentalSessionSupport
feature, Cypress will visitabout:blank
in between every single test case. I believe that turning this feature on globally (or for the affected spec) may solve this issue. Can anyone try this out and report if this is the case?Please make sure to read about
cy.session
and what it does before making those updates.