Tuesday 14 May 2013

Take Screenshot and save into specified location using WebDriver


Write the script in the following manner:

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class screenshot {
WebDriver driver;
@BeforeMethod
public void BM()
{
driver=new FirefoxDriver();
driver.get("http://google.com");
}
@Test
public void test()
{
try {
//Take the screenshot
File scrnsht =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Copy screenshot into a specific location
FileUtils.copyFile(scrnsht, new File("C:\\CANVAS\\k7.jpeg"));
}
catch (Exception e)
   {
e.printStackTrace();
}
}
@AfterMethod
public void AM()
{
driver.close();
driver.quit();
}
}

2 comments:

  1. Where the screenshot will be generated and how can we know whether it is copied or not.?

    ReplyDelete
  2. Hi Karthika,

    //Copy screenshot into a specific location
    FileUtils.copyFile(scrnsht, new File("C:\\CANVAS\\k7.jpeg"));

    ReplyDelete