|
| 1 | +package testreport; |
| 2 | + |
| 3 | +import aquality.selenium.browser.AqualityServices; |
| 4 | +import org.apache.logging.log4j.core.util.FileUtils; |
| 5 | +import org.openqa.selenium.OutputType; |
| 6 | +import org.openqa.selenium.TakesScreenshot; |
| 7 | +import org.testng.ITestResult; |
| 8 | +import org.testng.Reporter; |
| 9 | +import org.testng.TestListenerAdapter; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.text.SimpleDateFormat; |
| 15 | +import java.util.Calendar; |
| 16 | + |
| 17 | +public class ScreenshotListener extends TestListenerAdapter { |
| 18 | + @Override |
| 19 | + public void onTestFailure(ITestResult result) { |
| 20 | + Calendar calendar = Calendar.getInstance(); |
| 21 | + SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); |
| 22 | + String dateString = formatter.format(calendar.getTime()); |
| 23 | + String methodName = result.getName(); |
| 24 | + if(!result.isSuccess() && AqualityServices.isBrowserStarted()){ |
| 25 | + File scrFile = ((TakesScreenshot)AqualityServices.getBrowser().getDriver()).getScreenshotAs(OutputType.FILE); |
| 26 | + try { |
| 27 | + String reportDirectory = String.format("%s/target/surefire-reports", new File(System.getProperty("user.dir")).getAbsolutePath()); |
| 28 | + File destFile = new File(String.format("%s/failure_screenshots/%s_%s.png", reportDirectory, methodName, dateString)); |
| 29 | + FileUtils.makeParentDirs(destFile); |
| 30 | + Files.copy(scrFile.toPath(), destFile.toPath()); |
| 31 | + Reporter.log(String.format("<a href='%s'> <img src='%s' height='100' width='100'/> </a>", destFile.getAbsolutePath(), destFile.getAbsolutePath())); |
| 32 | + } catch (IOException e) { |
| 33 | + AqualityServices.getLogger().fatal("An IO exception occurred while tried to save a screenshot", e); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
0 commit comments