I have used following code —
For capturing screenshot
public CaptureScreenshot(AndroidDriver driver) {
super(driver);
// TODO Auto-generated constructor stub
}
//public static AndroidDriver<MobileElement> driver;
// function to capture screenshot.
public void captureScreenShot(String fileName){
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date(0));
String destination = System.getProperty("user.dir") + "/screenshots/" +fileName+".png";
TakesScreenshot takeScreenshot = ((TakesScreenshot)this.driver);
File srcFile = takeScreenshot.getScreenshotAs(OutputType.FILE);
File destFile = new File(destination);
try {
FileUtils.copyFile(srcFile, destFile);
System.out.println("Screenshot saved successfully at " + System.currentTimeMillis());
}
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
}
}
For calling function in listener
@Override
public void onTestFailure(ITestResult result) {
Reporter.log(“Status of test result” + result.getStatus());
System.out.println("Testcase failed for " + result.getMethod().getMethodName());
CaptureScreenshot bc = new CaptureScreenshot(null);
bc.captureScreenShot(result.getMethod().getMethodName());
}