Selenium Testing | Regular Expressions

Regular Expressions

Selenium Testing | Regular Expressions

Regular expression patterns are the most powerful of the three types of patterns that Selenese supports. Regular expressions are also supported by most high-level programming languages, many text editors, and a host of tools, including the Linux/Unix command-line utilities grep sed and awk. In Selenese, regular expression patterns allow a user to perform many tasks that would be very difficult otherwise. For example, suppose a test needed to ensure that a particular table cell contained nothing but a number. regexp: [0-9]+ is a simple pattern that will match a decimal number of any length.

Whereas Selenium globbing patterns support only the * and [ ] (character class) features, Selenese regular expression patterns offer the same wide array of special characters that exist in JavaScript.

Below are a subset of those special characters:

Pattern Match
. any single character
[ ] character class: any single character that appears inside the brackets
* quantifier: 0 or more of the preceding character (or group)
+ quantifier: 1 or more of the preceding character (or group)
? quantifier: 0 or 1 of the preceding character (or group)
{1,5} quantifier: 1 through 5 of the preceding character (or group)
| alternation: the character/group on the left or the character/group on the right
( ) grouping: often used with alternation and/or quantifier

Regular expression patterns in Selenese need to be prefixed with either regexp: or regexpi:. The former is case-sensitive; the latter is case-insensitive.

A few examples will help clarify how regular expression patterns can be used with Selenese commands. The first one uses what is probably the most commonly used regular expression pattern–.* (“dot star”). This two-character sequence can be translated as “0 or more occurrences of any character” or more simply, “anything or nothing.” It is the equivalent of the one-character globbing pattern * (a single asterisk).

Command Target Value
click link=regexp:Film.*Television Department  
verifyTitle regexp:.*Film.*Television.*  

 

The example above is functionally equivalent to the earlier example that used globbing patterns for this same test. The only differences are the prefix (regexp: instead of glob:) and the “anything or nothing” pattern (.* instead of just *).

The more complex example below tests that the Yahoo! Weather page for Anchorage, Alaska contains info on the sunrise time:

Command Target Value
open http://weather.yahoo.com/forecast/USAK0012.html  
verifyTextPresent regexp:Sunrise: *[0-9]{1,2}:[0-9]{2} [ap]m  

Let’s examine the regular expression above one part at a time:

Sunrise: * The string Sunrise: followed by 0 or more spaces
[0-9]{1,2} 1 or 2 digits (for the hour of the day)
: The character : (no special characters involved)
[0-9]{2} 2 digits (for the minutes) followed by a space
[ap]m “a” or “p” followed by “m” (am or pm)

Exact Patterns

The exact typeof Selenium pattern is of marginal usefulness. It uses no special characters at all. So, if you needed to look for an actual asterisk character (which is special for both globbing and regular expression patterns), the exact pattern would be one way to do that For example, if you wanted to select an item labeled “Real *” from a dropdown, the following code might work or it might not. The asterisk in the glob: Real * pattern will match anything or nothing. So, if there was an earlier select option labeled “Real Numbers,” it would be the option selected rather than the “Real *” option.

select //select glob:Real *

In order to ensure that the “Real *” item would be selected, the exact: prefix could be used to create an exact pattern as shown below:

select //select exact:Real *

But the same effect could be achieved via escaping the asterisk in a regular expression pattern:

select //select regexp:Real \*

It’s rather unlikely that most testers will ever need to look for an asterisk or a set of square brackets with characters inside them (the character class for globbing patterns). Thus, globbing patterns and regular expression patterns are sufficient for the vast majority of us.

This module covers the following topics:

Selenium professional free practice test

Go back to Tutorial                                                                                Go to Home Page

Selenium IDE | Locating by DOM & CSS | Locating Hyperlinks by Link Texts
Selenium testing | Matching Text Patterns

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?