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.

After updating to Cypress 10.4.0 localStorage and sessionStorage are not populated with part of login happening in cy.origin() and login process fails

See original GitHub issue

Current behavior

Before Cypress 10.4 update, the test framework had the current workflow:

  1. In each test, beforeEach runs Commands.login()
  2. Commands.login() contains the login wrapped inside cy.session to preserve session data
  3. Inside the cy.session, part of the login is inside cy.origin, because a part of login process is done in secondary domain
  4. Rest of the steps are done in the initial first domain

Problem with part 4: now after Cypress 10.4 update, when it returns from the secondary domain, it looks like both localStorage and sessionStorage are emptied, thus the test cannot continue without the data stored in both of the storages.

Desired behavior

cy.session() should preserve localStorage and sessionStorage as documented.

Test code to reproduce

import { Commands } from '../support/commands';
beforeEach(() => {
    cy.viewport(1412, 937);
    cy.log('I run this text and login before every test in every spec file!');
    Commands.login(Cypress.env("username"), Cypress.env("password"));
});

…/support/commands.ts

public static login = (username: string, password: string): void => {
        cy.session([username, password], () => {

	    // Visit main page, we are in firstdomain.com
            cy.visit(Cypress.env("testUrl"));

            // Click the button in the firstdomain that redirects to secondary domain
            cy.get(':nth-child(1) > .--span-s-12 > .c-button > .button-container > .button-text').click();

            // Commands that need to run in another domain
            // Anything that runs in the secondary domain needs to be wrappen inside cy.origin for the test to continue
            cy.origin('different.domain.com', { args: [username, password] }, ([username, password]) => {
                // Enter login info in secondary domain
                cy.get('#email').click();
                cy.get('#email').type(username);
                cy.get('#password').click();
                cy.get('#password').type(password);
                cy.get('#next').click();
            });
            
            // Now we're redirected back at the original firstdomain.com, so we need to continue commands outside of cy.origin
	    // PROBLEM: In Cypress 10.4.0 the sessionStorage and localStorage are not preserved at this step, at least it looks like that from DevTools

            // Select company from list, which can't be found in 10.4 because it's not in the correct page
            cy.contains("72", { timeout: 6000 }).click();
            
            // Do other stuff in firstdomain.com..

            // Session ends    
        });
    };

Cypress Version

10.4.0

Other

This worked fine until 10.3.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Wilhopcommented, Sep 14, 2022

@AtofStryker I can happily confirm that after updating 10.8.0 the login is now working and cookies/storages are preserved.

Was the fix this item on the release notes? “Fixed an regression introduced in Cypress 10.3.0, and further exposed in 10.4.0, that omitted same-site cookies when the URL Scheme, Domain, and Top Level Domain matched, but the ports are different (i.e. same-site). Fixes #23132.”

I believe the issue can be closed, hopefully not introduced again 😃

1reaction
AtofStrykercommented, Sep 13, 2022

@Wilhop We released some cookie changes today with 10.8.0. Are there any improvements with your issue? My guess is the getCookie issues might still persist, but hopefully correct cookies are now being sent from the proxy server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog - Cypress Documentation
Testing multiple origins in a single test with the new cy.origin() command. Caching and restoring cookies, localStorage , and sessionStorage between tests ...
Read more >
cypress-io/cypress - Gitter
Hi, I'm looking into skipping through the login parts of all our tests, through api-calls. I got the idea from watching your talk...
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 Local Storage Commands - YouTube
If you want to preserve local storage between Cypress tests or disable local storage to check error handling. You can extend Cypress Cy...
Read more >
Use LocalStorage From Cypress Test To Set Initial Data
We can set the initial data to be read by the application by using localStorage from the Cypress test. We can then verify...
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