How to capture screenshot for failed test cases in Selenium

How to capture screenshot for failed test cases in Selenium Webdriver,selenium providing TakesScreenshot interface to capture screenshot for failed test cases using TestNG or Junit framework.In order to capture screenshots in Test execution time,we need to design reusable methods in the framework.

Mostly scripts fails in different conditions

1.Element locators issue,some times xpath will changes after new CR and build deployment in QA environment
2.Issue in Applications such as 404 page not found,SQL Server exceptions,StateElement exceptions etc.

How to capture screenshot for failed test cases in Selenium

Send Extent Reports in Email with Screenshots,in this post i’ve given complete Selenium code to capture screenshots and attach in Extent Reports to send in emails ,if you have not go through this post,please read above post for more details.In this post we will learn capture screenshot for failed test cases in Selenium code with proper examples.We will use

ALSO READ  How to run Selenium script in IE Browser

READ Test NG ITestResult INTERFACE documentation and TestNG Annotations documentation

1.ITestResult interface – This class describes the result of a test and test case name.
2.@AfterMethod – This is a TestNG annotation,The annotated method will be run after each test method.

ALSO READ Write Test Cases PASS or FAIL in Excel using Selenium

ALSO READ  How to run TestNG test cases using Maven

TypeError can’t access dead object Geckodriver

In case you are new to TestNG ,please read TestNG Tutorials.

Code to capture screenshot for failed test cases in Selenium

=======================================================

package com.selenium.practice;
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 TakeScreenshot 
{
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 successfully");
} 
catch (Exception e)
{

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

======================================================

ALSO READ Send Extent Reports in Email with Screenshots

=====================================================

package com.selenium.practice;
import java.io.File;
import library.Utility;
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.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class GoogleScreenshot {

// Create Webdriver reference
public static WebDriver driver;

@Test
public void testOne() throws Exception
{

// Initiate Google chrome browser
System.setProperty("webdriver.chrome.driver","CommonJarFiles/chromedriver.exe");
driver = new ChromeDriver();

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

// Pass application url
driver.get("http://www.google.co.in");

driver.findElement(By.xpath("//input[@name='q']")).sendKeys("Selenium Automation");
driver.findElement(By.xpath("//input[@name=''btnK]")).click();

} 

// It will execute after every test execution 
@AfterMethod
public void tearDown(ITestResult result)
{

// Here will compare if test is failing then only it will enter into if condition
if(ITestResult.FAILURE==result.getStatus())
{
//Call TakeScreenshot method 
TakeScreenshot.captureScreenshot(driver,"Google Search.png");
}
// close application
driver.quit();
} 


}

This is the question mostly asking in Selenium Automation interview ,it is very useful question in selenium.Please provide your valuable comments on this post.

Rajesh

Rajesh has over 8+ years of experience in Quality Assurance (Automation/ Manual). I've completed his B.Texh. He is an experienced software tester,work experience include working on tools like TestNG, Sikuli, Autoit, Selenium Builder and Appium. Abhishek has worked on various projects from verticals like Retail, Health Insurance, tolling and many more and passionate about designing Automation Frameworks that are effective and easy to maintain using Selenium Java.

Leave a Reply

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