如何将屏幕截图附加到失败的步骤?

时间:2019-09-24 18:14:44

标签: selenium cucumber hook extentreports selenium-extent-report

我正在研究范围报告,我想将屏幕截图附加到失败的步骤,而不是最后一步。我在@After钩子上使用了以下代码,但随后将屏幕快照附加到了最后一步,而不是失败的步骤。我尝试将@After钩子更改为@AfterStep钩子,但是该钩子下的代码甚至都没有执行。

此挂钩是在程序包配置中定义的,并且我已按如下所示将该程序包包含在胶水中:

glue = {"stepDefinitions", "configuration"}

用于附加快照的代码:

import cucumber.api.java.After;

@After()
     public void afterScenario(Scenario scenario) {
          WebDriver driver1 = BaseConfig.setDriver();
          System.out.println("after step");
         if (scenario.isFailed()) {
             System.out.println("scenario failed");
         String screenshotName = scenario.getName().replaceAll(" ", "_");
         System.out.println(screenshotName);
         try {

             File sourcePath = ((TakesScreenshot) driver1).getScreenshotAs(OutputType.FILE);
             System.out.println(sourcePath);
             File destinationPath = new File(System.getProperty("user.dir") + "/target/cucumber-reports/" + screenshotName + ".png");
             System.out.println(destinationPath);

             Files.copy(sourcePath, destinationPath);   
             destinationPath = new File("./" + screenshotName + ".png");

             Reporter.addScreenCaptureFromPath(destinationPath.toString());


         } catch (IOException e) {
         } 
         }
         }

依赖:

   <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>4.0.9</version>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-jvm</artifactId>
        <version>4.4.0</version>
        <type>pom</type>
    </dependency>

0 个答案:

没有答案