Header Ads

HOW TO TAKE SCREENSHOT USING SELENIUM WEBDRIVER?

Here is the very basic program to take screenshot of the current page using Selenium Webdriver script.




Java Source code:

package com.helloselenium.selenium.test;

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;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TakeScreenshot{

 public static void main(String[] args) { 

  WebDriver driver = new FirefoxDriver();

  driver.get("http://helloselenium.blogspot.com");

  File scrFile;
  scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  try {
   FileUtils.copyFile(scrFile, new File("C:\\Screenshots\\test.png"));
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  driver.quit();
 }

}

Detailed explanation for the above program is as follows:




Following code are the required packages for Selenium Webdriver.
All required packages are available in downloaded selenium server and selenium java client jar file.
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;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Following code is to open the hello selenium blog in browser.
driver.get("http://helloselenium.blogspot.com");
You can also use the following code is to open the hello selenium blog in browser.
driver.navigate().to("http://helloselenium.blogspot.com");




Following code is to take the screenshot of the current page. In the below code we are storing the taken screenshot as a file.
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Following code is to save the taken screenshot on a location. "C:\\Screenshots\\" is the path of location and "test.png" is the filename to save the taken screenshot as a file. This code needs exception handling which can be handled using throws or try catch.
FileUtils.copyFile(scrFile, new File("C:\\Screenshots\\test.png"));
Following code is to quit the Firefox driver instance.
driver.quit();
You can also use the following code is to close the Firefox driver instance.
driver.close();



2 comments:

  1. scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    Could you explain this line in detail..What is this interface is doing..

    ReplyDelete
    Replies
    1. Basically in the above line of code TakesScreenshot is used to capture a screenshot, similarly as PrintScreen command of OS and OutputType.FILE is used to set an output of captured screen. Basically we used output type as file because later on we have to save that output as a screenshot image file.

      Delete


What would you like to add in my list? I look forward to reading your comments below.



Powered by Blogger.