HomeServiceContact
Quality Engineering
min read
June 28, 2022
September 2, 2020

Improve performance and avoid false negatives by getting rid of hard-coded waits!

Improve performance and avoid false negatives by getting rid of hard-coded waits!
Table of contents

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:


Given I am on the login page
And I wait 10 seconds
And I login with "pooja@test.com" username and "p@ssw0rd" password
And I wait 10 seconds
And I wait for the page to load
  1. What if the user reaches the login page within 1 or 2 seconds? Test suites will be waiting for 10 seconds for nothing.
  2. 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:


/**

* Wait for a specified time until an element is found.
*
* @param $selector -  Selectors like css path, xpath path, label etc.
* @param string $type - Type of Mink Selector css, xpath, named etc.
* @param int $wait - Max wait time.
* @return NodeElement|null

*/

public function findElement($selector, $type = 'css', $wait = 15) {
 // Wait until max timeout.
 while ($wait > 0) {
   // Check for Element.
   $element = $this->getSession()->getPage()->find($type, $selector);
   if (!is_null($element)) {
     return $element;
   }
   else {
     // Wait for 1 sec and continue.
     sleep(1);
     $wait--;
   }
 }
 throw new \Exception("Time Out: Failed to find element '{$selector}' on current page.");
}

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:


Given I am on the login page
And I login with "pooja@test.com" username and "p@ssw0rd" password

And here is how we call the “findElement” function for elements before we perform any action on the respective element.


/**

*
* Context to login user with specific username and password.
*
* @param $username - username to login
* @param $password - password for given username
*
* @Given /^I login with "([^"]*)" username and "([^"]*)" password$/
*/

public function iLoginWithUsernameAndPassword($username, $password)
{
 $username_field = $this->findElement('#username');
 $password_field = $this->findElement('#password');
 $username_field->setValue($username);
 $password_field->setValue($password);
 $this->findElement('#loginbutton')->click();
}


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

Happy Testing!

Written by
Artwork by
No art workers.
We'd love to talk about your business objectives