Selenium中的隱式等待和顯式等待
在Selenium中,“等待”在執行測試中起著重要作用。在本文中,您將學習Selenium中“隱式”和“顯式”等待的各個方面。
在本文中,您將學習 -
2. 隱瞞等待
3. 明確等待
4. 流利的等待
為什麼我們需要在Selenium中等待?
大多數Web應用程式都是使用Ajax和Javascript開發的。當瀏覽器載入頁面時,我們想要與之互動的元素可能以不同的時間間隔載入。
它不僅難以識別元素,而且如果元素未定位,它將丟擲“ ElementNotVisibleException ”異常。使用Waits,我們可以解決此問題。
讓我們考慮一個場景,我們必須在測試中使用隱式和顯式等待。假設隱式等待時間設定為20秒,顯式等待時間設定為10秒。
假設我們試圖找到一個具有一些“ExpectedConditions ”(顯式等待)的元素,如果該元素不在顯式等待(10秒)定義的時間範圍內,它將使用由隱式等待定義的時間幀(在丟擲“ ElementNotVisibleException ” 之前20秒)。
Selenium Web驅動程式等待
- 隱含的等待
- 明確等待
隱含的等待
Selenium Web Driver借用了Watir隱式等待的想法。
隱式等待將告訴Web驅動程式在它丟擲“No Such Element Exception”之前等待一定的時間。預設設定為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 = "" ; // 啟動Chrome並將其重定向到基本網址 driver.get("http://demo.guru99.com/test/guru99home/" ); //最大化瀏覽器視窗 driver.manage().window().maximize() ; //獲取標題的實際值 aTitle = driver.getTitle(); //將實際標題與預期標題進行比較 if (aTitle.equals(eTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } //關閉瀏覽器 driver.close(); } }
程式碼說明
在上面的例子中,
考慮以下程式碼:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
隱式等待將接受2個引數,第一個引數將接受時間作為整數值,第二個引數將接受時間測量,包括SECONDS,MINUTES,MILISECOND,MICROSECONDS,NANOSECONDS,DAYS,HOURS等。
明確等待
顯式等待用於告訴Web驅動程式在丟擲“ ElementNotVisibleException ”異常之前等待某些條件(預期條件)或超過最大時間。
顯式等待是一種智慧的等待,但它只能應用於指定的元素。顯式等待提供比隱式等待更好的選項,因為它將等待動態載入的Ajax元素。
一旦我們宣告顯式等待,我們必須使用“ ExpectedCondtions ”,或者我們可以配置我們想要使用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 = "" ;
// 啟動Chrome並將其重定向到Base URL
driver.get("http://demo.guru99.com/test/guru99home/" );
//最大化瀏覽器視窗
driver.manage().window().maximize() ;
//獲取標題的實際值
aTitle = driver.getTitle();
//將實際標題與預期標題進行比較
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();
}
}
程式碼說明
考慮以下程式碼:
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程式碼宣告我們正在等待網頁上“ WebDriverWait ”類中定義的20秒時間幀的元素,直到滿足“ ExpectedConditions ”並且條件為“ visibilityofElementLocated ”。
以下是可以在顯式等待中使用的預期條件
· alertIsPresent()
· elementSelectionStateToBe()
· elementToBeClickable()
· elementToBeSelected()
· frameToBeAvaliableAndSwitchToIt()
· invisibilityOfTheElementLocated()
· invisibilityOfElementWithText()
· presenceOfAllElementsLocatedBy()
· presenceOfElementLocated()
· textToBePresentInElement()
· textToBePresentInElementLocated()
· textToBePresentInElementValue()
· titleIs()
· titleContains()
· visibilityOf()
· visibilityOfAllElements()
· visibilityOfAllElementsLocatedBy()
· visibilityOfElementLocated()
流利的等待
流暢的等待用於告訴Web驅動程式等待條件,以及在丟擲“ElementNotVisibleException”異常之前我們想要檢查條件的頻率。
頻率:設定具有時間範圍的重複迴圈,以定期驗證/檢查條件
讓我們考慮一個場景,其中元素以不同的時間間隔載入。如果我們宣告顯式等待20秒,該元素可能會在10秒,20秒甚至更長時間內載入。在丟擲異常之前它會等到指定的時間。在這種情況下,流暢的等待是理想的等待使用,因為這將嘗試以不同的頻率找到元素,直到它找到它或最終的計時器用完為止。
句法:
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, 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();
// 啟動Chrome並將其重定向到Base URL
driver.get("http://demo.guru99.com/test/guru99home/" );
//最大化瀏覽器視窗
driver.manage().window().maximize() ;
//獲取標題的實際值
aTitle = driver.getTitle();
//將實際標題與預期標題進行比較
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"));
}
});
//點選selenium連結
clickseleniumlink.click();
//關閉瀏覽器
driver.close() ;
}
}
程式碼說明
考慮以下程式碼:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
在上面的例子中,我們通過忽略“ NoSuchElementException ” 來宣告一個流暢的等待,超時為30秒,頻率設定為5秒。
考慮以下程式碼:
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元素只不過是網頁上的selenium連結)。
頻率設定為5秒,最大時間設定為30秒。因此,這意味著它將每隔5秒檢查網頁上的元素,最長時間為30秒。如果元素位於此時間範圍內,它將執行操作,否則將丟擲“ ElementNotVisibleException ”
隱式等待與顯式等待的區別
隱含的等待 |
明確等待 |
|
|
|
|
|
|
結論:
隱式,顯式和流利等待是selenium中使用的不同等待。這些等待的使用完全基於以不同時間間隔載入的元素。在測試我們的應用程式或構建我們的框架時,始終不建議使用Thread.Sleep()。
如果你需要更多的selenium自動化的相關知識,歡迎繼續關注我!當然你也可以加入我的群:175317069,一起進行學習交流,不僅僅只有自動化哦~