Like locators, patterns are a type of parameter frequently required by Selenese commands. Examples
of commands which require patterns are verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation,
verifyText, and verifyPrompt.Patterns allow one to describe, via the use of special characters, what text is expected rather
than having to specify that text exactly
Global Patterns
* which translates to “match anything,” i.e., nothing, a single character, or many characters.
[ ] (character class) which translates to “match any single character found inside the square
brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters
(which are contiguous in the ASCII character set). A few examples will make the functionality
of a character class clear:
[aeiou] matches any lowercase vowel
[0-9] matches any digit
[a-zA-Z0-9] matches any alphanumeric character
In most other contexts, globbing includes a third special character, the ?. However, Selenium globbing
patterns only support the asterisk and character class.
To specify a globbing pattern parameter for a Selenese command, one can prefix the pattern with a glob:
label. However, because globbing patterns are the default, one can also omit the label and specify just
the pattern itself.
Below is an example of two commands that use globbing patterns. The actual link text onthe page
being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click
command will work even if the link text is changed to “Film & Television Department” or “Film and
Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word
“Film” and the word “Television”.
click link=glob:Film*Television Department
verifyTitle glob:*Film*Television*
The actual title of the page reached by clicking on the link was “De Anza Film And Television Department
- Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as
the two words “Film” and “Television” appear (in that order) anywhere in the page’s title. For example,
if the page’s owner should shorten the title to just “Film & Television Department,” the test would still
pass. Using a pattern for both a link and a simple test that the link worked (such as the verifyTitle
above does) can greatly reduce the maintenance for such test cases.
Some examples for global pattern
Lets say your element looks like
<input class="foo" value="the text value" name="theText">
verifyEquals("*text*", selenium.getValue("theText"));
<input type="hidden" value="the hidden value" name="theHidden">
verifyEquals("* hidden value", selenium.getValue("theHidden"));
If you have drop down combo box then you can use global pattern matching .
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
select the second option and verify like this
assertEquals("second *", selenium.getSelectedLabel("theSelect"));
or,
String[] arr = {"first*", "second*", "third*"};
verifyEquals(arr, selenium.getSelectOptions("theSelect"));
The question mark ? indicates there is zero or one of the preceding element. For example, colo?r matches both "color" and "colour".
If your element is like
<input class="foo" value="the text value" name="theText">
verifyEquals("?oo", selenium.getAttribute("theText@class"));
Regular expression
Regular expression patterns are the most powerful of the three types of patterns that Selenese supports.
Regular expressions are also supported by most high-level programming languages, manytext editors and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.
regexp (wildcard) matching
<input class="foo" value="the text value" name="theText">
verifyEquals("regexp:^[a-z ]+$", selenium.getValue("theText"));
<input type="hidden" value="the hidden value" name="theHidden">
verifyEquals("regexp:dd", selenium.getValue("theHidden"));
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
assertEquals("regexp:second .*", selenium.getSelectedLabel("theSelect"));
verify Attributes using regular expression
<input class="foo" value="the text value" name="theText">
verifyEquals("regexp:^f", selenium.getAttribute("theText@class"));
verifyEquals("regex:^[a-z ]+$", selenium.getValue("theText"));
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
assertEquals("exact:second option", selenium.getSelectedLabel("theSelect"));
String[] arr = {"regexp:^first.*?", "second option", "third*"};
verifyEquals(arr, selenium.getSelectOptions("theSelect"));
of commands which require patterns are verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation,
verifyText, and verifyPrompt.Patterns allow one to describe, via the use of special characters, what text is expected rather
than having to specify that text exactly
* which translates to “match anything,” i.e., nothing, a single character, or many characters.
[ ] (character class) which translates to “match any single character found inside the square
brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters
(which are contiguous in the ASCII character set). A few examples will make the functionality
of a character class clear:
[aeiou] matches any lowercase vowel
[0-9] matches any digit
[a-zA-Z0-9] matches any alphanumeric character
In most other contexts, globbing includes a third special character, the ?. However, Selenium globbing
patterns only support the asterisk and character class.
To specify a globbing pattern parameter for a Selenese command, one can prefix the pattern with a glob:
label. However, because globbing patterns are the default, one can also omit the label and specify just
the pattern itself.
Below is an example of two commands that use globbing patterns. The actual link text onthe page
being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click
command will work even if the link text is changed to “Film & Television Department” or “Film and
Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word
“Film” and the word “Television”.
click link=glob:Film*Television Department
verifyTitle glob:*Film*Television*
The actual title of the page reached by clicking on the link was “De Anza Film And Television Department
- Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as
the two words “Film” and “Television” appear (in that order) anywhere in the page’s title. For example,
if the page’s owner should shorten the title to just “Film & Television Department,” the test would still
pass. Using a pattern for both a link and a simple test that the link worked (such as the verifyTitle
above does) can greatly reduce the maintenance for such test cases.
Some examples for global pattern
Lets say your element looks like
<input class="foo" value="the text value" name="theText">
verifyEquals("*text*", selenium.getValue("theText"));
<input type="hidden" value="the hidden value" name="theHidden">
verifyEquals("* hidden value", selenium.getValue("theHidden"));
If you have drop down combo box then you can use global pattern matching .
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
select the second option and verify like this
assertEquals("second *", selenium.getSelectedLabel("theSelect"));
or,
String[] arr = {"first*", "second*", "third*"};
verifyEquals(arr, selenium.getSelectOptions("theSelect"));
If your element is like
<input class="foo" value="the text value" name="theText">
verifyEquals("?oo", selenium.getAttribute("theText@class"));
Regular expression patterns are the most powerful of the three types of patterns that Selenese supports.
Regular expressions are also supported by most high-level programming languages, manytext editors and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.
regexp (wildcard) matching
<input class="foo" value="the text value" name="theText">
verifyEquals("regexp:^[a-z ]+$", selenium.getValue("theText"));
<input type="hidden" value="the hidden value" name="theHidden">
verifyEquals("regexp:dd", selenium.getValue("theHidden"));
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
assertEquals("regexp:second .*", selenium.getSelectedLabel("theSelect"));
<input class="foo" value="the text value" name="theText">
verifyEquals("regexp:^f", selenium.getAttribute("theText@class"));
verifyEquals("regex:^[a-z ]+$", selenium.getValue("theText"));
<select id="theSelect">
<option id="o1" value="option1">first option</option>
<option selected="selected" id="o2" value="option2">second option</option>
<option id="o3" value="option3">third,,option</option>
</select>
assertEquals("exact:second option", selenium.getSelectedLabel("theSelect"));
String[] arr = {"regexp:^first.*?", "second option", "third*"};
verifyEquals(arr, selenium.getSelectOptions("theSelect"));
No comments:
Post a Comment