第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

當(dāng)我的測試失敗或發(fā)生異常時(shí),我需要始終注銷我的應(yīng)用程序。我該怎么做?

當(dāng)我的測試失敗或發(fā)生異常時(shí),我需要始終注銷我的應(yīng)用程序。我該怎么做?

躍然一笑 2022-09-28 15:51:55
我有一個(gè)測試用例如下:@Testpublic void checkSomething(){//line1//line2//line3//line4[Exception occurs here]//line5//line6//line7 homepage.Logout();}現(xiàn)在,例如,如果第 4 行中發(fā)生異常,則我的應(yīng)用程序?qū)⒂肋h(yuǎn)不會(huì)注銷 [line7]。這將導(dǎo)致我的進(jìn)一步測試用例失敗,因?yàn)樗鼈儗o法登錄,因?yàn)橛脩魰?huì)話將處于活動(dòng)狀態(tài)。我如何使注銷始終在測試過早失敗時(shí)發(fā)生?我嘗試將注銷邏輯放在@AfterMethod中。它工作正常,但這是在配置方法(如@AfterMethod)中編寫測試代碼的最佳實(shí)踐嗎?
查看完整描述

2 回答

?
慕工程0101907

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>


查看完整回答
反對 回復(fù) 2022-09-28
?
慕的地8271018

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)該被意外問題打斷。


查看完整回答
反對 回復(fù) 2022-09-28
  • 2 回答
  • 0 關(guān)注
  • 96 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)