How to take ScreenShot of failed test case using TestListenerAdapter class?

In TestNG, we can generate logs for some specific conditions like OnTestStart, OnTestSuccess, OnTestFailure, and many other. Most of us prefer to take the screen shot of the failed test case.
To Achieve this, you can either extends TestListenerAdapter or implement and interdate ITestListener.
By using TestNG listeners ‘ITestListener’ or ‘TestListenerAdapter’ we can change the default behavior write our own implementation when a Test fails or Skips etc.
TestListenerAdapter internally implements ITestListener Interface only. So, using them is like one and same.
Here, we will see how can we take logs / take screen shot of failed test cases by using TestListenerAdapter.
To know how can we achieve the same using ITestListener, refer below post – 
To start, Create one sample test case.

package screenshotOfFailedTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;


@Listeners (screenshotOfFailedTest.CustomListener.class)

public class SampleTest {

public static WebDriver driver;

@Test
public void TestCase(){
driver = new FirefoxDriver();
driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.findElement(By.xpath("invalidXpath")).click(); //using invalid xpath so that test will fail & we can invoke ITestListener.
}

}

Note: We need to mention somewhere about listenerclass in our test case. There are 2 ways to do so. You may like to do this at class level (this will be applicable to all test cases in that class) or you may like to do this at suite level (which will be applicable to whole suite. You need to define this in testng.xml file).
In above example. I have done it for class level by adding below line of code for listener.
                              @Listeners(screenshotOfFailedTest.ListenerClass.class)
Now, create a class for customListenerwhich will extends to TestListenerAdapter. Write the code to capture screen shot in OnTestFailure method.

package screenshotOfFailedTest;

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

public class CustomListener extends TestListenerAdapter {

public void onTestFailure(ITestResult result){

TakesScreenshot ts = (TakesScreenshot)SampleTest.driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);

try {
FileUtils.copyFile(srcFile, new File("./ScreenShots/"+result.getName()+".jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}

}
That’s it, all done, when you run this sample test case & if the test case fails, it will capture screen shot.
Hope this helps!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories