Thursday, July 1, 2010

TestNG - Take Screenshot of Failed Test Cases.

In my project I required to take to take the screenshot of the failed test cases. I initially searched for ready made listeners that can do this. Bug when i failed to find any i decided to write down my own.

I also wanted the screenshots to be embedded in ReportNG report. But HTML tags in ReportNG report is by default disabled. You require to set its system property to enable to HTML Tags in report.

The following listener will take the screenshot of the failed testcases and will embed that in your ReportNG report but for that you require to change in your TestNG task in your build.xml.

Listener to Take the screenshot of failed testcases
package test.Lib;


import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

public class Screenshot extends TestListenerAdapter {

public void onTestFailure(ITestResult result) {
File file = new File("");


Reporter.log("screenshot saved at "+file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
Reporter.log("<a href='../"+result.getName()+".jpg' <img src='../"+result.getName()+".jpg' hight='100' width='100'/> </a>");

public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped

public void onTestSuccess(ITestResult result) {
// will be called after test will pass 


BaseClass: BaseClass is the class where my setup(@BeforeClass) and teardown(@AfterClass) method is declared. I have taken selenium object is static so i can access it directly by its classname.

To use this listener you need to mention that in your Testng.xml file as follows.

<suite name="TestNG Take Screenshot of failed Test cases">
<listener class-name="Screenshot" /> 
<test verbose="6" name="Summery and Submit Page Page">
<class name=test.Lib.testScreenshot" />

Changes Require in TestNG task to enable HTML tags in ReportNG report.
<project name="ScreenShotFailedTestcases" basedir=".">
<target name="TestNG" depends="compile">
<taskdef resource="testngtasks" classpathref="lib.classpath"/>
<testng outputDir="${report.dir}" 
<!-- <classfileset dir="${classes.dir}" includes="**/*.class" /> -->
<xmlfileset dir="${basedir}" includes="testng.xml"/>  
<sysproperty key="org.uncommons.reportng.escape-output" value="false"/>


Sarthak said...

Thanks for the post Gaurang. I tried implementing in my project. However when testcase fails, I get get the following error at command prompt.

[testng] ===============================================
[testng] LebaraSmokeTest
[testng] Total tests run: 1, Failures: 1, Skips: 0
[testng] ===============================================

C:\Users\sarthak.dayanand\Documents\WebRefreshTest\Automation\testws\build.xml:100: The tests failed.

Total time: 45 seconds


Line number 100 is the line with listeners in build.xml


My ANT target is below.

Could you please help me resolve this.

Sumer said...
This comment has been removed by the author.
Unknown said...

i am working on testNG XSLT reports, I am trying to add a screen shot in REPORTER Log but i facing an issue.

The issue is : its posting a Path insted of link but its shoing a link in TestNG reports. Can u pls help me out

Unknown said...

Listener Screenshot was not found in project's classpath
at org.testng.TestNG.initializeConfiguration(
at org.testng.remote.RemoteTestNG.initAndRun(
at org.testng.remote.RemoteTestNG.main(

Unknown said...

Hey Maxim as error suggests the Screenshot classs has been placed in some other location and it is not in the classpath variable.

Gaurav Gupta said...

Hey Gaurang, Would you like to tell me, how can I solve the "Listener Screenshot was not found in project's classpath" problem. How can I add in the class path

Unknown said...

Hey @Gaurav try adding following code in build.xml (ANT file)
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
<pathelement path="${basedir}\bin"/>

Unknown said...

Boss, You are AWESOME ! ! !

Unknown said...

Hi Gaurang,

I am using testNg XLST to generate the test reports , I want to embed the screen shots in the report by giving hyperlink with listeners. Can you please let me know how to do this?..

Unknown said...

Thanks for posting, Gaurang!
But I was trying to get my ReportNg results but my attempts failed!
please, can You send my this project to my mail

Unknown said...

Hi Gaurang,

I could see the Reporter.log() messages from the Custom Listener are being displayed only in Log Output section of ReportNg Report. Whereas Reporter.log() messages from the Test class is being displayed at the test result page.

Is this the way this works or am I missing anything ?

Unknown said...

hi ,
i am getting an error in BaseClass.selenium.captureScreenshot line.

Unknown said...


I am using the abobe listener but i am getting an error in the below mentioned line.

I replaced the baseclass instead of my class. But still i am getting an error. Can you help me in solving the error?


Post a Comment