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
Over 2 million developers have joined DZone.
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]
  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team

Trending

  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Invisible Failures in S/4HANA Conversions (And Why Teams Miss Them)
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • How AI Coding Assistants Are Changing Developer Flow
  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 user avatar
Ganesh Hegde
·
Aug. 20, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
21.0K Views

Join the DZone community and get the full member experience.

Join For Free

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]
  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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