ExtentReports - 屏幕截图不在报告中 - 损坏的图像

ExtentReports - screenshot not in the report - broken image

我正在尝试将屏幕截图添加到我的 ExtentReport HTML 文件,但由于某种原因,图像不存在,即使它确实存在并且控制台显示它正在查看正确的位置( href 是正确的)。

这是最新的试用码:

Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
String destination = getScreenshotPath();
ImageIO.write(screenshot.getImage(), "IMG", new File(destination));
test.fail("Details: " + test.addScreenCaptureFromPath(destination));

屏幕截图已保存到目的地。 当我尝试调试模式或查看报告时,它打印为:

详情:com.aventstack.extentreports.ExtentTest@62041567 它下面有一张破损的图片:

我用的是绝对路径

注意:从浏览器检查损坏的图像以验证图像的绝对路径

截图:

  public static String TakesScreenshot(IWebDriver driver, string FileName)
    {

        string pathProject = AppDomain.CurrentDomain.BaseDirectory;
        string pathScreen = pathProject.Replace("\bin\Debug", "");
        string path = pathScreen + "project/Test-output/Images/";

        StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString());
        TimeAndDate.Replace("/", "_");
        TimeAndDate.Replace(":", "_");
        TimeAndDate.Replace(" ", "_");

        string imageName = FileName + TimeAndDate.ToString();

        ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(path + "_" + imageName + "." + System.Drawing.Imaging.ImageFormat.Jpeg);

        return path + "_" + imageName + "." + "jpeg";
    }

使用预览方法的路径将图像附加到报告中: 具体步骤中:

ExtentTest.Fail("message", MediaEntityBuilder.CreateScreenCaptureFromPath(TakeScreenShot.TakesScreenshot(driver, "Fatal")).Build());

用方法"TakesScreenshot"截图

版本 ExtentReport:3, C#, N单元 3

使用 JAVA:

        <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
        </dependency>

是:

 ExtentTestManager.getTest().log(LogStatus.ERROR, ExtentTestManager.getTest().addScreenCapture("//ABOLUTE/PATH/IMAGE.PNG"));

问候。

如建议的那样 - 绝对路径可能是一个解决方案,但我不想那样做。

我发现一个解决方案是将图像存储在生成报告的同一目录中,将图像名称命名为 .addScreenCaptureFromPath(screenshotName.PNG) 并且效果很好。

为了在范围报告中获取屏幕截图,只需在范围报告屏幕截图路径中添加一个额外的点。参考以下代码:

test.log(Status.INFO, "FAQs button clicked",
                        MediaEntityBuilder.createScreenCaptureFromPath("." + screenshot()).build());

希望对您有所帮助!

这对你有用,对我也有用。

public void afterScenario(Scenario scenario) throws IOException {
        if (scenario.isFailed()) {
            String screenshotName = scenario.getName().replaceAll(" ", "_");
            //This takes a screenshot from the driver at save it to the specified location
            File sourcePath = ((TakesScreenshot) testContext.getWebDriverManager().getDriver()).getScreenshotAs(OutputType.FILE);

            //Building up the destination path for the screenshot to save
            //Also make sure to create a folder 'screenshots' with in the cucumber-report folder
            File destinationPath = new File("./src/test/resources/"+screenshotName + ".png");

            //Copy taken screenshot from source location to destination location
            Files.copy(sourcePath, destinationPath);

            //This attach the specified screenshot to the test
            Reporter.addScreenCaptureFromPath(screenshotName+".png");
            System.out.println("Cant take screenshot in grid.");
        }
    }

您可以添加绝对路径截图如下:

文件源 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

//文件复制,绝对路径从你的项目目录开始

FileUtils.copyFile(src, 新文件("../resources/reports/screenshots/screen2.png"));

字符串 img = logger.addScreenCapture("./screenshots/screen2.png");

logger.log(LogStatus.PASS, "标题已验证...", img);

请注意,这里的 addScreenCapture() 方法路径从屏幕截图文件夹开始,而不是 copyFile() 方法中的资源。这是因为 ExtentReports 被初始化为:

ExtentReports 报告 = new ExtentReports("../resources/reports/extentreports_v2.html", true);

所以 extentreports_v2.html 要找到屏幕截图的绝对路径,它应该从“报告”目录开始

我针对这个问题尝试了各种网站和博客,但无法在任何地方找到解决方案。尝试各种方法后,我得到了以下解决方案。试试下面的解决方案,它对我来说效果很好。

public static synchronized String addScreenshots(){
     WebDriver webDriver = Browser.getDriver();
     Long l = Calendar.getInstance().getTimeInMillis();
     String screenshotId = l.toString();
     String Path = System.getProperty("user.dir")+"/ExtentReports/"; 

     File screenshot = ((TakeScreenshot)WebDriver).getScreenshotAs(OutputType.FILE);
     String imgPath = Path+screenshotId+".png";
     
     File dest = new File(imgPath);
     try {
         FileUtils.copyFile(screenShot, dest);
     } catch (IOException e) {
         e.printStackTrace();
     }
     
     String ImagePath = "../ExtentReports/"+screenshotId+".png";
     
     return ImagePath;
}

我尝试了下面的代码,结果在报告中记录了一个屏幕截图。

File file = new File("./Screenshots/" + result.getName() + ".png");

String absolutePath = file.getAbsolutePath();
logger.fail("details", MediaEntityBuilder.createScreenCaptureFromPath(absolutePath).build());