Hi Amit,
You can try the following code to capture screenshot on test fail.
public class ScreenShotOnFail extends TestListenerAdapter
{
static String newScreenShotPath=null;
static String screenShotFile=null;
@Override
public void onTestFailure(ITestResult arg0)
{
try {
File location=new File(".");
newScreenShotPath=location.getCanonicalPath()+"\\test-output\\screenShots\\"+arg0.getName()+getdate()+"";
screenShotFile=newScreenShotPath+"_Report"+".png";
captureScreenShot(screenShotFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Reporter.log("<a href=\""+ screenShotFile+"\">"+arg0.getName()+"_ErrorLogScreenShot");
}
public static void captureScreenShot(String name) throws Exception
{
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot,new File(name) );
Thread.sleep(2000);
}
public static String getdate()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
Date date = new Date();
return dateFormat.format(date);