question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Visited page leaks between tests

See original GitHub issue

Current 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.

image

We got redirected to Auth0.

image

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jennifer-shehanecommented, Jun 15, 2021

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

describe('tests', () => {
  it('test', () => {
    cy.visit('index.html')
    cy.get('button').click()
  })
})

describe('more tests', () => {
  before(() => {
    cy.wait(10000)
  })

  it('test', () => {
    // we expect to still be on the page 
    // (in real world, maybe still logged into the app)
    cy.url().should('eq', 'index.html')
  })
})

index.html

<html>
<body>
  <button id='button'>Create Side Effect</button>
  <script>
    document.getElementById('button').onclick = () => {
      setTimeout(() => {
        window.location.assign("https://www.google.com")
      }, 5000);
    }
  </script>
</body>
</html>
Screen Shot 2021-06-15 at 2 13 44 PM
1reaction
jennifer-shehanecommented, Sep 10, 2021

A workaround for this that I think would work today is a bit sideways, but with our new experimentalSessionSupport feature, Cypress will visit about: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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Find Memory Leaks in Websites and Web Applications
To test the actual memory leak I created a very simple web application that simulates e-commerce user behavior tracking. The web page tracks ......
Read more >
BrowserLeaks - Web Browser Fingerprinting - Browsing Privacy
BrowserLeaks.com is all about browsing privacy and web browser fingerprinting. Here you will find a gallery of web browser security testing tools that...
Read more >
What info does your browser leak about you? Test your ...
Our free tool gives an indication of what information others see about you. What information does your browser leak? Including tips to improve...
Read more >
MemLab: An open source framework for finding JavaScript ...
Finding and addressing the root cause of memory leaks is important for ... using Puppeteer and visits the test pages in the following...
Read more >
How do I test a website for memory leaks? [closed]
For an Java application running in a java server create a integration tests with tools like JMeter and configure your server side to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found