Best Practices for enhancing speed and stability of automated UI tests
When it comes to UI automated tests, we would have faced the issues with test execution speed and stability of UI automated tests.
Once the execution is complete, we end up revalidating flaky tests manually or using breakpoints…
There are few approaches with which the flakiness of the tests and speed of the test execution could be optimised.
#Utilise the APIs:
In the modern era most of the web/apps have APIs as their skeletal system.
- Data creation, data clean up or the pre-requisites for the test can be handled via APIs. This would help in better test data management and lesser maintenance in terms of test data.
- Have a test suite with API inits for test data creation and avoid them as part of functional tests, if it is the prerequisite for your tests.
- Maintain separate CRUD and extensive functional test suites. (Ensuring to cover the UI automation validation for the prerequisites.)
Use case : If a test expects payment via a wallet
- Since the idea is to test the wallet payment screens,
- Pre - requisite for the test: Make sure to have some amount in the wallet, - - This could be achieved via API, rather than UI top up flow.
- Proceed the UI flow for wallet payments
#Incubate the flaky tests until fixed:
- Running flaky tests is equivalent to no testing of the feature.
- Identify the flaky tests, if the scripts keep failing due to automation error, it's no longer testing the functionality.
- Incubate these tests.
→ Find the flake → Fix it → Re - run → Move to the pool of regular running tests
#Usage of conditional waits : Automate → Re-run → Repeat:
- Say no to halt program execution, or freeze the thread without any conditions, this would make your tests extremely slow and could be the culprits for flakiness of the tests.
- Try to eliminate wait statements within your tests, replace them with conditional waits rather than hard coded wait period.
**we can look out for various other conditions as well: listed here
#Use the navigational shortcuts:
- An end user could type the url and hit on certain services to navigate to a specific service provided by the application.
- To achieve the above said case, your tests can directly navigate to the Url to be tested, using driver navigate functions.
#Modularise the tests:
- Use the power of APIs to handle certain actions which is already covered in few cases.
Scenario A : Verify user is able to create an order successfully
Scenario B : Verify if user is able to view the order details in the order history screen
Here the order creation can be a prerequisite, covered via API call to create an order, rather than redoing the whole flow via UI.
- Break your tests smartly, achieve the test goals as use case.
- Have independent test as much as possible to avoid huge amount of test failures, due to a single flaky test.
- Do not club extensive checks in a single test, just to avoid writing a new test. This would deteriorate the readability as well as maintenance of the tests.
# Optimise the locators for DOM elements:
- Trivia question : easiest locator type to break - xpaths (A small ui change can affect huge amount of tests).
- Xpaths are slower while compared with other locator types(css, id, name)
#Fix the unoptimised code:
- Looping through data fetching results can be optimised, using right set of algorithms and data structures.
- Revisit the code, find out anomalies, get it fixed.
#Efficiently utilise automated test
- Determine the tests to be automated efficiently.
- Categorise based on necessity of integration - end to end flows.
- All tests may not be UI Automated tests.
- Leverage the unit tests.
Use case : User should be able create orders via all the available payment methods
- Extensive non UI flows for forms of payment can be covered in API and Unit tests
- While UI can cover the majority of UI validation chunks
- Leverage CI-CD, make automation as part of the deployment builds
- Analyse the failures early.
- Change is the only constant, hence agile.
UI changes can happen frequently
Collaborate with the team to define better locators way before its deployed on UI. Use them in automation.
Have less maintenance efforts. Make it robust.
#Fail fast mechanism
- Skip the tests at initial stage, fail fast rather than waiting for longer time to determine errors.
- Look for faster feedback loops rather than longer execution time.
For instance: if the user can’t sign in, then there is no need to test other features that an authenticated user can do because those will eventually fail as well.
# Headless Testing
- Trigger the test cases in headless mode -> Headless testing still tests the components, but skips the time- and resource-consuming process of rendering a visual display.
- Headless tests can typically execute much faster than (Non Headless mode) browser tests.
- As the displaying an application visually is skipped it also reduces the amount of resources that each test consumes. As a result, you can run more tests at once on the same infrastructure.
#Parallelise automated tests execution
- There are various approaches to achieve parallel runs :
- Configure selenium grid, sauce labs to have tests run in parallel
- Or you could choose to set up Jenkins nodes to handle parallel runs via testNG
- Segregate dependent tests to be run sequentially, while other non dependents can be run in parallel.
Getting started with best practices could help in amplifying the speed and stability of automated tests to be a part of your DNA while automating. Adapting to such strategies could help avoid common mistakes and easy upscaling of the tests.
Aforesaid approaches can be either as part of the framework building strategy or as part of the enhancement, and by no means these are exhaustive. If it can bring the desired capabilities, then definitely its worth a try!
These focused plan of actions and pragmatic insights can be beneficial while executing & automating the tests.
Scalability, reliability and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resumé at firstname.lastname@example.org.
Halodoc is the number 1 all around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek and many more. We recently closed our Series C round and In total have raised around USD$180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.