Selenium IDE: Pattern matching

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

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.

Command Target Value
clickAndWait link=search  
verifyTextPresent glob: colo*r  

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

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.

Exact Patterns

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’.

Command Target Value
clickAndWait link=search  
verifyValue glob: *.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.

Command Target Value
clickAndWait link=search  
verifyValue exact: *.java  

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.

17 thoughts on “Selenium IDE: Pattern matching

  1. Hi,

    i want to check email address 10 types of negative case one by one but in one line command use i want test all type of validation, have any command for this ? in selenium IDE ?

  2. Hi,
    I need to check that signature states “some text 2011”, where 2011 – is the current year according to current computer clock prefference
    how check it in selenium IDE ?

  3. The value 223 is changing every time. Please help me how to do regular expression for this line. My environment is Java

    selenium.click(“//[@id=’gwt-uid-223′]”);

  4. This is not recognize. It says invalid xpath

    selenium.click(“//[@id=’gwt-uid-[0-9]+’]”);

    selenium.click(“//[@id=’gwt-uid-[0-9]’]”);

  5. My xpath will be like this

    /html/body/div[@id=’tn-PopupPanel-default-panel’]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr/td/div/table/tbody/tr[6]/td[2]/span[@id=’tn-RateTierTariff-PopupPanel-field-cppTier’]/input[@id=’gwt-uid-223′]

  6. /html/body/div[@id=’tn-PopupPanel-default-panel’]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr/td/div/table/tbody/tr[6]/td[2]/span[@id=’tn-RateTierTariff-PopupPanel-field-cppTier’]/input[@id=’gwt-uid-223′]

  7. regexp:(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

    could you please explain in detail – each of them

    Thanks in advance

  8. hey how can i make a value stored in a string to be considered as case insensitive for selenium. i.e if i have india as name of country but drop down has India, and india is stored in a variable and i am passing the variable to selenium webdriver. how can i make that variable case insensitive for selenium..? thanks for the help.

  9. Hello, I need to verify that a list of words on a page is in alphabetical order, how do I do this? Sorry, completely new to coding/Selenium…

Comments are closed.