In the previous tutorials, I have explained how we can take screenshots in Selenium using FileUtils or FileHandler. In this tutorial, I will explain how to capture screenshots of Failed Test Cases in Selenium.
We are going to use TestNG to capture screenshot of failed test cases.
We will be using below mentioned features of TestNG
1) ITestResult – This Interface will provide us the result of test case execution. @AfterMethod method can declare a parameter of type ITestResult, which will reflect the result of the test method that was just run.
2) @AfterMethod – The annotated method will be run after each test method. Any @AfterMethod can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this after the method as run.
3) result.getName() – will return name of test case so that screenshot name will be same as test case name
4) @BeforeTest – The annotated method will be run before any test method belonging to the classes inside the tag is run.
5) @AfterTest – The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
We are executing 2 test cases. One of the Test Case will pass and another will fail. This program will only capture the screenshot of failed test case, not the passed one as we have used condition
if (ITestResult.FAILURE == result.getStatus())
Let see this as a program.
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.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScreenshotFailedCases {
static WebDriver driver;
@BeforeTest
public static void init() {
System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
// Initiate Firefox browser
driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
// Pass application url
driver.get("https://duckduckgo.com/");
System.out.println("BeforeTest");
}
@Test
public void captureCorrectScreenMethod() throws Exception {
String Text = driver.findElement(By.xpath("//*[@id='logo_homepage_link']")).getText();
// Verify the text on the landing page
Assert.assertTrue(Text.contains("About DuckDuckGo"));
}
@Test
public void captureIncorrectScreenMethod() throws Exception {
// Fail test by using incorrect XPath to find the search box
driver.findElement(By.xpath("//*[@name='qe']")).sendKeys("agile");
}
@AfterTest
public static void exit() {
// Close the WebPage
driver.quit();
}
// AfterMethod annotation - This method executes after every test execution
@AfterMethod
public void screenShot(ITestResult result) {
// ITestResult.FAILURE is equals to result.getStatus then it enter into
// if condition
if (ITestResult.FAILURE == result.getStatus()) {
try {
// To create reference of TakesScreenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
// Call method to capture screenshot
File src = screenshot.getScreenshotAs(OutputType.FILE);
// Copy files to specific location result.getName() will
// return name of test case so that screenshot name will be same as test case name
FileUtils.copyFile(src, new File("./Screenshots/" + result.getName() + System.currentTimeMillis() + ".png"));
System.out.println("Successfully captured a screenshot");
} catch (Exception e) {
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
}
}


A folder with name Screenshots is created and the screenshot is placed in that folder as you can see the image below

Execution Status as shown below

TestNG Report – Go to test-output folder and open emailable-report.html
