Optimize Selenium WebDriver Automated Test Scripts: Speed

Simple techniques to improve the execution speed of some automated test steps, up to 50X.

Zhimin Zhan
Geek Culture
Published in
5 min readJun 8, 2021

--

Updated 2023–12–12: a new recipe from my daughter, Use Chained Locator to Speed Up Selenium WebDriver Tests.

Working automated test scripts is only the first test step to successful test automation. As automated tests are executed often and the application changes frequently too, it is important that test scripts need to be

Fast
Easy to maintain
Easy to read

In this article, I will show some techniques to optimize test scripts for speed.

The test script examples are in Ruby syntax, the techniques are applicable to other languages as well.

1. Assert text in page_source is faster than the text

To verify a piece of text on a web page, frequently for assertions, we can use

driver.page_sourceor driver.find_element(:tag_name => ‘body') .

Besides the obvious different output, there are big performance differences as well. To get a text view (for a whole page or a web control), Webdriver needs to ‘analyze’ the raw HTML to generate the text view, which takes time. We do not usually notice the time spent when the raw HTML is small. However, for a large web page such as the WebDriver Standard (over 430KB in file size), incorrect use of ‘text view’ will slow your test execution significantly.

Page Text:

expect(driver.find_element(:tag_name => "body").text).to include("platform- and language-neutral wire protocol")

Page Source:

expect(driver.page_source).to include("platform- and language-neutral wire protocol")

Let’s see the difference.

Method 1: Search whole document text took 0.823076 seconds 
Method 2: Search whole document HTML took 0.039573 seconds

Though there are functional differences between page_source and page_text too, here I will only focus on the performance differences.

--

--

Zhimin Zhan
Geek Culture

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.