<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國際加速解決方案。 廣告
                # Selenium WebDriver 中的隱式,顯式&流利等待 > 原文: [https://www.guru99.com/implicit-explicit-waits-selenium.html](https://www.guru99.com/implicit-explicit-waits-selenium.html) 在硒中,“等待”在執行測試中起著重要作用。 在本教程中,您將學習 Selenium 中“隱式”和“顯式”等待的各個方面。 在本教程中,您將學習: * [為什么我們需要硒中的等待?](#1) * [隱式等待](#2) * [顯式等待](#3) * [流利的等待](#4) ## 為什么我們需要硒? 大多數 Web 應用程序都是使用 Ajax 和 Javascript 開發的。 當瀏覽器加載頁面時,我們要與之交互的元素可能會在不同的時間間隔加載。 這不僅使識別元素變得困難,而且如果沒有找到該元素,也會引發“ **ElementNotVisibleException** ”異常。 使用等待,我們可以解決此問題。 讓我們考慮一下在測試中必須同時使用隱式和顯式等待的情況。 假定隱式等待時間設置為 20 秒,顯式等待時間設置為 10 秒。 假設我們正在嘗試找到一個具有**“ ExpectedConditions** ”(顯式等待)的元素,如果該元素不在顯式等待(10 秒)定義的時間范圍內,它將使用該時間 由隱式等待(20 秒)定義的幀,然后拋出“ **ElementNotVisibleException** ”。 **Selenium Web 驅動程序等待** 1. 隱式等待 2. 顯式等待 ## 隱式等待 Selenium Web Driver 從 Watir 借用了隱式等待的想法。 隱式等待將告訴 Web 驅動程序等待一定的時間,然后再引發“無此類元素異常”。 默認設置為 0。一旦設置了時間,Web 驅動程序將等待該時間,然后引發異常。 在下面的示例中,我們聲明了一個隱式等待,其時間范圍為 10 秒。 這意味著,如果該元素不在該時間段內位于網頁上,它將引發異常。 聲明隱式等待: **語法**: ``` driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS); ``` ``` package guru.test99; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class AppTest { protected WebDriver driver; @Test public void guru99tutorials() throws InterruptedException { System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" ); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ; String eTitle = "Demo Guru99 Page"; String aTitle = "" ; // launch Chrome and redirect it to the Base URL driver.get("http://demo.guru99.com/test/guru99home/" ); //Maximizes the browser window driver.manage().window().maximize() ; //get the actual value of the title aTitle = driver.getTitle(); //compare the actual title with the expected title if (aTitle.equals(eTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } //close browser driver.close(); } } ``` **代碼**的說明 在以上示例中, **考慮以下代碼:** ``` driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ; ``` 隱式等待將接受 2 個參數,第一個參數將接受時間作為整數值,第二個參數將接受以秒,分鐘,英里,微秒,納秒,天,小時等為單位的時間度量。 ## 顯式等待 顯式等待用于告訴 Web 驅動程序等待某些條件(**預期條件**)或在引發“ **ElementNotVisibleException** ”異常之前超過的最大時間。 顯式等待是一種聰明的等待,但是它只能應用于指定的元素。 顯式等待比隱式等待提供更好的選擇,因為它將等待動態加載的 Ajax 元素。 聲明顯式等待后,我們必須使用“ **ExpectedConditions** ”,也可以使用 **Fluent Wait** 來配置檢查條件的頻率。 這些天,在實現我們使用 **Thread.Sleep()**時,通常不建議使用 在下面的示例中,我們將創建等待“ **WebDriverWait** ”類的引用,并使用“ **WebDriver** ”引用實例化,并且給出的最大時間范圍為 20 秒。 **語法:** ``` WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); ``` ``` package guru.test99; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class AppTest2 { protected WebDriver driver; @Test public void guru99tutorials() throws InterruptedException { System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" ); driver = new ChromeDriver(); WebDriverWait wait=new WebDriverWait(driver, 20); String eTitle = "Demo Guru99 Page"; String aTitle = "" ; // launch Chrome and redirect it to the Base URL driver.get("http://demo.guru99.com/test/guru99home/" ); //Maximizes the browser window driver.manage().window().maximize() ; //get the actual value of the title aTitle = driver.getTitle(); //compare the actual title with the expected title if (aTitle.contentEquals(eTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } WebElement guru99seleniumlink; guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"))); guru99seleniumlink.click(); } } ``` **Explanation of Code** **Consider Following Code:** ``` WebElement guru99seleniumlink; guru99seleniumlink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"))); guru99seleniumlink.click(); ``` 在上面的示例中,等待在“ **WebDriverWait** ”類或“ **ExpectedConditions** ”類中定義的時間發生,以先到者為準。 上面的 [Java](/java-tutorial.html) 代碼指出,我們正在等待元素的時間為 20 秒(如網頁上的 **WebDriverWait** 類中所定義的),直到[ **ExpectedConditions** 滿足,條件為“ **visibleofElementLocated** ”。 以下是可以在顯式等待中使用的預期條件 1. alertIsPresent() 2. elementSelectionStateToBe() 3. elementToBeClickable() 4. elementToBeSelected() 5. frameToBeAvaliableAndSwitchToIt() 6. invisibilityOfTheElementLocated() 7. invisibilityOfElementWithText() 8. presentOfAllElementsLocatedBy() 9. presentOfElementLocated() 10. textToBePresentInElement() 11. textToBePresentInElementLocated() 12. textToBePresentInElementValue() 13. titleIs() 14. titleContains() 15. visibleOf() 16. visibleOfAllElements() 17. visibleOfAllElementsLocatedBy() 18. visibleOfElementLocated() ## 流利的等待 流利的等待用于告訴 Web 驅動程序等待條件,以及在拋出“ ElementNotVisibleException”異常之前我們要用來檢查條件的**頻率**。 **頻率:**設置一個具有時間范圍的重復周期,以固定時間間隔驗證/檢查條件 讓我們考慮一個場景,其中元素以不同的時間間隔加載。 如果我們聲明顯式等待 20 秒,則該元素可能會在 10 秒,20 秒甚至更長的時間內加載。 它會等到指定的時間,然后引發異常。 在這種情況下,流暢的等待是理想的等待方式,因為它將嘗試以不同的頻率查找元素,直到找到它或最終計時器用完為止。 **語法:** ``` Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class); ``` 在 Selenium v??3.11 及更高版本中不建議使用以上代碼。 您需要使用 ``` Wait wait = new FluentWait(WebDriver reference) .withTimeout(Duration.ofSeconds(SECONDS)) .pollingEvery(Duration.ofSeconds(SECONDS)) .ignoring(Exception.class); ``` ``` package guru.test99; import org.testng.annotations.Test; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import java.util.function.Function; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class AppTest3 { protected WebDriver driver; @Test public void guru99tutorials() throws InterruptedException { System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" ); String eTitle = "Demo Guru99 Page"; String aTitle = "" ; driver = new ChromeDriver(); // launch Chrome and redirect it to the Base URL driver.get("http://demo.guru99.com/test/guru99home/" ); //Maximizes the browser window driver.manage().window().maximize() ; //get the actual value of the title aTitle = driver.getTitle(); //compare the actual title with the expected title if (aTitle.contentEquals(eTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement clickseleniumlink = wait.until(new Function<WebDriver, WebElement>(){ public WebElement apply(WebDriver driver ) { return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")); } }); //click on the selenium link clickseleniumlink.click(); //close~ browser driver.close() ; } } ``` **代碼**的說明 **Consider Following Code:** ``` Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); ``` 在上面的示例中,我們通過忽略“ **NoSuchElementException** ”聲明了等待時間為 30 秒且頻率設置為 5 秒的流暢等待 **Consider Following Code:** ``` public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")); ``` 我們創建了一個新功能來識別頁面上的 Web 元素。 (例如:這里的 Web 元素不過是網頁上的硒鏈接)。 頻率設置為 5 秒,最長時間設置為 30 秒。 因此,這意味著它將每 5 秒檢查一次網頁上的元素,最長時間為 30 秒。 如果元素位于此時間范圍內,它將執行操作,否則將拋出“ **ElementNotVisibleException** ” ## 隱式等待與顯式等待之間的差異 | <center>**隱式等待**</center> | <center>**顯式等待**</center> | | * 隱式等待時間應用于腳本 中的所有元素 | * 顯式等待時間僅適用于我們想要的那些元素 | | * 在隱式等待中,我們需要 **而不是** 在要定位的元素上指定“ ExpectedConditions” | * 在“顯式等待”中,我們需要在要定位的元素上指定“ ExpectedConditions” | | * 當元素位于隱式等待 中指定的時間范圍內時,建議使用 | * 建議在元素需要較長時間加載時使用,也建議用于驗證元素的屬性,例如(visibilityOfElementLocated,elementToBeClickable,elementToBeSelected) | **結論:** **隱式,顯式**和**流利等待**是硒中使用的不同等待。 這些等待的使用完全基于在不同時間間隔加載的元素。 在[測試我們的應用程序或構建我們的框架時,始終不建議**使用** Thread.Sleep()。](/software-testing.html) ***本文由 Chaitanya Pujari 提供***
                  <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>

                              哎呀哎呀视频在线观看