Selenium-Extent_Reports:无法在其他计算机/机器上查看故障屏幕截图

时间:2018-05-21 06:57:35

标签: selenium-webdriver extentreports selenium-extent-report

-Failure屏幕截图在我本地计算机上的Extent_Reports中可见。但无法在其他计算机/机器上查看Extent_Reports中的故障屏幕截图。

- 当我从Jenkins触发构建时,在构建成功后,将电子邮件发送到:收件人列表

捕获屏幕截图

    public String captureScreen(String fileName) {
    if(fileName =="") {
        fileName="Screenshot";  }

    File destFile=null;
    Calendar calendar =Calendar.getInstance() ;
    SimpleDateFormat formater= new SimpleDateFormat("dd_MM_yyy_hh_mm_ss");
    File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    try {

         String reportDirectory = "/src/main/java/com/test/automation/Demo/screenshot/";
         //String reportDirectory= new File(System.getProperty("user.dir")).getAbsolutePath()+"./src/main/java/com/test/automation/Demo/screenshot/";
         destFile= new File((String)reportDirectory + fileName +"-" + formater.format(calendar.getTime())+ ".png");
         FileUtils.copyFile(srcFile,destFile );
         //This will help us to link screen shot in Extent report
         Reporter.log("<a href='"+destFile+ "'><img src='" +destFile+"' height='100' width='100'/></a>");
         //Reporter.log("<a href='"+destFile.getAbsolutePath()+ "'><img src='" +destFile.getAbsolutePath()+"' height='100' width='100'/></a>");
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    return destFile.toString();
}

用于生成包含失败测试用例屏幕截图的扩展区报告

 public void getresult(ITestResult result) {

if(result.getStatus()==ITestResult.FAILURE) 
{
  test.log(LogStatus.ERROR, result.getName()+" Test case FAILED due to below issues: "+result.getThrowable());
  String screen = captureScreen("");
  test.log(LogStatus.FAIL," Failure Screenshot : "+ test.addScreenCapture(screen));
    }}

3 个答案:

答案 0 :(得分:2)

如果您使用的是remoteWebDriver,则必须先进行扩充,然后才能使用屏幕截图功能。你试过

吗?
WebDriver driver = new RemoteWebDriver();
driver = new Augmenter().augment(driver);

// or for mobile driver

androidDriver.setFileDetector(new LocalFileDetector()); 
//this is needed when using remoteDriver

以下是我如何截取ExtentReport的截图

File scrFile = driver.getScreenshotAs(OutputType.FILE);

String dest = System.getProperty("user.dir") + "/resources/screenshots/" + dataMethod.getAndroidDriver().getSessionId() + ".png";

File destination = new File(dest);
try {
    FileUtils.copyFile(scrFile, destination);

    // this is just utility which takes screenshot and copy it to desired destination 

     dataMethod.setScreenshotPath(destination.getAbsolutePath());
} catch (IOException e) {
     e.printStackTrace();
}

代码失败:

@Override
public synchronized void onTestFailure(ITestResult result) {
    setTestEndTime(result);

    ExtentTest extentTest = methodData.getExtentTest();           
    extentTest.addScreenCaptureFromPath(methodData.getScreenshotPath());
}

希望这会有所帮助。

答案 1 :(得分:2)

我没有使用Extent报告,我有自己的报告实现。但我期待src存在问题

<img src='" +destFile+"' height='100' width='100'/></a>");

此处,destFile会显示与您的计算机相关的图像或屏幕截图的位置。同样不应该为他人工作。我们必须使用相对路径,请参阅此

https://www.w3schools.com/html/html_filepaths.asp

并确保在共享报告时,它还应包含所有需要的文件和文件夹。

答案 2 :(得分:1)

通常,由于不允许加载本地文件,因此会发生此问题。因此,即使我们采用相对路径或绝对路径,在许多情况下似乎也不起作用。 因此,我尝试改用base64screenshot,它在范围报告中显示得很好。 要将屏幕快照保存在文件夹报告中,只需照常进行屏幕截图即可。

public static String getBase64Screenshot(WebDriver driver, String screenshotName) throws IOException {
    String encodedBase64 = null;
    FileInputStream fileInputStream = null;
    TakesScreenshot screenshot = (TakesScreenshot) driver;
    File source = screenshot.getScreenshotAs(OutputType.FILE);
    String destination = windowsPath + "\\FailedTestsScreenshots\\"+screenshotName+timeStamp+".png";
    File finalDestination = new File(destination);
    FileUtils.copyFile(source, finalDestination);

    try {
        fileInputStream =new FileInputStream(finalDestination);
        byte[] bytes =new byte[(int)finalDestination.length()];
        fileInputStream.read(bytes);
        encodedBase64 = new String(Base64.encodeBase64(bytes));
    }catch (FileNotFoundException e){
        e.printStackTrace();
    }

    return encodedBase64;
}

在失败的情况下调用它:

public synchronized void onTestFailure(ITestResult result) {
    System.out.println("==="+methodDes + "=== failed!");
    try {
        WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver");
        String base64Screenshot = ExtentManager.getBase64Screenshot(driver, result.getName());
        MediaEntityModelProvider mediaModel = MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build();
        test.get().fail("image:", mediaModel);
    } catch (IOException e) {
        e.printStackTrace();
    }
    test.get().fail(result.getThrowable().getMessage());
}