首页 文章

获取sscreenshot附加失败的测试以引发格式的报告:datetime-classsname-testname selenium,testng,allure

提问于
浏览
0

我在我的框架中使用testng,maven,allure . 目前我的测试失败截图保存在surefire-reports / screenshots中: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);
    }

屏幕截图已附加到倾城报告中,但我怎样才能获得与surefire报告相同的格式 .

谢谢 !!

1 回答

  • 0

    您可以将自定义名称直接传递给使用 @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} 是方法参数的名称 .

相关问题