• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Capture Screenshot in Selenium Webdriver and Use in Reporting

How to Capture Screenshot in Selenium Webdriver and Use in Reporting

April 14, 2020 by Mukesh Otwani

Screenshot in Selenium Webdriver

Hello, welcome to Selenium tutorials, in this post we will see how to capture Screenshot in Selenium Webdriver and use the same screenshot for reporting or for bug reporting or for any purpose.

In automation, it is mandatory to take the screenshot for verification so that it can prove also that your test case has covered certain functionality or not. Screenshots also help you a lot when your test case will fail so that you can identify what went wrong in your script or in your application.

Do you know that you can also use Robot class to capture screenshot in Selenium? When you capture screenshot using Robot Class it capture complete window screenshot. It will help you to capture alert as well which you can’t use with the normal screenshot method of Selenium.

Recently I used one of the reports called Extent Report which helps to attach the screenshot to the report which actually makes my reporting better than traditional TestNG reporting.

Screenshot in Selenium Webdriver

I also implemented screenshot on failure in my framework which makes my test more robust so I would recommend you to implement the same in your framework too.

 

Note-  If you are using 3.6 and above version of Selenium then you should FileHandler Class in order to copy the screenshots.

 

                                                                         I have created a youtube video on same. 

Note-if you are working with 3.6 version and above

Recently Selenium has done some changes in recent version so if you are using Selenium 3.6.0 then you need to add below jar to project or if you are using then you can provide below dependency in project.

https://mvnrepository.com/artifact/commons-io/commons-io 

Apache Commons IO

How to capture Screenshot in Selenium webdriver

For taking  screenshots Selenium has provided TakesScreenShot interface in this interface you can use getScreenshotAs method which will capture the entire screenshot in form of file then using FileUtils we can copy screenshots from one location to another location

Scenario – Open Google  and take screenshot

Let’s implement the same

How to capture Screenshot in Selenium Webdriver

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;

import org.testng.annotations.Test;

public class ScreenshootGoogle {

 @Test
 public void TestJavaS1()
{
// Open Firefox
 WebDriver driver=new FirefoxDriver();

// Maximize the window
driver.manage().window().maximize();

// Pass the url
driver.get("http://www.google.com");

// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
}

catch (IOException e)
 {
  System.out.println(e.getMessage());

 }
 }

 

If you use the above code then it will take the screenshot and will store in C:/selenium and screenshot name will be error.png

Note- If you are using Selenium version 3.6 and above then you will not find FileUtils class. You can use FileHandler class which is again part of Selenium. Please check detailed article on this.

Now consider a scenario where you have to take multiple screenshots then above code will be repetitive so for this we will create a small method which captures screenshots and will call this method from our script.

Example-

 public static void captureScreenShot(WebDriver ldriver){

  // Take screenshot and store as a file format
  File src= ((TakesScreenshot)ldriver).getScreenshotAs(OutputType.FILE);
try {
  // now copy the  screenshot to desired location using copyFile method

 FileUtils.copyFile(src, new File("C:/selenium/"+System.currentTimeMillis()+".png"));
       }

catch (IOException e)

{

System.out.println(e.getMessage());

    }

}

 

So this method we can call from our test case and call multiple time.

Copy the full code and try to execute in Eclipse

package com.bog.htmldemo;

The complete program for Screenshot in Selenium Webdriver

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;

import org.testng.annotations.Test;

public class ScreenshootGoogle {

@Test
public void TestJavaS1(){

 // Open Firefox
 WebDriver driver=new FirefoxDriver();

// call method
                        ScreenshootGoogle.captureScreenShot(driver);

// Maximize the window
driver.manage().window().maximize();
                        ScreenshootGoogle.captureScreenShot(driver);

// Pass the url
driver.get("http://www.google.com");
                  ScreenshootGoogle.captureScreenShot(driver);

  }

public static void captureScreenShot(WebDriver ldriver){
// Take screenshot and store as a file format             
 File src=((TakesScreenshot)ldriver).getScreenshotAs(OutputType.FILE);           
try {
// now copy the  screenshot to desired location using copyFile method

FileUtils.copyFile(src, new File("C:/selenium/"+System.currentTimeMillis()+".png"));                              } catch (IOException e)

{
  System.out.println(e.getMessage()) 
 }
  }

}

 

So in the above program, you can see I called captureScreenshot method three times like this you can create reusable actions in your script and you can use the same.

 

Filed Under: Advance Selenium Tagged With: Screenshots

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?