Hi,
Please find below the code
package com.aol.Test1;
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) {
// Tear down method saved the screenshot in a temporary location
// Rename the file to specific test case file name
File file = new File("");
String tempFileName = file.getAbsolutePath() + "\\reports\\temp.jpg";
String newFileName = file.getAbsolutePath()+ "\\reports\\"+result.getName()+ ".jpg";
try
{
// Delete if the file exists.
File newFile = new File(newFileName);
if (newFile.exists())
newFile.delete();
file = new File(tempFileName);
file.renameTo(new File(newFileName));
if (file.exists())
file.delete();
}
catch (Exception e)
{
}
Reporter.setCurrentTestResult(result);
Reporter.log("screenshot saved at "+ newFileName);
Reporter.log("<a href='../reports/"+result.getName()+".jpg'> <img src='../reports/"+result.getName()+".jpg' height='100' width='100'/> </a>");
//MainTestCoordinator.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
Reporter.setCurrentTestResult(null);
}
@Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
// Delete the temp file
File file = new File("");
String tempFileName = file.getAbsolutePath() + "\\reports\\temp.jpg";
File file1 = new File(tempFileName);
file1.delete();
}
}
And the code to capture screenshot independant of success or failure is in the tearDownTest
@AfterMethod(alwaysRun=true)
public void tearDownTest()throws Exception {
// Take screen shot independent of success/failure
File file = new File("");
MainTest.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\temp.jpg");
}
The same code is working perfectly fine while running scripts in eclipse using Testng.xml ---> Run as TestNG suite.
It is not working properly when running using command line.
Thanks lot for your help.