Friday, 6 January 2017

Take ScreenShot with Selenium WebDriver

Taking screenshot while testing is very important for automation so that you can point out and catch the exact bug. In this post we will learn about how to take screen shot when selenium test case fails. It is more important to have a screenshot when the selenium test fail but you can add the code if you want to capture screen shot when selenium test passes.
Once you know how to use it then you can use it anywhere you want. So lets get started . . .

Take ScreenShot with Selenium WebDriver

1. Take Screen shot when JUnit Test Case Fails


import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org..junit.Test;
     
    public class takeScreenShotExample{
        public WebDriver driver; 

        String filePath = "D:\\SCREENSHOTS";  

      @Test
      public void openBrowser() throws Exception {
       driver = new FirefoxDriver();
       driver.manage().window().maximize();
       driver.get("http://www.qaautomated.com");
       try{
                //the below statement will throw an exception as the element is not found, Catch block will get executed and takes the screenshot.
        driver.findElement(By.id("About Me")).click();             

         }catch (Exception e){
        System.out.println("I'm in exception");
                 //method to take the screenshot.
        getscreenshot(openBrowser);
        }
      }
      
      public void getscreenshot(String methodName) throws Exception 
      {
              File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
           //The below method will save the screen shot in d drive with name "screenshot.png"
              FileUtils.copyFile(scrFile, new File(filePath+methodName+".png"));
      }
 }

2. Take Screen shot when TestNG Test Case Fails -


To achieve this first we need to create a class and then implement TestNG 'ITestListener interface'. We will override a method called 'onTestFailure' and add the code to take the screen shot . Instead of just taking the screen shot, we will get the Test method name and save  screenshot with test name in  destination folder.

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.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.pack.sample.TestBase;

public class TestListener implements ITestListener {
 WebDriver driver=null;
 String filePath = "D:\\SCREENSHOTS"; 
 
    @Override
    public void onTestFailure(ITestResult result) {
     System.out.println("***** Error "+result.getName()+" test has failed *****");
     String methodName=result.getName().toString().trim();
     takeScreenShot(methodName);
    }
    
    public void takeScreenShot(String methodName) {
     //get the driver
     driver=TestBase.getDriver();
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
         //The below method will save the screen shot in d drive with test method name 
            try {
    FileUtils.copyFile(scrFile, new File(filePath+methodName+".png"));
    System.out.println("***Placed screen shot in "+filePath+" ***");
   } catch (IOException e) {
    e.printStackTrace();
   }
    }
 
}  

Whenever test case fails it will execute onTestFailure() method which intern will call takeScreenShot() method. For this we need to add the below code in our test testng.xml while inside <project> tag


<listeners>
       <listener class-name="com.pack.listeners.TestListener"/>
  </listeners>

3 comments:

  1. Hello,
    The Article on Take ScreenShot with Selenium WebDriver is informative give detail information about it.Thanks for Sharing the information about program taking a ScreenShot with Selenium WebDriver. Software Testing Services

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Nice information Thanks a lot for such a great information.

    ReplyDelete