[英]How to suppress Cucumber/Junit assertion stack trace
I have a cucumber scenario and the step uses assertEquals
.我有一个黄瓜场景,该步骤使用
assertEquals
。 My results report shows the stack trace which is not end user friendly.我的结果报告显示了对最终用户不友好的堆栈跟踪。 How can I suppress it
我怎样才能压制它
Scenario: Add two numbers
Given I have two inputs "3" and "2"
When I add them
Then the output should be "15"
You're correct in observing that the default XML output (assuming you're not outputting to JSON or text, but you didn't say) from a Junit tests show stack traces for failed steps.您观察到来自 Junit 测试的默认 XML 输出(假设您没有输出到 JSON 或文本,但您没有说)显示失败步骤的堆栈跟踪是正确的。 This isn't actually a Cucumber thing.
这实际上不是黄瓜的事情。
CucumberOptions
won't help you here. CucumberOptions
在这里帮不了你。
You can:你可以:
Hope that gives you what you need.希望能给你你所需要的。
I was also facing same issue with my Cucumber-Selenium-Java project.我的 Cucumber-Selenium-Java 项目也面临同样的问题。 In the cucumber reports, it was generating around 40 lines of stacktrace.
在黄瓜报告中,它生成了大约 40 行堆栈跟踪。 Due to this, it was impacting look and feel of the report.
因此,它影响了报告的外观。 And the end user/client was little concerned about it.
最终用户/客户对此并不关心。 Because he/she was not really able to figure out the actual use of this stacktrace.
因为他/她无法真正弄清楚这个堆栈跟踪的实际用途。 So, I came up with below idea/approach.
所以,我想出了以下想法/方法。 It's little bit tricky but, it's worthy.
这有点棘手,但值得。
Few notes before starting:开始前的一些注意事项:
You need to create a common method for handling all the exceptions and reuse this method in all the required classes.您需要创建一个通用方法来处理所有异常,并在所有必需的类中重用此方法。 eg: I have named the method as "processException" and placed it in "ReusableMethod" class.
例如:我将该方法命名为“processException”并将其放在“ReusableMethod”类中。
Note that, I am using package name "page" in below method (line#8), because all my test classes are placed in this package.请注意,我在下面的方法(第 8 行)中使用了包名“page”,因为我所有的测试类都放在这个包中。 In your case you need to update the package name as per your need.
在您的情况下,您需要根据需要更新包名称。 Also, I have written custom cases for two exceptions only: NoSuchElementException & AssertionError.
另外,我只为两个异常编写了自定义案例:NoSuchElementException 和 AssertionError。 You might need to write more cases as per your need.
您可能需要根据需要编写更多案例。
public void processException(Throwable e) throws Exception {
StackTraceElement[] arr = e.getStackTrace();
String className = "";
String methodName = "";
int lineNumber = 0;
for (int i = 0; i < arr.length; i++) {
String localClassName = arr[i].getClassName();
if (localClassName.startsWith("page")) {
className = localClassName;
methodName = arr[i].getMethodName();
lineNumber = arr[i].getLineNumber();
break;
}
}
String cause = "";
try {
cause = e.getCause().toString();
} catch (NullPointerException e1) {
cause = e.getMessage();
}
StackTraceElement st = new StackTraceElement(className, methodName, "Line", lineNumber);
StackTraceElement[] sArr = { st };
if (e.getClass().getName().contains("NoSuchElementException")) {
String processedCause = cause.substring(cause.indexOf("Unable to locate"), cause.indexOf("(Session info: "))
.replaceAll("\\n", "");
Exception ex = new Exception("org.openqa.selenium.NoSuchElementException: " + processedCause);
ex.setStackTrace(sArr);
throw ex;
} else if (e.getClass().getName().contains("AssertionError")) {
AssertionError ae = new AssertionError(cause);
ae.setStackTrace(sArr);
throw ae;
} else {
Exception ex = new Exception(e.getClass() + ": " + cause);
ex.setStackTrace(sArr);
throw ex;
}
}
Below is the sample Method to showcase the usages of above method in Test Class methods.下面是示例方法,用于展示上述方法在测试类方法中的用法。 We are calling the above created method by using the class reference, which is "reuseMethod" in my case.
我们通过使用类引用来调用上面创建的方法,在我的例子中是“reuseMethod”。 And we are passing the caught Throwable reference "e" to the above method in catch block:
我们将捕获的 Throwable 引用“e”传递给 catch 块中的上述方法:
public void user_Navigates_To_Home_Page() throws Exception {
try {
//Certain lines of code as per your tests
//element.click();
} catch (Throwable e) {
reuseMethod.processException(e);
}
}
Here are few screenshots for implementation of NoSuchElementException:下面是一些实现 NoSuchElementException 的截图:
Before Implementing this approach:在实施此方法之前:
After Implementing this approach:实施此方法后:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.