Saturday 1 February 2014

Taking Screenshot with WebDriver


You can use the below script to take the screenshot :



package webdriverJunit.testScripts;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.sun.jna.platform.FileUtils;

public class TakingScreenShot {

      /**
       * @param args
       * @throws MalformedURLException
       */
      public static void main(String[] args) throws MalformedURLException {
             WebDriver driver = new RemoteWebDriver( new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.firefox());
             driver.get("http://www.google.com");

// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
             getScreenshotAs(OutputType.FILE);

try {
      org.apache.commons.io.FileUtils.copyFile(screenshot, new File("D:\\Rajendra\\Test.jpg"));
} catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
}

      }

}

No comments:

Post a Comment