Google Search

Saturday, October 26, 2013

Matching Patterns in Selenium

Patterns are one of the frequently used parameters by Selenese commands. Patterns allow you to describe what text you are looking for, by using special characters rather than mentioning the exact text. Some of the commands which use patterns very frequently are
·         verifyTextPresent
·         verifyTitle
·         verifyAlert
·         assertConfirmation
·         verifyPrompt
·         verifyText


There are three types of patterns:
1.       Globbing: Globbing is very limited, with only 2 special characters supported in Selenium. One of them is
a.       * Which means match anything, i.e. match nothing, single character or many characters.
b.      [] (character class) which means match any single character found inside the square brackets []. You can use hyphen to classify a range of continuous characters as long as they are continuous in ASCII range. Some of the examples are as below:
                                                               i.      [a-f]: match any character between ‘a’ and ‘f’ in lower case.
                                                             ii.      [1-9]: matches any number between 1 and 9.
                                                            iii.      [a-z0-9A-Z]: matches any alphanumeric character.

Example: Let us suppose there is a link on the page “Movie / Serial Schedule” which navigates to a new page with title “Welcome to Movie Schedule - ITV”. Glob pattern for that will be:

Command
Target
Value
click
Link=glob:Movie * Schedule

verifyTitle
glob:*Movie Schedule*



2.       Regular Expressions: Regular Expressions are the strongest of the patterns that Selenium supports. These allow many tasks that would otherwise be hard to perform. Some of the special characters that Selenium supports   

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

A Regular Expression in a command needs tro be prefixed by regexp: (case sensitive) or regexpi: (case insensitive.)
Let us have a look at some examples:
First is .* (dot star) which is equivalent to * in Globbing. “.*” is equivalent to nothing or anything.

Command
Target
Value
click
Link=regexp:Movie .* Schedule

verifyTitle
regexp:.*Movie Schedule.*

This example is equivalent of example we used in Globbing section.

3.       Exact Patterns: This type of pattern is of marginal usefulness. It does not use any special character. This comes handy, when you need to look for a real * in the pattern, exat pattern will be useful. For example, if you want to search for “Actual *”. If you use glob here it may look for Actual followed by anything or may be nothing. The below example may look for “Actual number” on the webpage as well.

Command
Target
Value
verifyTextPresent
glob:Actual *


In order to make sure that “Actual *” is looked for we need to use exact

Command
Target
Value
verifyTextPresent
exact:Actual *


Similar results can be obtained by using escape character in Regular Expression pattern.

Command
Target
Value
verifyTextPresent
regexp:Actual \*





No comments:

Post a Comment