3

-故障屏幕截图在我本地机器上的 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));
    }}
4

3 回答 3

2

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

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

在这里, destFile 带来与您的机器相关的图像或屏幕截图的位置。同样不应该适用于其他人。我们必须使用相对路径,看这个

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

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

于 2018-05-21T08:37:09.307 回答
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());
}

希望这会有所帮助。

于 2018-05-21T07:51:52.467 回答
1

通常,问题是由于不允许加载本地文件而发生的。因此,即使我们放置相对或绝对路径,这在许多情况下似乎也不起作用。所以我尝试使用 base64screenshot 来代替,它在 Extent Report 中显示得很好。要在文件夹报告中截屏,只需照常截屏即可。

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());
}
于 2018-11-29T03:03:13.920 回答