“Give me six hours to chop down a tree and I will spend the first four sharpening the axe.”
A similar analogy can be derived from the above quote when it comes to using a test automation tool. Apart from understanding the basics of a tool we also need to work on understanding the best practices of using the tool which in turn can help us save time and increase our productivity.
A combination of the use of best practices and avoiding mistakes can help a tester in mastering the tool. This is why we at Testrig Technologies have got you this blog which throws some light on efficient ways to use the Cypress tool.
The blog will also help you avoid those pitfalls which can delay your test execution.
So let’s dive in to know more about it.
Tips To Improve Cypress Testing
1. Set a baseUrl:
Setting up a baseUrl will help you save a lot of time while executing Cypress scripts. Once the baseUrl is added Cypress will attempt to prefix the baseUrl provided to commands like cy.request(), cy.visit().
For instance cy.visit(‘http://localhost:8080/abc.html’ ) can be shortened to cy.visit(‘/abc.html’).
To add the baseUrl in Cypress we need to add the baseUrl in cypress.config.js file as shown in the image below.
2. Use aliases in cy.wait()
The cy.wait() command provides two options for the wait, i.e. one to wait for some number of seconds or for an aliased element prior to moving to the following step.
The usage is as follows:
cy.wait(1000) to wait for 1000 milliseconds or as shown in the image below
It must be noted that if we use the commands
This will add a waiting period of 1000 milliseconds after cy.request() is executed. This causes an unnecessary delay and so we can use the alternate option of waiting explicitly for an aliased route as shown in the above image.
3. Avoid visiting external sites while test execution
Involving a third-party server slows down your tests and is incredibly time-consuming. This is why you must avoid visiting external sites as such sites may be having issues outside of your control. For situations where it is necessary to visit such sites, we should always use cy.request() to talk to such servers via their APIs.
4. Avoid coupling of tests
We must always see to it that the Cypress tests are not dependent on each other and are running independently. If tests are dependent on each other it might happen that if one test fails it impacts the other tests. This might result in delayed execution of tests.
5. Using hooks the right way
Cypress Testing provides us with different hooks like before(), after(), beforeEach() and afterEach() hook. While the before() hook is executed once the prior execution of any tests within a ‘describe’ block, the after() hook is executed once the post-execution of all the tests in a describe block. Similarly, in the beforeEach() hook the execution takes place prior to the execution of the individual ‘it’ blocks in a ‘describe’ block, and in the afterEach() hook the execution occurs post the execution of the individual ‘it’ blocks in a describe block.
Now in a case where we have a spec file with 5 test cases and we need to write 2-3 lines of code that is common to all test cases. So in such a case as a good practice, we should use the beforeEach() hook to write the code.
6. Incorporate Cypress into your Continuous Integration and cloud testing plan
In order to increase the speed and scalability of test automation, the Cypress automation tool provides integration with various cloud testing platforms like BrowserStack and LambdaTest. Similarly, for continuous integration testing, Cypress can be integrated with tools like Gitlab, CircleCI, AWS CodeBuild, and various others. Once these tools are integrated with Cypress, the Cypress tests can be run from anywhere and anytime.
Important Read: The Complete Guide to Cypress : How to Setup and Test?
The above tips need to be incorporated into your Cypress test automation strategy to reap the benefits of the tool. While some of these tips may take time to implement, they would certainly be worth the effort.
At Testrig Technologies, our QA expert engineers follow such best practices so as to save valuable time for our customers. We thrive on innovation and our customers have always appreciated our contribution. If you are looking for end-to end Cypress Testing, we will be more than happy to assist you on your test automation journey. As an established Automation Testing Company, we are dedicated to providing QA Automation assistance and knowledge.