java - 当我的脚本失败时,用自定义错误代替堆栈跟踪的 TestNG 报告

标签 java selenium testng

我正在使用 Testng 和 selenium,如果我的脚本失败,那么它会在报告中显示总堆栈跟踪,但我只需要自定义消息就可以知道错误。

错误是这样的:

 FAILED: abc
java.lang.AssertionError: url:http//:abc.com
at org.testng.Assert.fail(Assert.java:94)
at com.ebates.qa.dotcom.test.MerchantImages.storeSmallIconImageVerification(MerchantImages.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

但实际上我不想要这个堆栈跟踪 我需要一个自定义消息来代替堆栈跟踪..

最佳答案

您可以使用 SkipException 类中的 protected 方法 ReduceStackTrace()。

创建一个新类并使用 SkipException 对其进行扩展,并在构造函数中添加 this.reduceStackTrace()。

现在抛出异常 - 抛出新创建的类。它只会在控制台中打印 2 行,而不是像以前那样打印很多行。

public class MyException extends SkipExecution
{
// auto generated constructer
public MyException(String skipMessage)
    {
    super(skipMessage);
    this.reduceStackTrace(); // this should be added.
    }

现在使用这个类来抛出你的 skipException。

throw new MyException("Hi dude");

关于java - 当我的脚本失败时,用自定义错误代替堆栈跟踪的 TestNG 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17141369/

相关文章:

python - Selenium webdriver 错误 - 过时的元素引用

java - Selenium 服务器和 Java

java - 如何使用TestNG创建多用户测试场景?

java - Apache 长耳大野兔 : Error while retrieving repository using JNDI

java - 将按钮的旧 ActionListener 替换为新的 ActionListener

java - 如何测试示例 TCP Socket 程序

maven - 运行 selenium maven 插件时 firefox 配置文件为 "parent.lock"

java - 如何在运行时获取 TestNG 线程数

xml - TestNG Xml 文件 :I want to run same class for multiple time with different parameters

java - 如何在java中分割没有任何索引的字符串