【问题标题】:ExtentReports - screenshot not in the report - broken imageExtentReports - 屏幕截图不在报告中 - 损坏的图像
【发布时间】:2017-11-29 14:50:30
【问题描述】:

我正在尝试将屏幕截图添加到我的 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 并且下面有一个损坏的图像:

【问题讨论】:

    标签: java selenium screenshot extentreports


    【解决方案1】:

    我用的是绝对路径

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

    截屏:

      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”截屏

    版本范围报告:3, C#, NUnit 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"));
    

    问候。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这并不能解决问题,至少在我的情况下没有。
      • 如果您在同一个套件中运行多个测试并且每次失败都会出现多次失败并带有屏幕截图怎么办?
      【解决方案3】:

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

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

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

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

        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.");
                }
            }
        

        【讨论】:

          【解决方案5】:

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

          文件 src = ((TakesScreenshot) 驱动程序).getScreenshotAs(OutputType.FILE);

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

          FileUtils.copyFile(src, new File("../resources/reports/screenshots/screen2.png"));

          String 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要找到截图的绝对路径,应该从“reports”目录开始

          【讨论】:

            【解决方案6】:

            我尝试了各种网站和博客来解决这个问题,但在任何地方都找不到解决方案。在尝试了各种方法后,我得到了以下解决方案。尝试以下解决方案,它对我来说非常有效。

            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;
            }
            

            【讨论】:

            • 你能澄清一下String imgPath = Path+screenshotId+".png";中的Path吗?
            • 以上代码中,Path = System.getProperty("user.dir")+"/ExtentReports/";
            【解决方案7】:

            我尝试了以下代码,结果截图记录在报告中。

            File file = new File("./Screenshots/" + result.getName() + ".png");
            
            String absolutePath = file.getAbsolutePath();
            logger.fail("details", MediaEntityBuilder.createScreenCaptureFromPath(absolutePath).build());
            

            【讨论】:

            • 你确定它正在解决问题,如果它导致同样的错误?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-25
            • 2021-08-22
            相关资源
            最近更新 更多