Learning Resources
 

Regular Expression


Of the three type of patterns, Regular Expressions are the one that are the most useful. Selenium supports the complete set of RegEx patterns that Javascript supports. So now you are not limited by the *,? And [] globbing patterns. To use RegEx patterns you need to prefix each RegEx with either regexp: or regexpi:, the latter being case-insensitive.

For example the following will test if a input field with the id ‘name’ contains the string ‘javascript’, ‘JavaScript’ or ‘Javascript’.

Command Target Value
clickAndWait link=search  
verifyValue id=name regexp:[Jj]ava([Ss]cript)

Below are a few common regular expression patterns:

regexp:(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
:match a date in ‘mm/dd/yyyy’ format with any of the ‘-’, ‘/’, ‘.’ as separators.

regexpi:^[A-Z0-9+_.-]+@[A-Z0-9.-]+$
:match a generic email address.

regexp:^[0-9]{5}(?:-[0-9]{4})?$
:match a ZIP code (U.S. postal code), allowing both the five-digit and nine-digit (ZIP + 4) formats.

regexp:^(?!000|666)(?:[0-6][0-9]{2}|7(?:[0-6][0-9]|7[0-2]))-?(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
:match U.S Social Security numbers in the in the AAA-GG-SSSS format.

regexp:^(https?|ftp|file)://.+$
:match almost any url.