gpt4 book ai didi

java - 使用范围报告打印屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:06 24 4
gpt4 key购买 nike

我不明白为什么我的屏幕截图没有出现在本地的 ExtentReport 中(我还没有从构建服务器尝试过)。这是调用 takeScreenShot 方法的 TestNG @AfterMethod:-

@AfterMethod
public void afterMethod(ITestResult result) throws IOException {
APPLICATION_LOGS.debug("Running @AfterMethod");
switch (result.getStatus()) {
case ITestResult.FAILURE:
childTest.fail(result.getThrowable())
.addScreenCaptureFromPath((takeScreenShot(result.getMethod().getMethodName())));
try {
childTest.info("<a href='https://mywebsite.com/fabrix/logs/s/" + webSessionId + "'" + "target=_'blank'>Data control logs</a>");
} catch (Exception e) {
childTest.info("Unable to get Session ID");
}//.....more code

这是被调用的方法:

private String takeScreenShot(String methodName) {
String path = System.getProperty("user.dir") + "\\target\\reports\\screenshots\\" + methodName + ".png";
try {
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(path));
} catch (Exception e) {
APPLICATION_LOGS.error("Could not write screenshot" + e);
}
return path;
}

报告:

[![在此处输入图像描述][1]][1]

我使用 Chrome 开发者工具检查图像,路径是完美的,并且图像保存在与报告相同的目录中。那么为什么不显示呢?

来自 IntelliJ:

enter image description here

最佳答案

这是因为指定的屏幕截图路径需要相对于报告路径。我自己也经历了一番艰难才发现这一点。虽然屏幕截图确实是在您认为的位置拍摄并存储的,但由于它与报告的位置完全相同,因此只需省略通向文件名的路径即可。

在我的输出中,我将报告文件放在项目文件夹外的 target\reports 文件夹中,但随后我有一个 target\reports\screenshots 文件夹,因此当我将屏幕截图附加到测试对象时,我只指定“screenshots\filename.png”,当我尝试完整路径时,它不会工作。

关于java - 使用范围报告打印屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078525/

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