gpt4 book ai didi

java - 无法将屏幕截图添加到 Cucumber 中的范围报告

转载 作者:行者123 更新时间:2023-11-30 05:52:08 24 4
gpt4 key购买 nike

I am unable to add the screenshot of failed case to the extent report in Cucumber (java).

//Runner class to generate extent report
@AfterClass
public static void Report() {

Reporter.loadXMLConfig(new File("/Users/chetan/Documents/workspace/Packages/extent-config.xml"));
Properties p = new Properties();
p.list(System.out);

}

//Main class contains step definitions
@After("@browser")
public void teardown(WebDriver driver, Scenario scenario, String screenshotName) throws IOException {
if (scenario.isFailed()) {
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");

driver.quit();
}
}

最佳答案

您需要在类级别或任何您喜欢的地方定义范围报告对象。然后你可以在失败时使用它。

  • 对于报告位置:在根位置的项目目录中创建名为 Report 的新文件夹
  • 对于屏幕截图位置:在根位置的项目目录中创建名为 Screenshots 的新文件夹

代码:

//Report Initialization
ExtentHtmlReporter htmlreport = new ExtentHtmlReporter(".\\Report\\Extent Report with Screenshot.html");
ExtentReports reports = new ExtentReports();
reports.attachReporter(htmlreport);
ExtentTest testlog;

//Capture and save screenshot
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + "Test.png"));

//Log Screenshot in Report
testlog.info("Details of " + "Test screenshot", MediaEntityBuilder
.createScreenCaptureFromPath(System.getProperty("user.dir") + "\\Screenshots\\" + "Test.png").build());

//Flush Report-Mandatory, Else report will not generate.
reports.flush();

为了方便使用,您可以引用我在 Github ( Click here ) 上的示例。

关于java - 无法将屏幕截图添加到 Cucumber 中的范围报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com