在扩展方案报告4中为失败的场景添加屏幕截图

时间:2019-05-27 09:19:40

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

我正在使用范围报告4。实际上,我试图在测试失败时在范围报告中添加屏幕截图。我正在使用下面的代码来实现这一目标。屏幕快照附在我的报告末尾(请参阅屏幕快照),但是我希望屏幕截图在适当的步骤(例如,测试用例失败时)附上。请为我提供解决方案或解决方法

            case FAIL:
            String path = WrapperMethods.captureScreenshot();
            try 
            {
                test.log(logStatus, message + test.addScreenCaptureFromPath(path));
            } catch (IOException e) 
            {
                e.printStackTrace();
            }
            break;

enter image description here

1 个答案:

答案 0 :(得分:0)

对于版本4,您可以按以下方式进行操作

if (result.getStatus() == ITestResult.FAILURE) {
            test.fail(MarkupHelper.createLabel(result.getName() + "Test Failed", ExtentColor.RED));
            test.fail(result.getThrowable());
            String screencastPath = null;
            try {
                screencastPath = Screeshot.captureScreenshot(WebDriverManager.driver);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                test.fail("<b><font color=blue>" + "Screenshot Name: " + Screeshot.sendScreenName() + "</font></b>");
                test.fail("<b><font color=red>"
                        + " Click the below link OR check the report folder by above screenshot Name to get the Screenshot of failure "
                        + "</font></b>", MediaEntityBuilder.createScreenCaptureFromPath(screencastPath).build());
            } catch (IOException e) {
                test.fail("Test Failed, Cannot attach screeshot");
            }
            WebDriverManager.driver.close();
        }