Tuesday, December 4, 2012

Take a screenshot with Selenium WebDriver for Selenium


Take a screenshot with Selenium WebDriver for Selenium:

--------------------------------------------------------------------------

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

public class Screenshot{


public void screenshot(WebDriver webdriver, String filename) throws IOException{

//Taking the Screenshot

File screenshot = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
//Saving the image
  try {
                 FileUtils.copyFile(screenshot, new File(filename));
             } catch (IOException e) {
                 e.printStackTrace();
             }           

}
--------------------------------------------------------------------------
public static void main(String[] args) throws IOException {
 Screenshot  s = new Screenshot();
 String file1 = "d://Screenshot//screenshot.png";
 s.screenshot(data.oWebDriver, file1);

 }
}

By HoaLe

No comments:

Post a Comment