Tuesday, November 15, 2011

verifyTextPresent in Web Driver

Following function takes the WebElement and searches the text of elements with particular tags
public boolean verifyTextPresent(WebElement rootElement, String tag, String text) {
List elements = rootElement.findElements(By.tagName(tag));
boolean match = false;
for (WebElement elem : elements){
String elementText = elem.getText();
if (elementText.contains(text)){
match = true;
break;
}
}
try {
Assert.assertTrue(match);
System.out.println("\"" + text + "\" was found within elements within the passed in WebElement with ID (" + rootElement.getAttribute("id")+ ")");
}
catch (Exception e) {
System.err.println("\"" + text + "\" not found within elements within the passed in WebElement with ID (" + rootElement.getAttribute("id")+ ")");
}
return match;
}

No comments:

Post a Comment