Saturday, January 11, 2014

Taking Screen Shot Using Webdriver


We need to take screen shot of the application for capturing the state of the application.It may be required to store the state when application not behave as per requirement or to store the state after some operation properly happened.

There is two way to take the screen shot.

1.

a. TypeCast the driver object as TakesScreenshot class and use getScreenshotAs(OutputType.FILE) method.


b. Then copy this screen shot file in the desired directory using FileUtils.copyFile(scrFile, new File("src//google.jpg"));


Example Code is given below:

package seleniumwebdriver;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;



public class ScreenShots{
        public static void main(String[] args) throws IOException {
        WebDriver myTestDriver = new FirefoxDriver();
        myTestDriver.get("http://www.google.com");
        File scrFile = ((TakesScreenshot)myTestDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("src//google.jpg"));

        myTestDriver.quit();
    }

}

2. On previous example we take the screen shot using Selenium Web Driver but there is no way to take screen shot when any event happen on webdriver.

Here we learn how to take screen shot when any event happen webdriver take the screenshot and store the Image file into the desired path. There is 15 abstract method into the WebDriverEventListener class so when we implement this it will generate 15 methods to overrride.

1.   public void beforeNavigateTo(String url, WebDriver driver) 
2.   public void afterNavigateTo(String url, WebDriver driver)
3.   public void beforeNavigateBack(WebDriver driver)
4.   public void afterNavigateBack(WebDriver driver)
5.   public void beforeNavigateForward(WebDriver driver)
6.   public void afterNavigateForward(WebDriver driver)
7.   public void beforeFindBy(By by, WebElement element, WebDriver driver)
8.   public void afterFindBy(By by, WebElement element, WebDriver driver)
9.   public void beforeClickOn(WebElement element, WebDriver driver)
10  public void afterClickOn(WebElement element, WebDriver driver)
11  public void beforeChangeValueOf(WebElement element, WebDriver driver)
12. public void afterChangeValueOf(WebElement element, WebDriver driver)
13. public void beforeScript(String script, WebDriver driver)
14. public void afterScript(String script, WebDriver driver)
15. public void onException(Throwable throwable, WebDriver driver) 


So we need to follow this septs:




a. Implement WebDriverEventListener class  
b. We also need to register the driver  like this way :

                   WebDriverEventListener evtlistner=new ScreenShots();
                   driver = new EventFiringWebDriver(new FirefoxDriver()).register(evtlistner);


Sample code is given below:


public class ScreenShots implements WebDriverEventListener{
        public static void main(String[] args) throws IOException {
                   WebDriver driver;

                  //WebDriverListner is assigned for the desired class
                  

                   WebDriverEventListener evtlistner=new ScreenShots();
                   
                  //Register Firefox driver with this event listner
                   

                   driver=new EventFiringWebDriver(new FirefoxDriver()).register(evtlistner);
                   driver.navigate().to("https://www.google.com");
                   driver.navigate().back();
                   driver.navigate().forward();
                   driver.navigate().refresh();
               driver.quit();
    }

    @Override
    public void beforeNavigateTo(String url, WebDriver driver) {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        StringBuilder s= new StringBuilder();
        System.out.println(s.append(String.valueOf(Calendar.getInstance().get(Calendar.HOUR))).append(Calendar.getInstance().get(Calendar.HOUR)).append(Calendar.getInstance().get(Calendar.MILLISECOND)));

        try {
                    FileUtils.copyFile(scrFile, new File("src//"+s+".jpg"));
                } catch (IOException ex) {
                    Logger.getLogger(ScreenShots.class.getName()).log(Level.SEVERE, null, ex);
                }
    }

 

No comments:

Post a Comment