Cypress is a front-end testing tool. It allows us developers and QA engineers who build web applications using the modern JavaScript framework to set up, write, run, and debug tests. It is getting popular as it enables us to write faster, easier, and more reliable tests. Cypress can test anything that runs in a browser.
Let’s learn about the most basic component – variables in Cypress.
Types of Variables Variables are containers for storing information.
- Variables are used to store and manage data.
- We can create a variable or rather declare a variable in JavaScript syntax for Cypress.
- In Crypress, we use objects while using closures which can be utilized without assigning them.
- However, when we need to deal with mutable objects, we use variables. Variables allow us to compare an object’s previous value to the next value. Variables are mutable types in JavaScript, which means that the value that is stored in the variable (a container) can be changed (set, removed, or modified).
Rules for naming variablesThe rules for naming variables for Cypress are the same as the rules for naming variables in JavaScript as Cypress is purely based on JavaScript. So following are the rules for naming variables:
- Variable names must start with a letter, an underscore(_), or a dollar sign($).
- Variable names can contain numbers, alphabets, underscores(_) and dollar sign($).
- Variable names cannot contain spaces.
- Special characters and symbols are not allowed in variable names.
- By convention, variable names in JavaScript are written in camelCase.
- Variable names cannot be a reserved keyword.
- Variable names should be short and descriptive to make it easy and efficient to understand.
- In JavaScript, variables do not have a set data type, (like int or integer, bool or boolean, or string and so on) .
Types of VariablesThere are three types of variables:
1. varThe var keyword is used to declare a variable. It has the following scope behaviors- function-scoped and global-scoped.
Syntaxvar variable_name = assign_some_value ;
Examplevar age = 20 ;
Code ( basic usage of var keyword)
JavaScript
var a = "Hello Geeks!"
var b = 5;
var c = 10;
var addition = b + c ;
console.log(a);
console.log(addition);
OutputHello Geeks! 15
2. letThe let keyword is also used to declare a variable. It has block-scoped behavior. It is used for variables whose values may change.
Syntaxlet variable_name = assign_some_value ;
Examplelet fruit = “ripe” ;
Code ( basic usage of var keyword)
JavaScript
let a = "Hello Geeks!"
let b = 5;
let c = 10;
let addition = b + c ;
console.log(a);
console.log(addition);
OutputHello Geeks! 15
3. constThe const keyword is also used to declare a variable, however, it is used for declaring variables that cannot be reassigned. It has the following scope behaviors- function-scoped and global-scoped. It is used for variables whose values should not change.
Syntax const variable_name = assign_some_value ;
Exampleconst pi = 3.14 ;
Code ( basic usage of var keyword)
JavaScript
const a = "Hello Geeks!"
const pi = 3.14;
const radius = 10;
const circumference = 2 * pi * radius ;
console.log(a);
console.log(circumference);
// If you change value of radius variable
// It will show error
// Because cannot change value of const variables
OutputHello Geeks! 62.800000000000004
Code Implementation of variables in CypressExample – A simple counter applicationIn below code we have 2 files, one for html and one for JavaScript.
- The HTML file provides a button to increment a counter.
- The JavaScript files (2 versions are provided to you to try and test on Cypress) has code to ensure that each test starts with a fresh page load of index.html. It has a variable num1 that captures initial count value and stores value.
- The cy.get finds the counter element and retrieves its text content which is used to initialize num1.
- The click even simulates a click on the increment button. After clicking the button, it it retrieves the updated count num2 and asserts that it equals num1+1 to ensure that the count is incremented correctly.
HTML (index.html)
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter App</title>
</head>
<body>
<button id="incrementBtn">Increment</button>
<span data-testid="counter">0</span> times clicked
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let count = 0;
$('#incrementBtn').on('click', function() {
$('[data-testid="counter"]').text(++count + ' times clicked');
});
});
</script>
</body>
</html>
JavaScript – Cypress Test (counter_spec.js)
JavaScript
describe('Counter App', () => {
beforeEach(() => {
cy.visit('index.html'); // Assuming index.html is in the root of your project
});
it('increments the counter correctly', () => {
// Using var (though not recommended in modern JS, just for demonstration)
cy.get('[data-testid="counter"]').then(function($span) {
var num1 = parseInt($span.text(), 10); // Using parseInt for clarity
cy.get('#incrementBtn').click();
cy.get('[data-testid="counter"]').then(function($span) {
var num2 = parseInt($span.text(), 10);
expect(num2).to.equal(num1 + 1);
});
});
// Using let (modern recommended approach)
cy.get('[data-testid="counter"]').then(($span) => {
let num1 = parseInt($span.text(), 10);
cy.get('#incrementBtn').click();
cy.get('[data-testid="counter"]').then(($span) => {
let num2 = parseInt($span.text(), 10);
expect(num2).to.equal(num1 + 1);
});
});
// Using const (for the immutable reference to DOM element)
cy.get('[data-testid="counter"]').then(($span) => {
const num1 = parseInt($span.text(), 10);
cy.get('#incrementBtn').click();
cy.get('[data-testid="counter"]').then(($span) => {
const num2 = parseInt($span.text(), 10);
expect(num2).to.equal(num1 + 1);
});
});
});
});
JavaScript
describe('Counter App', () => {
beforeEach(() => {
cy.visit('index.html'); // Assuming index.html is in the root of your project
});
it('increments the counter correctly', () => {
// Using let and const variables
// Capture initial count
let num1;
cy.get('[data-testid="counter"]').then(($span) => {
num1 = parseInt($span.text(), 10);
// Click the button to increment
cy.get('#incrementBtn').click();
// Verify the updated count
cy.get('[data-testid="counter"]').then(($span) => {
const num2 = parseInt($span.text(), 10);
// Assert that the count increased by 1
expect(num2).to.equal(num1 + 1);
});
});
});
});
Conclusion One needs to be proficient in JavaScript before getting started with Cypress Testing. This is due to the fact that Cypress is purely based on JavaScript and this tool is designed to meet the needs of front-end developers in particular. So, we have covered one of the basic topics, variables, in this article.
Frequently Asked Questions on Variables in CypressWhat are the different types of variables that I can use in Cypress tests?In Cypress tests, you can use var, let and const for declaring variables. Var is not used often and let and const are recommended for modern JavaScript practices.
What is the difference between let and const when declaring variables in Cypress tests?‘let’ is used for variables that can be reassigned, while ‘const’ is used for variables whose value won’t change once initialized to maintain immutability.
In Cypress, how can we share variables between different test cases?The Cypress’ support commands like cypress.env can be used for environment variables or store values in global scope to share variables across different test cases. It isn’t recommended for large-scale applications.
|