<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 9R WebDriver – 斷言和驗證 > 原文: [https://javabeginnerstutorial.com/selenium/9r-webdriver-assert-and-verify/](https://javabeginnerstutorial.com/selenium/9r-webdriver-assert-and-verify/) 嗨呀超級巨星! 我們已經定位元素很多天了。 讓我們今天換一下話題,談談“確認和驗證”。 ### 要記住 當“條件/檢查”的斷言或驗證**失敗**時,兩者之間的主要區別是: * **斷言**將使測試失敗,并且**中止**當前測試用例的執行。 跳過該特定代碼行之后的所有其他測試步驟 * **驗證**將記錄故障,但**繼續執行**測試用例。 ## 何時使用斷言和驗證? 最簡單的答案是 – 由您決定,換句話說,就是您的愿望! 您可以根據情況使用斷言或驗證,即是希望測試中止還是在檢查失敗后繼續進行。 ### 使用斷言的好處 大多數情況下,我們希望在檢查失敗時停止測試執行,而這正是我們通過斷言得到的結果。 測試用例失敗,并且清楚地突出顯示為“失敗”。 這將立即向我們顯示哪些測試用例沒有通過完整測試套件中的檢查。 然后,我們可以直接轉到那些失敗的案例,并檢查檢查/條件未通過的原因。 那不是很方便嗎? 由于此*立即反饋*的可用性,因此斷言更為常用。 ### 使用斷言的缺點 當第一個斷言條件失敗時,將不再執行以下代碼行。 可能還要執行其他檢查,我們永遠不會知道他們的結果。 ### 使用驗證的優勢 即使條件之一失敗,我們希望繼續執行測試時,通常使用此方法。 故障將被記錄或打印到控制臺。 因此,無論測試是通過還是失敗,我們都會獲得測試用例中所有檢查的結果。 ### 使用驗證的缺點 驗證不提供立即反饋,因為條件失敗后不會終止測試用例的執行。 因此,每次執行測試時,我們都必須花費大量時間在控制臺中查看日志或打印的語句,以確定哪些檢查失敗。 例如,如果要針對不同的數據集多次執行數百個測試用例,則可能不可行。 ## 示例場景 讓我們獲得為本教程系列創建的[示例網頁](https://chandanachaitanya.github.io/selenium-practice-site/)的標題。 這將是我們使用`WebDriver`的`getTitle()`方法獲得的**實際標題**。 **預期標題**是“WebDriver 演示網站”。 ### 情況 1:通過`assertEquals`通過測試用例 實際標題與預期標題相同,因此條件`Assert.assertEquals("WebDriver Demo Website", pageTitle);`的輸出將是*成功*。 將執行此行之后的代碼,并且將它*傳遞給*測試用例。 ### 情況 2:使用`assertNotEquals`使測試用例失敗 實際標題與預期標題相同,因此條件`Assert.assertNotEquals("WebDriver Demo Website", pageTitle);`的輸出將是*故障*。 此行之后的代碼將不執行。 測試執行*被中止*,并且測試用例將失敗。 **代碼段** ```java // Making the test fail Assert.assertNotEquals("WebDriver Demo Website", pageTitle); // Following lines will not be executed as above assert condition fails System.out.println("Assert not equals failed"); ``` ![Assert condition failed](https://img.kancloud.cn/ff/4a/ff4af8d261e55985fb4cb7e4e741db66_720x340.png) 上圖中的控制臺顯示`assertEquals`條件成功,因此將打印檢查后的語句,“**斷言等于通過**”,而`assertNotEquals`條件失敗,因此將不執行此檢查之后的行。 打印語句“**斷言不等于失敗**”不會打印到控制臺。 ### 情況 3:盡管`assertNotEquals`條件失敗,但通過測試用例 要僅驗證實際值和預期值是否不相等,請使用`try-catch`塊。 **代碼塊** ```java //Verify title not equal using try-catch block try { // Making the test fail Assert.assertNotEquals("WebDriver Demo Website", pageTitle); } catch(Error e){ // Following lines will be printed when the assert condition fails System.out.println("Assert not equals failed. But test execution is not aborted."); System.out.println("Error message: " + e.toString()); } ``` 即使`assertNotEquals`條件失敗,`catch`塊中的語句也將被執行,并且錯誤消息將被打印到控制臺。 ![Verify condition fails](https://img.kancloud.cn/50/f0/50f095bc76d6c30b74048bdc50d8d015_825x342.png) 如圖所示,測試用例執行成功,并且錯誤被打印到控制臺。 ## 完整的代碼 ```java import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class AssertAndVerify { // Declaring variables private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { // Selenium version 3 beta releases require system property set up System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\" + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe"); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Assign the URL to be invoked to a String variable baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/"; } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Get the page title String pageTitle = driver.getTitle(); // Print the title to console System.out.println("The actual title is: " + pageTitle); // Check if actual and expected values are equal Assert.assertEquals("WebDriver Demo Website", pageTitle); // Printing success message System.out.println("Assert equals passed."); // Making the test fail //Assert.assertNotEquals("WebDriver Demo Website", pageTitle); // Following lines will not be executed as above assert condition fails //System.out.println("Assert not equals failed"); //Verify title not equal using try-catch block try { // Making the test fail Assert.assertNotEquals("WebDriver Demo Website", pageTitle); } catch(Error e){ // Following lines will be printed when the assert condition fails System.out.println("Assert not equals failed. But test execution is not aborted."); System.out.println("Error message: " + e.toString()); } } // End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` 所有代碼文件都放置在 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium/tree/master/WebDriver)中,以方便訪問。 您可以為倉庫加注星標和分支以方便使用。 請仔細閱讀“`README.md`”文件以獲取明確說明。 總結了斷言和驗證的這一部分。 祝你有美好的一天!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看