Tuesday, April 19, 2011

Selenium Tip - Using Regular Expressions

This comes straight out of the LMN Selenium classes we offer. A tip to help you find html elements that have the same series of letters in them. This is a small screen shot from one of the applications we built to filter news articles for us, determine the location and put it on a map.

Notice the two rows that have the letters bomb. How would you verify the existence of each element?


<a target="_blank" href="http://news.google.com/news/url?fd=R&amp;sa=T&amp;url=http%3A%2F%2Fwww.topnews.in%2Fsuicide-bombing-iraqi-football-stadium-2261845&amp;usg=AFQjCNGt7-LevFBRrBlwKot-8QL5ymhpOA">Suicide bombing at Iraqi football stadium - TopNews</a>
<a target="_blank" href="http://news.google.com/news/url?fd=R&amp;sa=T&amp;url=http%3A%2F%2Fwww.abc.net.au%2Fnews%2Fstories%2F2010%2F05%2F15%2F2900299.htm%3Fsection%3Dworld&amp;usg=AFQjCNG9SJlQwiS55Jv77lp8Nb5OhVwOaQ">Deadly bomb blast hits packed football stadium - ABC Online</a>

Find the link with the word bombing and then the one with the word bomb.

Selenium IDE:

Command
Target
Value
verifyElementPresent
css=a:contains(“bomb”)
verifyElementPresent
css=a:contains(“^bomb$”)


Selenium RC - jUnit
verifyTrue(selenium.isElementPresent("css=a:contains(‘^bomb$’)"));

The ^ means start with. The $ means ends with. This is called regular expression language. This combination of caret bomb dollar sign is the compete expression and will find the exact word bomb.

For more information, questions or topics you would like to see here e-mail us.


No comments:

Post a Comment