@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult result) throws Exception {
if (!result.isSuccess()){
takeScreenShoot(threadDriver, result.getMethod());}
// Quit environment.
driver().close();
driver().quit();
}
public void takeScreenShoot(ThreadLocal threadDriver, ITestNGMethod
testMethod) throws Exception {
WebDriver augmentedDriver = new Augmenter().augment((WebDriver)
threadDriver.get());
File screenshot = ((TakesScreenshot)
augmentedDriver).getScreenshotAs(OutputType.FILE);
String nameScreenshot = testMethod.getMethodName();
String path = getPath(nameScreenshot);
FileUtils.copyFile(screenshot, new File(path));
Reporter.log("<a href="file://"" target="_blank">" +
this.getFileName(nameScreenshot) + "</a>");
}
private String getFileName(String nameTest) throws IOException {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");
Date date = new Date();
return dateFormat.format(date) + "_" + nameTest + ".png";
}
private String getPath(String nameTest) throws IOException {
File directory = new File(".");
String newFileNamePath = directory.getCanonicalPath() +
"/target/surefire-reports/screenShots/" + getFileName(nameTest);
return newFileNamePath;
}