Case Study: Count the number of “shakes” within “Shake it Off”’s lyrics on a webpage.
Using Ruby’s scan and some regex for text counting.
The lyrics of “Shake it Off” are on a web page.
The task is to write a Selenium WebDriver test script to perform an assertion: find out the occurrences of word “shake”.
Test Design
Navigate to the target web page
Extract the lyric text
Count of the word “shake”
There is no string counting function in Selenium WebDriver. So, we’ll use Selenium WebDriver to get the text (from the web page), then use Ruby’s text manipulation functions to do the counting.
Test Steps
I will write this test script in raw Selenium WebDriver + RSpec, using TestWise IDE.
1. Navigate to the target web page
driver.get("https://agileway.com.au/demo/shake-it-off")
2. Extract the lyric text
Using driver.page_source
is not correct, as there might be the word “shake” somewhere in a hidden division. As a generic rule, we try to be as specific as possible.
Getting the text content of the <pre>
here is better.
Some might think, “but there are no unique identifier or attributes?”. We can use the tag name.
lyrics_text = driver.find_element(:tag_name, "pre").text
3. Count of the word “shake” using Ruby’s “scan” function
Ruby is a powerful text manipulation language with intuitive syntax.
Ruby’s scan
method returns a list of all the matches of a provided string or regex.
lyrics_text.scan("shake") #=> ["shake", "shake", ...]
Since we now have the list of matches, we can now use the count
function to see how many “shake”s there are.
num_shakes = lyrics_text.scan("shake").count #=> 63
Finally, to round off the test, let’s do an assertion:
expect(num_shakes).to eq(63)
Handle Complex Scenarios with Regular Expression
Now, let’s extend the original task for further scenarios:
4. Count the case-insensitive string “shake”
The i
in below regex means case-insensitive.
num_shakes_insensitive = lyrics_text.scan(/shake/i).count
expect(num_shakes).to eq(70)
5. Count the string “Shake it off”
expect(lyrics_text.scan("Shake it off").count).to eq(6)
6. Count the string “Shake it off” case-insensitive
“Shake it off”, “shake it Off”, or “shake it off” are all OK. \s
mean a white space character.
expect(lyrics_text.scan(/Shake\sit\soff/i).count).to eq(36)
7. Count the whole word “to”
Please note that this exercise matches the word “to”, not the string. There are several words in the lyrics containing the string “to”, such “too”, “stop” that we want to exclude. For this, use \b
for word-boundaries.
expect(lyrics_text.scan(/\bto\b/).count).to eq(4)
For more info about using Regular Expression in test automation, check out this article, Use Regular Expressions in Test Automation.
Related reading:
Selenium WebDriver Recipes in Ruby
You many find more solutions likes this in this book.Selenium Training Workbook, a series of guided E2E web testing exercises.
Practical guided test automation exercises, a series of bite-sized web test automation training with Selenium WebDriver. No previous knowledge and experience is required.Why Ruby is the Best Scripting Language for End-to-End Test Automation?