My Innovative Solution to Test Automation: Easy Wait in Web Automation Scripts
An intuitive and proven solution that utilizes a feature in Ruby, a great scripting language.
A repost of my past article published on Medium in 2024
This is included in the “My Innovative Solution to Test Automation and Continuous Testing” series.
I started test automation in 2005 (see My Test Automation Journey), at that time, web pages were quite static. There was no or little concept of “waiting” in the context of test automation. We wrote web automation scripts step by step like below (in Watir).
browser.button(:value,"Pay now").click
# this might take a while
expect(browser.text).to include("RN#")
Then, Web 2.0 came. AJAX and the use of JavaScript make the web page much more dynamic. This introduces challenges to test automation.
Table Of Contents:
· Selenium WebDriver Waits Works for AJAX
· I also made the mistake of “Auto-Waiting”
· My Simple Solution
· Instant Success
· Other Uses
Selenium WebDriver Waits Work for AJAX
The above test script will fail (the assertion) for the below payment page using AJAX. After clicking the ‘Pay now’ button, a loading image (animated GIF, indicating an AJAX operation) shows up. A few seconds later, a receipt number is shown.
So, waiting is required.
The solution has been always there. When I first encountered AJAX testing, I already switched Watir to Selenium Ruby as I like the raw Selenium WebDriver syntax better, as it is consistent and intuitive. Especially when I trained manual testers (with no programming/automation experience), they mastered raw Selenium syntax quickly.
Below is a typical Selenium WebDriver script to test AJAX.
driver.find_element(:xpath,"//input[@value='Pay now']").click
wait = Selenium::WebDriver::Wait.new(:timeout => 9)
wait.until {
driver.find_element(id: "rn").text).to include("RN#")
}
The term is “Explicit Wait”. Some readers would think there must be another way, “Implicit Wait”. Yes, but you don’t need to know that. In fact, IMO, it is a minor mistake to include “Implicit Wait”. Some people used this to attack Selenium (for commercial reasons).
The Selenium Wait syntax is not bad at all, it works well, still quite readable.
However, it breaks my test script writing style, which is “One line test statement for one user operation step”. This syntax introduced an extra line (excluding braces).
I also made the mistake of “Auto-Waiting”
You might have heard of “Auto-Waiting,” a feature highlighted by some so-called new automation frameworks, such as Cypress. Other frameworks, such as TestCafe, made a similar claim. However, this approach does not work well (as people thought) and can introduce complexity.
I made the same kind of mistake over ten years ago. For more, check out my other article, “Auto-Waiting in Web Test Automation Clarified”. Anyway, I quickly realized and corrected it with a good solution that I have been using for over 15 years.
My Simple Solution
Keep reading with a 7-day free trial
Subscribe to The Agile Way to keep reading this post and get 7 days of free access to the full post archives.