Use Regular Expressions in Test Automation
Extracting dynamic text is made a lot easier with regular expressions
This article is included in my “How to in Selenium WebDriver” series.
Regular Expressions are patterns used to match character combinations in strings. To put it simply in the context of test automation, I use regular expressions to find or extract dynamic text in strings. In this article, I will illustrate with a couple of examples.
Regular expression originated in 1951, is widely used in Unix programs. Most programming languages, if not all, have built-in support for Regular expression, a medium-level programmer should be comfortable with it. However, few test engineers I have met knew about Regular Expression.
Let me share a funny story first. After I explained my test scripts to Nick, a newly hired senior tester (in a government project many years ago), Nick said this to me: “Oh, Regular Expression, I know it. It is specific to Ruby.😱 ” This ‘senior automated tester’ claimed had developed over 10,000 IBM RFT automated tests; of course, he did not, as I never saw him writing a single automated test for 6+ months (until I left the project). In fact, he had been sabotaging test automation. I often use this real story to warn companies to be aware of fake test automation engineers.
I will use Ruby in the following two practices, while the regular expression will work for all mainstream languages. Ruby’s String has built-in support for Regex to extract pattern text in a simpler way.
Table of Contents· Extract dynamic text from web page
· Scan pattern text
· Quick Regex syntax
· Recommend Regex Tool: Rubular
Extract dynamic text from web page
After executing a typical payment test, we often need to save the receipt number for later use. For example, in the case below, the number (in this case, called the booking number) is 604.
If the developer has a testing mindset, he would have wrapped the booking number in a specific tag, such as