Friday 6 January 2017

How to Capture 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

Video Tutorial Is Also Available -


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 chrome browser
 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();

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

// Pass the url
driver.get("http://www.qaautomated.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());

 }
 }

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