Sitemap
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.

Passing variables between tests in Cypress

Testing business workflows end2end with cy.task()

3 min readFeb 4, 2021

--

Press enter or click to view image in full size
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…

--

--

Quick Code
Quick Code

Published in 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
Paul de Witt

Written by Paul de Witt

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

Responses (1)