These days, the field of website development is growing at a very fast speed. To make the website meet the business requirement, you need to make your website efficient enough to handle a huge user base and also provide them with the best user experience. There are various testing tools and frameworks available which are used to perform website testing. Cypress is one of the popular web testing tools which is mostly recommended.
There are some times when you may face test failure while performing Cypress automation testing. But in Cypress, you can give the failed test a chance to rerun the test and it may be a success. This is known as the retry strategy. It is used to increase the testing stability. There are some other testing strategies that can make this even better. In this article, you will first see a brief introduction to Cypress Automation and then you will see how you can enhance test stability with retry strategies in Cypress automation.
What is Cypress Automation
Cypress is an open-source automation testing tool that is used to perform web component testing. These components run in a web browser for which you can perform automation testing. Cypress is basically based on JavaScript language. It allows you to perform several tests, such as unit testing, integration testing, and also end-to-end testing, which helps to increase the software application efficiency and reliability. Cypress is a very developer-friendly tool because it operates directly in the browser using a DOM manipulation technique. Using the DOM manipulation allows the front-end developers and QA engineers to write automated web tests while eliminating pain points.
As we know, there are many tools and frameworks available to fulfil the demand for automation testing for web apps, but Cypress is based on JavaScript, which is a universal and preferred programming language by developers. So, Cypress becomes a prime choice for the testing and QA team. Cypress helps to easily and quickly identify and debug bugs, which enhances the test stability of the Cypress automation process. Cypress offers very fast automation test execution, which increases the testing efficiency in less time. You can also use cloud-based Cypress automation testing because it has several benefits. Using cloud-based automation testing greatly increases testing stability.
You can modify the resource usage according to your needs. This helps to save the extra cost of resources as you need to pay for only those resources that you use. All these benefits suggest that you should prefer cloud-based automation testing platform to perform Cypress automation. One of such platform is LambdaTest. It is an AI-powered test orchestration and test execution platform, revolutionizing how developers and testers approach automation testing across a 3000+ farm of real browsers and operating systems combination.
It’s not just another cross-browser testing tool; LambdaTest is engineered to simplify and elevate the automation testing journey. Its strength lies in tackling even the most intricate and unpredictable testing scenarios, providing a reliable solution for diverse testing requirements.
What makes LambdaTest truly exceptional is its intuitive interface and advanced functionalities. Teams can seamlessly automate tests, ensuring thorough and efficient testing across a spectrum of browsers and operating systems. In summary, LambdaTest emerges as a versatile and sturdy ally for those seeking to streamline their automation testing processes.
Now, let us see what a test is in Cypress automation and how it works to better understand how it enhances the test stability in Cypress automation.
Introduction to Test Retry
As we know E2E (End-to-end) tests are capable enough to perform testing for complex systems. But still, there are some actions that are not easy to identify, and they make the test flaky. A flaky test is a test that is not reliable. It sometimes fails due to unfavorable conditions. These conditions include power cutoff, any kind of network issue, etc. There are also some other conditions in which you can face these rare conditions, which will lead to unreliable tests. These conditions are API calls, Test server availability, resource dependencies, etc.
To handle such rare conditions and avoid this, you can use test retries. The Cypress retry helps the developers and QA team detect test flakiness and continuous integration (CI) build failures. Cypress retry saves a lot of testing time and resources so that you can concentrate on better testing work, which is the ultimate goal of Cypress automation testing.
How does Test Retry Work?
Now, let us see how the test retry works with Cypress automation. Test retries were initially developed to give more chances to the tasks that are going to fail so that they can anyhow get passed. Suppose, for example, there is an instance whose CI environment is not reliable for automation testing purposes; then Cypress retry will give the maximum chance of retries so that the Cypress automation test gets passed.
As considered in the above example, CI environments for testing are unreliable while still being determined as flaky even if they get passed. However, this is not the expected testing output in every case. By default, tests will not retry when they fail. But if you want to use this feature for your Cypress automation testing, then you need to enable test retries in your configurations.
When you enable the test retries, you allow the tests to attempt N number of times to retry the tests. You can configure the number of attempts that the test will retry in Cypress automation testing. For example, if test retries are set to 2 retry attempts in the configuration, Cypress will retry tests up to 2 additional times. So, the total number of attempts will be 3 before and then it will be marked as a failed test. Each time you run the test retry, the following hooks will also re-run in your Cypress automation as “beforeEach” and “afterEach”.
Test Retry Configuration
Now, let us see the various methods of how you can configure the test retry in Cypress automation. Using these configurations will help the developers and the QA team enhance test stability with retry strategies in Cypress automation.
Global Configuration
Generally, the developers define various retry attempts for “cypress run” and “cypress open”. To do this, you need to pass “retry” as an object in your Cypress automation configuration. Follow the steps to do this:
{
retries: {
// Configure retry attempts for cypress run
// Default is 0
runMode: 2,
// Configure retry attempts for cypress open
// Default is 0
openMode: 0
}
}
Here, “runMode” is used to allow you so you can define the desired number of retries while running the “cypress run”. The “openMode” is used to allow you so you can define the desired number of retries while running the “cypress open”.
Retry Attempts for All Modes
If you want to combine and configure the retry attempts for both cypress run and cypress open, you need to define the configurations as retry properties and settings according to your retry need. Use the below code to set the retry property:
{
retries: 1
}
Custom Configuration
There is another configuration type that you can use in your Cypress automation testing, known as custom configurations. You can use this configuration for defining retry attempts on a specific targeted test. Use the below command:
// Customize retry attempts for an individual test
describe(‘User sign-up and login’, () => {
// it test block with no custom configuration
it(‘should redirect unauthenticated user to sign-in page’, () => {
// …
})
// it test block with custom configuration
it(
‘allows user to login’,
{
retries: {
runMode: 2,
openMode: 1,
},
},
() => {
// …
}
)
})
Screenshots
This way to retry configuration is to use the screenshot concept. When test retry gets in action, it takes a screenshot of every failed test. For every failed test case, it will continue to take screenshots for each failed attempt or cy.screenshot() and suffix each new screenshot with (attempt n). This number will be according to the retry attempt number.
Retry Strategies in Cypress Automation
Now, let us see the best and most preferred strategies and practices that should be followed to enhance test stability with retry strategies in Cypress automation.
1. Keep the Test Atomic
One of the best practices to make your Cypress automation test stable using the test retry is to make your tests atomic. Making a long, complex, and extensive test case will pass through numerous parts of the web app. So, it becomes hard to write such long tests and hard to maintain and debug them. So, you can keep your Cypress automation test atomic or in smaller chunks to make it more stable and reliable. Keeping the test atomic has several benefits such as it becomes easy to understand for the testing team. Maintaining the test takes less effort.
2. Use parallelization
Parallelization in Cypress automation is a great and very effective practice for making the test stable. When you run multiple tests at the same time against one environment, what might happen is that one test might cause another to fail due to changing conditions. Using test parallelization surely helps to run tests faster, but there are some costs that you need to pay. These costs are in terms of resources. As we know, in parallel test execution, you run various tests simultaneously, so it uses more resources. So, use Cypress parallel testing, but be cautious while doing it.
3. Test Optimization
Test optimization is a very useful strategy that is defined as making an organized arrangement of your tests, as you can make a priority list according to the test execution time that will take to run.
Conclusion
In this article, you will see what test retry is in Cypress. This is used to provide some more attempts to the running tests so that it may be successful before declaring it as a failed test. Also, keep in mind that you should follow the best practices while performing Cypress automation tests so that the testing stability can be enhanced.