2 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
將注銷放入會(huì)很好,但請確保您以有效的方式執(zhí)行此操作。@AfterMethod
如果僅測試失敗,則檢查注銷
避免使用嘗試捕獲,因?yàn)樗却o定的時(shí)間(隱式等待)來檢查存在的元素,然后進(jìn)入捕獲塊而不是使用列表
引用以下代碼使用@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監(jiān)聽器。在類中實(shí)現(xiàn)并重寫方法,如下所示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
}
}
}
在測試中添加下面的標(biāo)簽.xml
<listeners>
<listener class-name="com.pack.listeners.TestListener"/> // your created class name with package which implemented ITestListener
</listeners>

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
我使用 C# 工作,但概念在所有語言中很可能是相同的。在我的例子中,我在我的基類中使用所謂的“TearDown”標(biāo)簽來標(biāo)記一個(gè)應(yīng)該在測試后始終運(yùn)行的方法。所有測試都從基類繼承此方法,并進(jìn)行相應(yīng)的處理。在過去的幾年里,這已經(jīng)很好了,據(jù)我所知,任何類似的概念都被認(rèn)為是最佳實(shí)踐。
在偽代碼中:
[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中的邏輯不應(yīng)該被意外問題打斷。
添加回答
舉報(bào)