0% found this document useful (0 votes)
102 views1 page

TestNG Screenshot Automation Code

This document contains code for taking screenshots of test failures in Selenium tests. It defines methods to take a screenshot after a failed test, store it in the target/surefire-reports/screenshots folder with a timestamped filename, and add a hyperlink to the test report.

Uploaded by

Puneet Bahri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views1 page

TestNG Screenshot Automation Code

This document contains code for taking screenshots of test failures in Selenium tests. It defines methods to take a screenshot after a failed test, store it in the target/surefire-reports/screenshots folder with a timestamped filename, and add a hyperlink to the test report.

Uploaded by

Puneet Bahri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

@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