![](/img/trans.png)
[英]When do you declare a WebDriver driver public or private when using @BeforeMethod and @BeforeClass annotations
[英]How do I catch exceptions if thrown in @BeforeMethod or @BeforeClass without using a TestNGListener
我有一個針對特定功能的測試類,並具有被視為BaseClass的測試類層,其中發生了驅動程序初始化和測試啟動(我在這里避免使用Listener)。 在BaseClass中定義了一個AfterMethod,該方法可以注銷並捕獲帶有異常和stacktrace的結果,並記錄到數據庫中,如果測試失敗,我可以捕獲異常,如果BeforeMethod或BeforeClass失敗,則不捕獲異常,所有看到的是測試狀態,顯示為“跳過”。 這是我在AfterMethod中使用的代碼:
public void afterMethodBase(ITestResult result) {
logger.debug("TestName : " + result.getTestName());
logger.info("Method Name : " + result.getMethod().getMethodName());
ResultLogger.processResult(result);
Throwable exception = itr.getThrowable(); // this is null when status is skipped
}
如何捕獲BeforeMethod
或BeforeClass
引發的異常
您可以使用偵聽器及其適當的方法: IConfigurationListener#onConfigurationFailure(ITestResult)
查看文檔以獲取有關偵聽器的更多詳細信息。
如果您不想通過偵聽器訪問它,那么為什么不這樣訪問它們呢?
從通過您的@AfterMethod
注釋方法的ITestResult
對象中:
首先通過result.getTestContext().getFailedConfigurations().getAllMethods()
掌握所有失敗的配置方法
然后,通過調用org.testng.ITestNGMethod#isBeforeClassConfiguration
遍歷上述ITestNGMethod
集合,以僅過濾出@BeforeClass
故障。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.