如何在 Selenium 的软 assertAll() 中仅打印失败的断言而没有堆栈跟踪?

How to print failed assertions only without stack trace in soft assertAll() in Selenium?

当 Selenium 执行 assertAll 方法时,有什么方法可以只打印失败的断言而无需整个堆栈跟踪?

SoftAssert sa = new SoftAssert();
sa.assertTrue(true,"A failed");
sa.assertTrue(false,"B failed");
sa.assertAll();

因此,假设此代码段有两个断言 returns true 和 false。所以 assertAll() 方法 returns 如下所示。

java.lang.AssertionError: The following asserts failed:
    B failed expected [true] but found [false]
    at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
    at testPlayGround.TestNGExperimental.verifyWEBCFGPermissions(TestNGExperimental.java:220)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
        .
        .
        .
        .
        .
        .
        .
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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

我想在控制台日志中看到的是这样的。

java.lang.AssertionError: The following asserts failed:
    B failed expected [true] but found [false]


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

提前致谢。

您可以使用 getMessage() 方法来打印详细消息而不是整个堆栈跟踪。

代码:

try {
    SoftAssert sa = new SoftAssert();
    sa.assertTrue(true, "A failed");
    sa.assertTrue(false, "B failed");
    sa.assertAll();
} catch (java.lang.AssertionError e) {
    System.out.println(e.getMessage());
}

输出:

The following asserts failed:
    B failed expected [true] but found [false]