Patterns are bread and butter of Selenium tests. Many commands in Selenium take the pattern parameter. They enable you to match various content types on a web page – links, text, elements. There are chiefly three type of patterns you can use in your tests: globs, exact and regular expressions.
Globs or the verb Globbing is familiar to most people who have ever used file matching patterns. If you have ever searched for a file in Linux or DOS using a line like *.exe or photos*, then you have used globs. But globbing in Selenium is not as rich as the one in Linux – it supports only three special characters: *, ? and [ ].
* matches any number of characters, by any we mean ‘nothing’, ‘a single character’ or ‘many characters’.
? , matches a single character.
[ ] , called a character class, lets you match any single character found within the brackets. e.g
[0-9] matches any digit
[a-zA-Z] matches any alphabet, regardless of case
To specify a glob in a selenium command, prefix the pattern with the glob: string. For example if you would like to search for the texts color or colour then you can use the colo*r glob as shown below.
However you are free to eliminate the glob: prefix and only specify the text pattern because globbing patterns are the default in Selenium.
Regular Expression patterns
Below are a few common regular expression patterns:
regexp:(0[1-9]|1)[- /.](0[1-9]|[0-9]|3)[- /.](19|20)\d\d
:match a date in ‘mm/dd/yyyy’ format with any of the ‘-‘, ‘/’, ‘.’ as separators.
:match a generic email address.
:match a ZIP code (U.S. postal code), allowing both the five-digit and nine-digit (ZIP + 4) formats.
:match U.S Social Security numbers in the in the AAA-GG-SSSS format.
:match almost any url.
Patterns with the prefix ‘exact:’ will match the given text as it is. For example if you give the search pattern as below, then it will match a glob pattern ‘*’ or ‘*.java’.
But if you want an exact match with the value string, i.e without the glob operator doing its work, you use the ‘exact’ pattern as below. In this example the ‘*’ (asterisk) will work as a normal character rather then a pattern-matching wildcard character.
In conclusion the glob: and the exact: pattern are the subsets of the Regular Expression pattern matcher. Everything you can do with glob: or exact: you can accomplish with RegExp.