Я использую testng, maven, allure в своей структуре. В настоящее время мои снимки экрана для неудачного теста сохраняются в отчетах о верности/скриншотах как: datetime-classname-methodname, например: 09-22-2017_01.13.23_ClassName_Methodname.png
Вот код для этого:
@AfterMethod
protected void screenShotIfFail(ITestResult result) throws IOException {
if (!result.isSuccess()) {
takeScreenShot(result.getMethod());
}
}
private void takeScreenShot(String name) throws IOException {
String path = getRelativePath(name);
File screenShot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShot, new File(path));
String filename = makeScreenShotFileName(name);
System.out.println("Taking Screenshot! " + filename);
Reporter.log("<a href=" + path + " target='_blank' >" + filename
+ "</a>");
}
private void takeScreenShot(ITestNGMethod testMethod) throws IOException {
String nameScreenShot = testMethod.getTestClass().getRealClass()
.getSimpleName()
+ "_" + testMethod.getMethodName();
takeScreenShot(nameScreenShot);
}
private String makeScreenShotFileName(String name) {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");
Date date = new Date();
return dateFormat.format(date) + "_" + name + ".png";
}
private String getRelativePath(String name) throws IOException {
Path path = Paths.get(".", "target", "surefire-reports", "screenShots",
makeScreenShotFileName(name));
File directory = new File(path.toString());
return directory.getCanonicalPath();
}
Чтобы привязаться к отчетам об очаровании, я попробовал @Attachment следующим образом:
@Attachment(value = "filename", type = "image/png")
private byte[] takeScreenShot(String name) throws IOException {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
Скриншот прикрепляется к отчету об очаровании, но как я могу получить его в том же формате, что и в надежных отчетах.
Спасибо !!