Selenium 中 Assert 和 Verify 的區別是什麼?
Selenium 中 Assert 和 Verify 之間存在差異。兩者都用於驗證頁面上是否存在 Web 元素。如果 Assert 失敗,則測試執行將停止。
一旦某個步驟中的斷言未透過,則其後的測試步驟將被跳過。但是,可以透過新增 try-catch 塊並將斷言包含在此塊中來避免這種情況。
因此,如果斷言產生真值條件,則程式執行流程將繼續。否則,失敗步驟之後的步驟將被繞過執行。
為了克服這個問題,引入了軟斷言或 Verify 命令的概念。在這裡,如果發生失敗,測試執行將繼續,並且會捕獲失敗日誌。因此,無論 Verify 命令產生真值條件還是假值條件,程式執行流程都將繼續。
這種型別的斷言用於不重要的場景,即使步驟的預期結果與實際結果不匹配,我們也可以繼續進行測試。
使用 Assert 的程式碼實現
import org.testng.Assert;
import org.testng.Annotations.Test;
public class NewTest{
@Test
public void f() {
//Assertion pass scenario
Assert.assertTrue(2+2 == 4);
System.out.println("Scenario 1 passed");
//Assertion fail scenario
Assert.fail("Scenario 2 failed with Assert");
System.out.println("Scenario 2 failed");
}
}輸出

使用 Verify/SoftAssert 的程式碼實現
import org.testng.Assert;
import org.testng.Annotations.Test;
import org.testng.asserts.SoftAssert;
public class NewTest{
@Test
public void f() {
//instance of SoftAssert
SoftAssert s = new SoftAssert();
//Assertion failed
s.fail("Scenario 1 failed with Soft Assert");
System.out.println("Scenario 1 failed");
//Assertion failed
s.fail("Scenario 2 failed with Soft Assert");
System.out.println("Scenario 2 failed");
//collects assertion result then mark test as failed/passed
s.assertAll()ß
}
}輸出

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP