Monday, January 28, 2013

WebDriver: isTextPresent

WebDriver is way far better in many ways than selenium RC, However if you have migrated from Selenium RC, you will miss few functions. And two on the top lists are isTextPresent and isElementPresent. Though it's not available in webdriver, it's not complicated to implement, there are various ways to the same thing. I will discuss few ways in following post.

  1. Using in in-build PageSource API
  2. public boolean isTextPresent(String text){
     if (driver.getPageSource().contains(text)) {
      return true;
     }
     else 
      return false;
    }
    Disadvantages:
    • It is too slow as compared other two options as it checks in whole page source.
    • Result might be wrong as it doesn't verify only visible text on webpage but whole source code (script + html tags)
  3. Using XPath
  4. public boolean isTextPresent(String text){
     try {
      driver.findElement(By.xpath("//*[contains(.,'"+text+"')]"));
      return true;
     }catch(NoSuchElementException e){
      return false;
     }
    }
    Disadvantages:
    As it is using XPath, it might be slow on IE than on Firefox and Chrome Browsers.
  1. Using Selenium RC Function.
  2. public boolean isTextPresent(String text){
     try{
      WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, "");
      if(selenium.isTextPresent(text)){
       return true;
      }else{
       return false;
      }
     }catch(Exception e){
      return false;
     }
    }
    Disadvantages:
    It is using Selenium RC code.

Thursday, October 18, 2012

WebDriver - Take Screenshots on Test Case Fail.

There are two way you can achieve this if you are using TestNG as you unit testing framework. Refer the following blog post to see how to do this using TestNG.
http://qtp-help.blogspot.in/2010/07/testng-take-screenshot-of-failed-test.html

Below we will discuss how to do the same thing using WebDriver listeners. Webdriver has a WebDriverEventListener interface which you can implement and override the particular listener you want. WebDriverEventListener interface has onException method which is being called whenever a WebDriver exception occurs, So we will override this method and will write down the code to take screenshot in it.

WebDriverEventListenerDemo.java
you need to implement all the methods of the WebDriverEventListener class, here I haven't show that to make code looks small.
import java.io.*;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.support.events.WebDriverEventListener;

/***
 * Implements WebDriverEventListener and overrides onException method
 * @author Gaurang
 * 
 */
public class WebDriverEventListenerDemo implements WebDriverEventListener {
 @Override
 public void onException(Throwable exception, WebDriver driver) {
  File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  try {
   FileUtils.copyFile(screenShot, new File("c:\\tmp\\exception.png"));
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}
TestWithEvenRegistered.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/***
 * Class registers the Event listern class we created 
 * @author gshah
 *
 */
public class TestWithEvenRegistered {
 
 WebDriver driver = new FirefoxDriver();
 EventFiringWebDriver eventFiringWebDriver;
 @BeforeClass
 public void setup() {
  eventFiringWebDriver = new EventFiringWebDriver(driver);
  eventFiringWebDriver.register(new WebDriverEventListenerDemo());
  eventFiringWebDriver.get("http://google.com");
  
 }
 
 /**
  * "qq" is wrong element id and so NoElementFound Exception will 
  * be generated, which will call our overridden method of WebDriverEventListenerDemo
  * class and will take the screenshot. 
  */
 @Test
 public void test(){
  eventFiringWebDriver.navigate().to("http://google.com");
  eventFiringWebDriver.findElement(By.name("qq"));
  
 }
}