Cypress.io

8 Tips To Write Automation Tests Like A Ninja

The most important thing in write automation tests is creating scalability and stability tests much as you can. Best practices are more than recommended to implement in your tests and better to implement them already yesterday, so what are the 8 Tips To Write Automation Tests Like A Ninja?

1. Stop Using The Wait Command

By using the wait command is like using a bandage, cy.wait() can seem like an elegant and easy solution but it actually hides the real problem of the application – Performance!
If we are using the wait command because the page load is too long so we have a performance issue and we need to fix that instead of adding more waiting time to the test.

Let’s say you using cy.wait() of 5 seconds twice in a test, and you have 50 tests in overall, so you just add 4 min more in the overall test run.

2. Start Using Attributes

Websites are using HTML to building the front-end side of the web, HTML also using Classes and Ids.
This is the easy and fast way to build automation, you can just get() the class name or the ID class and it will work as accepted.

But this is not the right way, let me explain – most of the time there is more than one front-end developer who works on the web, and not all of them are aware that automation relies on this specific element.

My point is – Classes and Ids are not stable they can be changed often and can crash the tests.
This is why using unique attributes such as data-testid=”testAttribute” is much more reliable.

3. Don’t Rely On Strings

One of the weaknesses in write automation tests is relying on Strings.
It is an easy way to validate data and cypress also giving the option to use
should(‘contain’, ‘Some String’) assertion.

This can work when your application is only in one language.
If the test will run in multiple languages then this is a bad practice and the test will fail.

4. Each Test Have To Be Individual

Tests must be individual and not relying on the previous test.
If we will rely on the previous test and he will fail then all the tests run will fail.

The expected result will be that if one test of 6 tests we have will fail, it will continue testing all the 5 other tests.

5. Use the POM power

Page Object Model (POM) is a great strategy to write automation code.
It will help you be much more productive with your code, POM is basically a class that can be imported to other tests.

It will reduce duplicate code and reduce hard and manual work when we need to change some code that appears in a lot of our tests.
To learn how to implement this you can read more here – How To Use Page Object Model In Cypress

6. BeforEach()

The beforeEach() means the code will be inside that function will run before other code in your test, this is a great practice to use it when we know we want to run something before the tests. for example – clear cookies, login and etc…

7. Use The Parallelization Strategy

Parallelization is a super important strategy for automation tests.
It gives you the option to run tests in parallel and not series, it will reduce the run time significantly.

For example, you have 10 tests with an overall run time of 12 min.
When using parallel means it will split the tests into 2 or 3 other machines in your CI and will run them parallel, then the overall run time will reduce to 6 min and even less. You save 6 min run time, 50% reduce the run time this is a lot!

8. Add Retries

Tests are failing not just when a bug appears they also fail because of Timeouts errors,
also known as “flaky tests”.
To reduce the times that tests are failed on timeouts issues, I recommend adding retries to the tests.

It means that if we set the retries option in our cypress.json file it will retry and will run the tests over and over again based on the number of the retries you set when the tests are failed.
If timeouts appear in the first retry attempt is not necessarily mean that the timeouts will appear in the second attempt.

Summary

  • Try not to use the wait commnd is just hiding the performance issues.
  • Attributes is super important to keep your tests stability.
  • Is not good to rely on strings if your app have multiple languages support.
  • Test must be individual and not rely on previous tests.
  • Use the Page Object Model class to reduce duplicated code.
  • The BeforEach() will help you run specific code before starting the tests.
  • I’m supper recommand to start using the Parallezation Strategy it will save you lot of run time.
  • Use the Retries option to reduce TimeOuts erros.

Hope you enjoy reading my 8 Tips To Write Automation Tests, let me know what you think in the comment below.

Ofir Blumental

QA Automation Engineer - ofirblumental1@gmail.com

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button