How to Capture Screenshot in Selenium WebDriver-2017

In my previous post we have discussed on the topic to find locators on web page. One of the major activity which comes under picture in testing is How to Capture Screenshot in Selenium WebDriver. Screenshot is required to be captured for various purpose, some of them are:

  1. To share screenshot of failed test cases to developer.
  2. While running hundred’s of test cases, screenshot is required to easily find out the bug where code fails.
  3. To capture unexpected behavior of web browser with invalid inputs.
  4. To keep the tract of resultant test cases for future reference.

Before starting with process, you should import screenshot-1.0.jar in project’s build path, click here to Download Jar

How to Capture Screenshot in Selenium WebDriver

Steps to Capture Screenshot

Step1: First one is to convert webdriver object or with the help of instance of webdriver.

Syntax: TakesScreenshot ts=(TakesScreenshot)driver
Step2: Using method name: getScreenhotAs() to create image file.

Syntax: File source=ts.getScreenshotAs(OutputType.FILE);
Step3: Defining location to store screenshot

Syntax: FileUtils.copyFile(source, new File(“./Screenshots/”+screenshotName+”.png”));

./Screenshots will create folder in your directory and copy screenshot taken in same directory.

Structure of  code to Capture Screenshot:

Best way to write out code is to define a separate screenshot.java class and defining prior method inside this class and creating an object in other class and call method defined in screenshot.java class with the help of defined method.

Step 1: Create a new class “Screenshot” and paste below code inside this class:

package “Package name”;

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;

public class Screenshot {

public static void capturescreenshot(WebDriver driver,String screenshotName)

{

try

{

TakesScreenshot ts=(TakesScreenshot)driver;

File source=ts.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(source, new File(“./Screenshots/”+screenshotName+”.png”));

System.out.println(“Screenshot taken”);

}

catch (Exception e)

{

System.out.println(“Exception while taking screenshot “+e.getMessage());

}

}

}

Step 2: Use capturescreenshot() method in other class to take take screenshot.

Create a dummy class “Login” and call method in particular class.

Give the file name inside double codes.

Syntax: Screenshot.Capturescreensho(driver,“File Name”);

 

package system.admin;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Login {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.navigate().to(“https://twitter.com/”);

driver.manage().window().maximize();

driver.findElement(By.xpath(“/html/body/div[2]/div[1]/div/div[1]/div[2]/a[2]”)).click();

screenshot.capturescreenshot(driver, “signup page – Twitter”);

}

}

Step 3:  Right Click on project click on “Refresh” option, Screenshots folder will be created inside project extend folder and find screenshot.

How to Capture Screenshot in Selenium WebDriver

Conclusion:

  1. Approach for how to capture screenshot in various conditions like when test case fails or pass.
  2. Structure of code to be defined so that quality and readability of code to be maintained.
  3. Defining path inside the code, path could be customized as per user requirement.
  4. To recognize screenshot in a particular folder inside the project itself.

To make this topic more clear watch this video

 

Leave a Reply

Your email address will not be published. Required fields are marked *