Monday, January 28, 2013

isTextPresent() ? In WebDriver

While automation web applications in some places we want/need to check particular text is present or not.

In WebDriver (Selenium 2), there is no predefined method for checking this. (In Selenium-RC isTextPresent("text") built-in method exist ) So we need to implement our own method to achieve this.


Let us consider you are searching for text "WebDriver"



boolean isTextPrest=false;

Method-1
 isTextPrest=driver.findElement(By.tagName("body")).getText().contains("WebDriver");  

Method-2
 isTextPrest=driver.findElement(By.xpath("//*[contains(.,'WebDriver')]")).isDisplayed();  

Method-3
 Selenium sel=new WebDriverBackedSelenium(driver, "");  
 isTextPresent=sel.isTextPresent("WebDriver");  


FYI : Method-1 will take more time than remaining (comparatively)

Happy Coding :)


Regards,
SantoshSarma

1 comment:

  1. We can also use following code line.

    isTextPrest = driver.getPageSource().contains("WebDriver");

    ReplyDelete

Note: Only a member of this blog may post a comment.