DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Did We Build The Right Thing? That's What UAT Is About.
  • Testcontainers: From Zero To Hero [Video]
  • Diagnosing Test Failures at Scale: Correlating Execution Evidence Across Ephemeral Runners
  • AI-Powered CI Test Failure Triage

Trending

  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • Git Branching Strategies for Maintainable Test Automation
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • The Real Cost of Change in Software Development

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Cypress Tests: Preserve Cookies and Keep Login Session Active

Cypress Tests: Preserve Cookies and Keep Login Session Active

This article explains step-by-step how to preserve and share the cookie/session between tests in an easy way.

By 
Ganesh Hegde
·
Aug. 20, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

By default, Cypress resets the sessions before each test. If you are logged in scenario first test (ex: it() block), then in the second test (second it block), and you are performing some other task, you are automatically taken back to the login page. This happens because Cypress doesn’t keep the session; it creates a new session. In end-to-end testing, mostly, users will be logged first and then all the workflow will be checked. This article explains the simplest way to overcome this problem using just a few lines of code.

Let me explain this scenario:

Testcase 1: You have logged into the application.

Testcase 2: You will click on the menu to verify some feature.

If you are using Selenium, the above option looks very simple.  However, in Cypress, after the execution of the first test case(Testcase 1), it will reset the session it’s like a fresh restart of the browser.  Therefore, when you arrive at Test case 2, you will be again asked to log in and perform all the actions in Testcase 1. If you want to fix that problem in Cypress, let us discuss the steps in an example to follow.

If you are not bothered about specific cookies and all you want is to preserve the login session active across the tests, you can follow the steps below. Also, note that in the steps below, the shared code preserves all the cookies in the Cypress test once you are logged in, and shares the cookies in each test so you no need to log in repeatedly.

There are many questions on the internet such as:

  • How to not flush cookies between tests
  • How to use Cypress to preserve cookies
  • How to preserve the Session in Cypress.io
  • How to restore cookies in Cypress
  • How to stop re-logins after each test in Cypress.io
  • How to preserve and not clear the cookie when the next test starts
  • How to create and preserve Cookies with Cypress.io
  • How to preserve cookies between tests in Cypress
  •  How to preserve cookies through multiple tests

Below is the simplest, easiest, and most generic approach, taking into consideration those who are just beginning with Cypress.  I hope this is of help to someone.

Step1: Navigate To index.js Located in the Cypress/Support Folder

Step 2: Copy and Paste the Code Below

JavaScript
 
afterEach(() => {
			//Code to Handle the Sesssions in cypress.
			//Keep the Session alive when you jump to another test
			let str = [];
			cy.getCookies().then((cook) => {
				cy.log(cook);
				for (let l = 0; l < cook.length; l++) {
					if (cook.length > 0 && l == 0) {
						str[l] = cook[l].name;
						Cypress.Cookies.preserveOnce(str[l]);
					} else if (cook.length > 1 && l > 1) {
						str[l] = cook[l].name;
						Cypress.Cookies.preserveOnce(str[l]);
					}
				}
			})

Note:   If you are seeing a timeout error after the above code, follow these steps:  

  • Navigate to cypress.json file located in the root folder.
  • Add line:  "defaultCommandTimeout":30000

Refer to the example project here.   

That's all! Happy Testing!!!

Encourage me to write more articles by buying a coffee for me.

If you are looking for any help, support, guidance contact me on LinkedIn|https://www.linkedin.com/in/ganeshsirsi

Testing Session (web analytics)

Opinions expressed by DZone contributors are their own.

Related

  • Did We Build The Right Thing? That's What UAT Is About.
  • Testcontainers: From Zero To Hero [Video]
  • Diagnosing Test Failures at Scale: Correlating Execution Evidence Across Ephemeral Runners
  • AI-Powered CI Test Failure Triage

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook