Cypress is an automation tool useful to perform automation testing and analyze regression failures. As compared to other automation tools, Cypress is one of the best tools useful for our automation project. Cypress provides many features like dynamic wait, direct interaction with browsers, recording video, and screenshots of failed tests, support component testing, etc. Thus, Cypress testing is one of the best testing process.
As a cypress automation QA, it is very important to know the important features provided by cypress that can be implemented in modern-day applications. Not only knowing it, but it is also very important to implement it in our cypress automation project.
Modern Testing with Cypress
Nowadays the customer is expecting an out-of-box automation framework implementation from QAs. This made the task of developing a good quality automation project, a challenging task.
To overcome these difficulties, cypress provides some features that can be implemented in our automation project. With these features, the project will become reliable, reusable, readable, and maintainable.
The features of cypress are:
- Design pattern implementation
- Usage of data-driven framework
- Creating custom commands
- Creating plugins
- Component testing
Design pattern implementation
The design pattern is a pre-defined solution for a particular problem. There are many design patterns available to us, and most of them are used by developers in their development code. As a QA, we can implement a design pattern named “Page Object Model” in our automation project.
Page Object Model(POM) is an architectural model in which all the locators of a web page are defined in a single file. We can create an object for this file and use it in our test cases.
We can implement this feature in our cypress project also. For example, on the login page, we have a username, password, and login button. In cypress, we can create a file named “login.page.ts” that contains all these locators as shown in the image below.
Here we have defined all the locators of the login page in this file and we are exporting it with the name “$page”. We can import this in our “spec” files and use it as an object to invoke locators wherever needed.
Consider the image shown below.
In the above image, we have a spec file named “login.spec.ts” and we have imported the login page in line no: 1, lines no: 15, 16, and 17 we are invoking the locators defined in the login page. In the same way, we can import this login page in many spec files wherever needed and reuse it.
This way of implementation is Page Object Model(POM). POM has many advantages such as reusability, readability, ease of understanding, and above all this, it avoids code duplication.
Usage of data-driven framework
The data-driven framework is useful for handling data in an optimal manner. We use different sets of data for Cypress testing different scenarios of a single functionality. To use these different sets of data in the automation code, it is not a good practice to hard code this data in a spec file directly.
To overcome this problem, cypress provides the ability to implement the data-driven framework. In cypress, these data can be stored in the form of a JSON file and all these JSON files can be stored in a separate folder named “fixtures”.
In the above image, we can see the “fixtures” folder and inside the folder, we have the “login.json” file. We can define different sets of data for login functionality in this JSON file and use it in spec files wherever needed.
The main advantage of this data-driven implementation is reusability and avoiding duplicate code.
Creating custom commands
Cypress provides us with the ability to create custom commands. Usually, we make use of the pre-defined commands provided by cypress in our automation code.
But in some scenarios, it is an important requirement to create our own custom commands in our project depending on project requirements. In such a scenario, we can write custom commands in a file named “command.ts” in cypress.
This file is automatically created when we install cypress and it is present inside the “support” folder. As a general scenario, in all the applications the login page is the first page and the Cypress testing starts from there.
So, we can define a custom command for login inside the “command.ts” file and call that function wherever necessary.
Consider the image below.
In this image we have defined a custom command named “loginUsnigEmail” in line no: 37. This command we can call in a spec file as shown in the image below.
In this image, we can see that in line no: 14, we called the “loginUsingEmail” custom command which we defined in our “command.ts” file. Like this, we can call the same command wherever necessary. The main advantage of this custom command is reusability. In the above scenario, instead of calling the “loginUsingEmail” command in “it()” of all spec files, it is a good practice to call it in the “before()” of the “index.ts” file in the “support” folder.
Plugins enable us to tap into the node process that is running outside the browser. Plugins provide us the ability to alter the resolved configuration and environment variables that come from the cypress configuration file.
This also enables us to use multiple environments with their own configurations and swap out environment variables based on an environment. For example, in the below image, we can see that we have three environment JSON files named qa1, qa2, and qa3.
We can define which JSON file data to use during our cypress run in the “index.ts” file of the “plugins” folder as shown in the image below.
In the above image line no: 20, we specified the default environment as qa2, if no environment name is defined in the command prompt. If we provide the environment name in the command prompt during the cypress run, it will invoke the corresponding JSON file.
Cypress provides us the capability to test the components of a web page. This feature of component testing is available from Cypress version 10 and above. Usually, the developers use component testing in building and testing components from multiple front-end UI libraries.
For example in the image below we have a test to assert a button component has the correct text.
Moreover, we can assert the background color, styles, and appearances using component testing.
From the above features that Cypress provides, we can conclude that Cypress is one of the best automation tools that we can use in automating our applications.
“Need help? Perfomatix |Software Testing
We are Perfomatix, a top Software Testing company. We specialize in building highly scalable APIs and Mobile apps and we also have strong expertise in IoT apps, Virtual Reality apps, and Augmented Reality apps. Get in touch with us to find out how we can help in shaping your disruptive idea into a prototype, MVP and finally into a killer product.
Visit our success stories section to find out more about some of the startups which made it big with us.”