如何在 Extent 报告中打印 selenium 异常堆栈跟踪

How to print the selenium exception stack trace in Extent reports

我想在 Extent 报告中的 Selenium 测试的每个步骤后记录评论。因此,当某个步骤抛出异常时,我想捕获堆栈跟踪并将其打印在 Extent Reports 上。我在网上找不到任何帮助。有没有人尝试过这个并找到了方法?

例如,下面创建一个报告实例并记录评论

// new instance

ExtentReports extent = new ExtentReports(file-path, replaceExisting);

// starting test

ExtentTest test = extent.startTest("Test Name", "Sample description");

// step log

test.log(LogStatus.INFO, "Click on the object");

Reference:

http://extentreports.relevantcodes.com/java/version2/docs.html#initialize-report

如果您想记录异常的堆栈跟踪,您可以将异常堆栈跟踪转换为字符串。此 class 在 Apache commons-lang-3.3.4 jar ExceptionUtils.getStackTrace(e)

中可用

简单示例

    try{
             int num[]={1,2,3,4};
             System.out.println(num[5]);
        }catch(Exception e){        
        test.log(LogStatus.INFO/ERROR, ExceptionUtils.getStackTrace(e));
        }

希望这对您有所帮助...如果您有任何疑问,请回来

或者你可以简单地做

catch (Exception e) {
    test.log(LogStatus.FAIL, e);
}

因为 ExceptionUtils 已弃用

test.log(LogStatus.INFO/ERROR, ExceptionUtils.getStackTrace()); ->this won't help

所以我们可以使用

test.log(Status.INFO, "StackTrace Result: " + Thread.currentThread().getStackTrace());