将屏幕截图附加到Testng报告的问题

时间:2016-06-13 08:56:58

标签: selenium selenium-webdriver testng

我在测试报告中显示屏幕截图时遇到问题。

以下是我用于捕获屏幕截图并将其附加到报告

的代码
    public void onTestFailure(ITestResult tr) {
        Object currentClass = tr.getInstance();
        WebDriver driver = ((TestBase) currentClass).getDriver();
        logger.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
        logger.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fail ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " + tr.getName());
        logger.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
        /* Capturing screen shot */

        Reporter.setCurrentTestResult(tr);

        logger.debug("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Capturing Screen");
        String imageFile;
        Date d = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd_MM_YYYY_HH_mm_ss");
        String date = df.format(d);
        Path screenShotPath = Paths.get(System.getProperty("user.dir"), new String[]{"target", "surefire-reports", "Screenshots"});
//        imageFile = screenShotPath + System.getProperty("file.separator") + tr.getName() + "_" + date + ".png";
        String fileName = System.getProperty("file.separator") + tr.getName() + "_" + date + ".png";
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(scrFile, new File(imageFile));
            Reporter.setEscapeHtml(false);
            Reporter.log("<a href=\"./Screenshots"+fileName+"\" height='100' width='100'>Screenshot</a>");
            Reporter.setCurrentTestResult(null);

        } catch (IOException e) {
            logger.debug(e, new RuntimeException());
        }
    }

正在捕捉屏幕。它甚至会附加在报告中,如下所示:

&#13;
&#13;
        <div panel-name="reporter-Life_Made_Easy" class="panel">
          <div class="main-panel-header rounded-window-top">
            <span class="header-content">Reporter output for Life Made Easy</span>
          </div> <!-- main-panel-header rounded-window-top -->
          <div class="main-panel-content rounded-window-bottom">
            <div class="reporter-method-div">
              <span class="reporter-method-name">chkLoginWithInvalidCred</span>
              <div class="reporter-method-output-div">
                <span class="reporter-method-output"><a href="./Screenshots/chkLoginWithInvalidCred_13_06_2016_14_17_16.png" height='100' width='100'>Screenshot</a></span>
              </div> <!-- reporter-method-output-div -->
            </div> <!-- reporter-method-div -->
            <div class="reporter-method-div">
              <span class="reporter-method-name">chkLoginWithValidCred</span>
              <div class="reporter-method-output-div">
                <span class="reporter-method-output"><a href="./Screenshots/chkLoginWithValidCred_13_06_2016_14_16_45.png" height='100' width='100'>Screenshot</a></span>
              </div> <!-- reporter-method-output-div -->
            </div> <!-- reporter-method-div -->
          </div> <!-- main-panel-content rounded-window-bottom -->
        </div> <!-- panel -->
&#13;
&#13;
&#13;

当我打开报告时,我看不到图像。但是当打开页面源并单击img src时,文件就会打开。

我做错了什么?为什么报告中没有显示图像。

任何帮助表示感谢。

0 个答案:

没有答案