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 java.io.File;

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

public class Screenshot extends TestListenerAdapter {

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

Reporter.setCurrentTestResult(result);
System.out.println(file.getAbsolutePath());
Reporter.log(file.getAbsolutePath());

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>");
BaseClass.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
Reporter.setCurrentTestResult(null);
}


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

@Override
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">
<listeners>
<listener class-name="Screenshot" /> 
</listeners>
<test verbose="6" name="Summery and Submit Page Page">
<classes>
<class name=test.Lib.testScreenshot" />
</classes>
</test>
</suite>


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}" 
haltonfailure="false"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter"
classpathref="lib.classpath">
<!-- <classfileset dir="${classes.dir}" includes="**/*.class" /> -->
<xmlfileset dir="${basedir}" includes="testng.xml"/>  
<sysproperty key="org.uncommons.reportng.escape-output" value="false"/>
</testng> 
</target>
</project>

13 comments:

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] ===============================================
[testng]

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

Total time: 45 seconds

C:\Users\sarthak.dayanand\Documents\WebRefreshTest\Automation\testws>

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

listeners="org.uncommons.reportng.HTMLReporter">

My ANT target is below.








Could you please help me resolve this.

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

Hi GAURANG SHAH
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...

org.testng.TestNGException:
Listener Screenshot was not found in project's classpath
at org.testng.TestNG.initializeConfiguration(TestNG.java:901)
at org.testng.TestNG.run(TestNG.java:1004)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Unknown said...

@maxim
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...

@Gaurang
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"/>
</fileset>
<pathelement path="${basedir}\bin"/>
</path>

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
mmmazurkevych@gmail.com

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...

Hi,

I am using the abobe listener but i am getting an error in the below mentioned line.
BaseClass.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");

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

Thanks,
Arul

Post a Comment