Automation suites are often used to reduce manual testing efforts or to obtain testing results in less time. However, inappropriate coding practices or scripting sometimes affects the script execution performance and ultimately increases the script execution time. Many factors contribute to the automation script execution performance. One of the most notorious ones is the " hard waits " in step definitions.
So what are Hard waits?
Hard waits are applied to automation scripts for elements being rendered on a page. It will try to find an element for a ‘defined’ time and if within this time if the element is not found, it will throw an error. If an element is found, then it will allow you to perform further actions on it.
Let’s discuss this in more detail.
Many times, I have come across these "hard waits" being applied in step definitions. Consider the example below:
- What if the user reaches the login page within 1 or 2 seconds? Test suites will be waiting for 10 seconds for nothing.
- What if you get a response in more than 10 seconds then the test suite fails, no matter if the cause of this is anything else let’s say slow network problem
Now if there are “n” number of scenarios then the “wait” seconds keep on getting multiplied with the number of scenarios and thus increasing the script execution time.
To resolve this issue, we can write a custom function to replace the step “And I wait for n seconds” as following:
In the above custom script, we are trying to find an element first using findElement() function with certain conditions applied in the loop. Here, the script will wait for an element to load and then perform an action on it. So we don’t need to add “And I wait for n seconds” and we don’t have to worry about how many seconds it will take for an element to load.
Now our step definition will finally look like this:
And here is how we call the “findElement” function for elements before we perform any action on the respective element.
The script will keep trying to find an element and there will be a limited number of tries. After this, if an element is not found, the test suite will fail.
Do try to avoid using “hard waits” in step definitions by using the above custom function and observe the time taken for script execution. These are a few tips on how to write precise and accurate step definitions that help improve script execution performance.
If you would like QED42's help in setting up or improving your automation suites please reach out to us!