Получение прикрепленного снимка экрана для неудачного теста в отчете allure в формате: datetime-classsname-testname selenium, testng, allure

Я использую 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);
    }

Скриншот прикрепляется к отчету об очаровании, но как я могу получить его в том же формате, что и в надежных отчетах.

Спасибо !!


person SeleniumTester    schedule 27.09.2017    source источник


Ответы (1)


Вы можете передать пользовательское имя непосредственно в метод с аннотацией @Attachment:

@Attachment(value = "{name}", type = "image/png")
private byte[] takeScreenShot(String name) throws IOException {
    return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}

Единственная разница между Allure 1 и Allure 2 заключается в параметре value. Для Allure 1 используйте следующий синтаксис:

value = "{0}"

Где {0} — ссылка на параметр первого метода (его индекс).

Для Allure 2 используйте следующее:

value = "{name}"

В данном случае {name} — это имя параметра метода.

person Sergey Korol    schedule 27.09.2017