Passing variables between tests in Cypress

Testing business workflows end2end with cy.task()

Paul de Witt
Quick Code

--

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…

--

--

Responses (1)