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.

Cypress keeps old session logged in even after clearLocalStorage() and clearCookies() and window.SessionsStorage().clear()

See original GitHub issue

Hi Team,

Current behavior

I have 3 spec files and in each one I have a before hook to clear localstorage, session and cookies then login to the app

before(function () {
		cy.clearCookies()
		cy.clearLocalStorage()
		cy.visit('/people/users', {
			onBeforeLoad: (win) => {
				win.sessionStorage.clear()
			},
		})
	})

When I run the tests Cypress keeps the old session logged in and redirects to the users page as it finds the token somewhere in cache every time and login to the old session.

So what happens :

1 - First attempt : run all specs, all fails as when cypress launches the app, it’s already signed in

2 - Clear App Data manually from Cypress then restart Cypress

3 - Second attempt : run all specs, first spec and the login in the before hook pass, but second and third specs fails on the login step as cypress kept the session of the first spec executed

Closing and starting cypress did not help too

The only thing it helped is clearing the app data manually in Cypress (deleting the production folder under cy).

the tested App is running locally It uses SSO for login (an outh url redirection to enter the username and password and then it sets the oauth token in the localstorage…)

Desired behavior

Cypress should clear the app data between specs execution.

Versions

Machine : Mac with Catalina 10.15.7 Cypress latest version happens in all browsers, Chrome, Firefox for devs, Edge and Electron

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jennifer-shehanecommented, Feb 27, 2021

Right now there doesn’t seem to be enough information to reproduce the problem on our end. We’ll have to close this issue until we can reproduce it. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please comment in this issue with a reproducible example and we will consider reopening the issue. Here are some tips for providing a Short, Self Contained, Correct, Example and our own Troubleshooting Cypress guide.

0reactions
dstjcommented, Mar 16, 2022

I have the same issue. I’m running cypress run via an npm script (which is itself launched from my NUnit C# tests, but that shouldn’t matter I think) and my tests sometimes fail, sometime pass.

I look at the recorded video and it’s because the first cy.visit('/') is loading the “already logged in page” instead of the login page itself.

My test file structure is pretty simple (see below) as I’m still new to Cypress. I couldn’t deduce a pattern to which tests fail. It’s not always the same one.

describe('Registration Process', () => {
	beforeEach(() => {
		cy.clearLocalStorage() // should not be required according to https://docs.cypress.io/api/commands/clearlocalstorage
		const username = `${Math.random().toString().substr(2, 10)}@example.com`;
		cy.wrap(username).as('username')
	})

	it('Can register', function () {
		cy.visit('/')
		cy.url().should('include', '/login')
		// ...
	})

	it('Can register version 2', function () {
		cy.visit('/')
		cy.url().should('include', '/login')
		// ...
	})

	it('Can register version 3', function () {
		cy.visit('/')
		cy.url().should('include', '/login')
		// ...
	})

	describe('w/ a stubbed response', function () {
		beforeEach(function () {
			cy.intercept({ method: 'GET', url: '/api/request-only-used-after-login...', }, [])

			cy.visit('/')
			cy.url().should('include', '/login')
			// ...
		})

		it('Test 4', function () {
			// ...
		})

		it('Test 5', function () {
			// ...
		})
		
		it('Test 6', function () {
			// ...
		})
	})
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

session - Cypress Documentation
The cy.session() command will inherit the testIsolation value to determine whether or not the page is cleared when caching and restoring the browser...
Read more >
Local storage not clearing during Cypress test - Stack Overflow
There is some Session Storage that you're not clearing. The simplest way would be before(() => { cy.clearLocalStorage('bluelibs-token') cy.
Read more >
cypress-io/cypress - Gitter
Facing issue with session it's auto logout the app. ... I am still looking for the solution. ... clearLocalStorage() //Clear Cookies cy.
Read more >
How to save and restore state in Cypress tests - Reflect.run
The new cy.session() command solves this problem by caching and restoring cookies, localStorage and sessionStorage after a successful login. The ...
Read more >
How to preserve localStorage between Cypress tests
Tagged with cypress, testing, localstorage, javascript. ... saveLocalStorage() to save a snapshot of current localStorage at the end of one ...
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