Friday, June 3, 2011

Selenium Regular Expression

Selenium supports a few methods that help match text patterns. However, selenium locators don’t accept regular expressions. Only patterns or values accept them.
Globbing:
selenium.click("link=glob:*Gifts"); // Clicks on any link with text suffixed with 'Gifts'
selenium.verifyTextPresent("glob:*Gifts*");
Regular Expressions:[regexp, regexpi]
selenium.click("link=regexpi:^Over \\$[0-9]+$");  //matches links such as 'Over $75', 'Over $85' etc
Contains:
selenium.highlight("//div[contains(@class,'cnn_sectbin')]");  //highlights the first div with class attribute that contains 'cnn_sectbin'
selenium.highlight("css=div#cat_description:contains(\"to last\")");  //locating a div containing the text 'to last' using css selector
Starts-with:
selenium.click("//img[starts-with(@id,'cat_prod_image')]");  //clicks on the first image that has an id attribute that starts with 'cat_prod_image'
selenium.click("//div[starts-with(@id,'tab_dropdown')]/a[last()]");  //clicks on the last link within the div that has a class attribute starting with 'tab_dropdown'
selenium.click("//div[starts-with(@id,'tab_dropdown')]/a[position()=2]"); //clicks on the second link within the div that has a class attribute starting with 'tab_dropdown'
selenium.highlight("css=div[class^='samples']"); //highlights div with class that starts with 'samples'
Ends-with:
selenium.highlight("css=div[class$='fabrics']"); //highlights div with class that ends with 'fabrics'
selenium.click("//img[ends-with(@id,'cat_prod_image')]"); //clicks on the first image that has an id attribute that ends with 'cat_prod_image'
[Note: ends-with is supported only by Xpath 2.0. FF 3 might throw an error for this.]
Ref: http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/

1 comment:

Venkatesh said...

I have a problem while handling dynamic values in Selenium RC.

I am executing my test case to check the 'status' field of the page. The dynamic values for 'Status' field are 'Opened' & 'Not opened'.
Here, while executing my test case, the default ‘Status’ field is 'Not opened’. After executing the test case, it will change as 'Opened'.
Once the status is changed as 'Opened', the status will never change to 'Not opened' (functionality)
Now, if I execute the same test case, it throws me an error message. Since the ‘Status’ field is 'Opened',
How can I check for the Status 'Not opened' after executing the test case? (for the same page)
Are there any possibilities to check the 'Not opened' Status?
Kindly give a suggestion for handling this scenario.