一、隱式等待
package com.automation.waits;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; /** * 類說明:隱式等待 * <br/> * @version 1.0 * 2016年11月22日 下午8:56:14 */ public class ImplictWait { public static void main(String[] args) { //1.打開瀏覽器; System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //2.;瀏覽器最大化 driver.manage().window().maximize(); /** * 3.設置全局隱式等待時間; * <br/>使用implicitlyWait方法,設定查找元素的等待時間; * <br/>當調用findElement方法的時候,沒有立刻找到元素,就會按照設定的隱式等待時長等待下去; * <br/>如果超過了設定的等待時間,還沒有找到元素,就拋出NoSuchElementException異常; */ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //設定隱式等待時間為10秒; //3.打開搜狗首頁; driver.get("http://www.sogou.com/"); try { //4.定位搜狗首頁:輸入框對象、搜索按鈕; WebElement inputBox = driver.findElement(By.id("query")); WebElement searchButton = driver.findElement(By.id("stb")); //5.在輸入框中輸入元素,然后點擊搜索按鈕。 inputBox.sendKeys("輸入框元素被找到了。"); searchButton.click(); } catch (NoSuchElementException e) { //如果元素沒有找到,則拋出NoSuchElementException異常。 e.printStackTrace(); } } }
隱式等待可以設定,但是有一個缺點:
缺點:如果我們在代碼中設定了隱式等待時間,當使用driver.findElement(By.*) 方法去查找頁面元素的時候,如果沒有第一時間找到元素,程序會等待下去。例如設置了隱式等待時間為10秒,某個元素沒有一開始就出現,而是在第5秒的時候 出現了,程序依然會等待10秒,然后才向下執行;
所以,推薦使用顯示等待。
二、顯式等待
顯示等待比隱式等待,更加節約測試執行的時間;
優勢:
1. 等待的方法多,都是ExpectedConditions類中的自帶方法,可以進行不同的等待判斷;
2. 等待靈活,等設置顯示等待10秒,而在第5秒元素出現了,那么就會立即向下執行,而不會繼續等待;只有超過了10秒,才拋出NoSuchElementException異常;
ExpectedConditions類自帶的等待方法:
最常用的是第三個,判斷元素在頁面中是否存在:presenceOfElementLocated(By locator)
package com.automation.waits;
import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; /** * 類說明:顯示等待 * <br/> * @version 1.0 * 2016年11月22日 下午9:38:12 */ public class explictWait { public static void main(String[] args) { //1.打開瀏覽器; System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //2.;瀏覽器最大化 driver.manage().window().maximize(); //3.打開搜狗首頁; driver.get("http://www.baidu.com/"); /* * 4.設置顯示等待時長:10秒; */ WebDriverWait wait = new WebDriverWait(driver, 10); //5.顯示等待:標題是否出現: try { wait.until(ExpectedConditions.titleContains("百度一下,你就知道")); System.out.println("百度首頁的標題出現了:百度一下,你就知道"); } catch (NoSuchElementException e) { //如果標題沒有找到,則拋出NoSuchElementException異常。 e.printStackTrace(); System.out.println("百度首頁的標題沒有找到"); } //6.顯示等待:搜索框是否出現-------最常用的顯示等待; try { wait.until(ExpectedConditions.presenceOfElementLocated(By.id("su"))); System.out.println("百度首頁的搜索輸入框出現了"); } catch (NoSuchElementException e) { //如果標題沒有找到,則拋出NoSuchElementException異常。 e.printStackTrace(); System.out.println("百度首頁的輸入框沒有找到"); } //7.顯示等待:頁面搜索按鈕是否可以被點擊; try { wait.until(ExpectedConditions.elementToBeClickable(By.id("kw"))); System.out.println("百度首頁的搜索按鈕可以被點擊"); } catch (NoSuchElementException e) { //如果標題沒有找到,則拋出NoSuchElementException異常。 e.printStackTrace(); System.out.println("百度首頁的搜索按鈕找不到"); } } }
三、自定義顯式等待
package com.automation.waits; 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.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; /** * 類說明:自定義顯示等待 * <br/> * @version 1.0 * 2016年11月22日 下午10:00:12 */ public class CustomExplictWait { public static void main(String[] args) { //1.打開瀏覽器; System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //2.;瀏覽器最大化 driver.manage().window().maximize(); //3.打開搜狗首頁; driver.get("http://www.baidu.com/"); /* * 4.自定義顯示等待,在等待代碼中找到某個元素; */ WebElement textInputBox = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver driver){ return driver.findElement(By.xpath("//*[@type='text']")); } }); textInputBox.sendKeys("自定義顯式等待1"); /* * 5.自定義顯示等待,獲取頁面元素//a[text()='關於搜狗']的文本值 */ String text = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<String>() { @Override public String apply(WebDriver driver){ return driver.findElement(By.xpath("//a[text()='關於搜狗']")).getText(); } }); System.out.println(text); //打印文本值 } }
自定義顯式等待,要注意:
期望值是WebElement類型,那么返回值一定也是WebElement類型;
我們專注於持續集成,更多原創請關注:www.hordehome.com