Passing variables between tests in Cypress

Testing business workflows end2end with cy.task()

Paul de Witt
Feb 4 · 3 min read
Photo by Aaron Burden on Unsplash

I always try to follow Cypress’s best practices when writing robust and isolated test cases. If test data is needed I use fixtures, so that I can predict the outcome. But sometimes you want to test an entire workflow, where different types of users execute different parts of the process.

Normally you would test all those different parts in separate spec files. But if you already have created these specs, you can re-use those. All we have to do is store the output as state so that we can use it as input for another spec.

Behind Cypress, there runs a Node.js server process in the background. You can make use of Node and store temporary data there. You can even seed a test database if you like! As long as you don’t close the Cypress console, values from the last run persist here.

Create a task

How do you do this? Well, with the command cy.task() you can pass a value to Node.js. You have to create a ”get” and “set” command. If you are familiar with getters and setters like you have in Java, this will be a piece of cake.

Getters and setters

So let’s start creating a getter and setter to pass a variable. Let’s say we have an onboarding flow, where the user will get a unique id after he or she has successfully registered. Let’s call it userId ( I know, not very original ).

Go to the index.js file in the support folder. Here you have to create the commands.

First, we create the setter:

setUserId: (val) => { return (userId = val); }

And second, we create the getter:

getUserId: () => { return userId; }

Now you are all set to implement this in your tests!

The task ahead

Let’s add the setter to the first spec file, where we test the onboarding flow. By doing this, the value will be stored in the Node.js backend.

cy.get('User').then(($userId) => {   cy.task('setUserId', userId);});

And in the second spec, we have to use the userId to log in, so we have to retrieve it first.

cy.task('getUserId').then((userId) => {   cy.get('User').type(userId);});

And that’s it! Now you can execute the entire process just like it is being executed in real life. Essentially you have covered this already with isolated tests and fixtures. But in my experience when test automation is relatively new for an organization, creating a test flow like this can help grow trust in automated testing principles. Especially if you can show that the results between the isolated tests and the workflow test are the same.

Wrap up

As you can see it is really easy to store state in Node, with Cypress. This way you can access the data in different spec files, or you might even seed a database with it. I am a firm believer in Cypress’s best practices, so I always try to create small, independent test cases. But I am very pragmatic as well. And if this means creating workflow tests, I will. As long as it helps to build trust in test automation, and makes my clients happy.

Quick Code

Find the best tutorials and courses for the web, mobile…

Paul de Witt

Written by

IT entrepreneur, Test Automation Specialist, trainer, DJ, and father of three. Creator at heart.

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Paul de Witt

Written by

IT entrepreneur, Test Automation Specialist, trainer, DJ, and father of three. Creator at heart.

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store