You are on page 1of 1

@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://&quot;" 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;
}

You might also like