簡體   English   中英

如何僅在 Selenium 的 soft 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();

所以,假設這個片段有兩個斷言,要么返回真,要么返回假。 所以 assertAll() 方法返回如下。

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM