2 回答

TA貢獻1887條經驗 獲得超5個贊
將注銷放入會很好,但請確保您以有效的方式執行此操作。@AfterMethod
如果僅測試失敗,則檢查注銷
避免使用嘗試捕獲,因為它等待給定的時間(隱式等待)來檢查存在的元素,然后進入捕獲塊而不是使用列表
引用以下代碼使用@AfterMethod
@AfterMethod
public void screenShot(ITestResult result){
if(ITestResult.FAILURE==result.getStatus()){
List<WebElement> username = driver.findElement(By.locator); // element which displays if user is logged in
if(!username.isEmpty())
// steps to logout will go here
}
}
}
另一種選擇是您可以使用TestNG監聽器。在類中實現并重寫方法,如下所示ITestListeneronTestFailure
@Override
public void onTestFailure(ITestResult result) {
if(ITestResult.FAILURE==result.getStatus()){
List<WebElement> username = driver.findElement(By.locator); // element which displays if user is logged in
if(!username.isEmpty())
// steps to logout will go here
}
}
}
在測試中添加下面的標簽.xml
<listeners>
<listener class-name="com.pack.listeners.TestListener"/> // your created class name with package which implemented ITestListener
</listeners>

TA貢獻1796條經驗 獲得超4個贊
我使用 C# 工作,但概念在所有語言中很可能是相同的。在我的例子中,我在我的基類中使用所謂的“TearDown”標簽來標記一個應該在測試后始終運行的方法。所有測試都從基類繼承此方法,并進行相應的處理。在過去的幾年里,這已經很好了,據我所知,任何類似的概念都被認為是最佳實踐。
在偽代碼中:
[TearDown]
public void Cleanup()
{
try
{
Logout();
OtherStuffLikeClosingDriver();
}
catch (Exception ex)
{
Log(ex); // Obviously, this logging function needs to generate logs that are easily readable, based on the given exception.
FinishTest(testInstance, testName); // Handles critical flows that should always be finished (and "should" not be able to error out)
throw ex; // In my case, throwing the exception again makes sure that the exception is shown in the test output directly. This often speeds up the first diagnose of a failed test run.
}
}
只要確保處理異常等:@AfterMethod中的邏輯不應該被意外問題打斷。
添加回答
舉報