Wednesday, October 29, 2014

Chapter 8- Selenium - IDE Pattern Matching 9922500360

Selenium - IDE Pattern Matching

In Selenium IDE, like locators, patterns are a type of parameter which are frequently used by selenium. It allows user to describe patterns with the help of special characters. Many a times, the text that we would like to verify are dynamic in that case pattern matching is very useful.
Pattern Matching is used with all verification points commands - VerifyTextPresent, verifyTitle, verifyAlert, assertConfirmation, verifyText, and verifyPrompt
There are three ways to define a pattern - globbing, regular expressions, and exact.

GLOBBING

Globbing is familiar to most techies who have used file matching patterns in Linux or Windows while searching for a certain file type like *.doc or *.jpg. But globbing in Selenium supports only three special characters: *, ? and [ ].
  • - matches any number of characters.
  • - matches a single character.
  • [ ] - called a character class, lets you match any single character found within the brackets. [0-9] matches any digit
To specify a glob in a selenium command, prefix the pattern with the keyword 'glob:'. For example if you would like to search for the texts "tax year 2013" or "tax year 2014" then you can use "tax year *" glob as shown below.
However the usage of keyword "glob:" is optional while specifies the text pattern because globbing patterns are the default in Selenium.
CommandTargetValue
clickAndWaitlink=search
verifyTextPresentglob: tax year *

EXACT PATTERNS

Patterns with the prefix 'exact:' will match the given text as it is. Let us say, the user wants an exact match with the value string, i.e without the glob operator doing its work, one can use the 'exact' pattern as below. In this example the operator '*' will work as a normal character rather then a pattern-matching wildcard character.
CommandTargetValue
clickAndWaitlink=search
verifyValueexact: *.doc

REGULAR EXPRESSION PATTERNS

Regular Expressions are the most useful among the pattern matching techniques available. Selenium supports the complete set of reugular expression patterns that Javascript supports. Hence the users are No longer limited by *, ? And [] globbing patterns.
To use RegEx patterns we need to prefix with either "regexp:" or "regexpi:". The prefix "regexpi" is case-insensitive. The glob: and the exact: pattern are the subsets of the Regular Expression patterns. Everything that is done with glob: or exact: can be accomplished with the help of RegExp.

EXAMPLE

For example the following will test if a input field with the id 'name' contains the string 'tax year', 'Tax Year' or 'tax Year'.
CommandTargetValue
clickAndWaitlink=search
verifyValueid=nameregexp:[Tt]ax ([Yy]ear)

No comments: