How to capture screenshot of a webpage using Selenium WebDriver?

How to capture screenshot of a webpage using Selenium WebDriver?

Hi Brett,

You can use TakesScreenshot interface to capture the screenshot of a webpage. getScreenshotAs() is a method which takes an argument of type OutputType.File so that it could return screen shot in file type. Following code shows how you can use this method to capture the screenshot of a webpage:

public class TakeScreenshot{

  WebDriver driver;
  String baseUrl;

  @BeforeTest
  public void start() {
    driver = new FirefoxDriver();
    baseUrl = "http://www.gmail.com";
    driver.get(baseUrl);
  }

  @Test
  public void testApp() throws InterruptedException, IOException {
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
     FileUtils.copyFile(scrFile, new File("D:\\gmailHomePage.jpg"));
     Thread.sleep(2000);
  }

  @AfterTest
  public void close() {
    driver.quit();
  }

}