標籤:關注 apply workspace new button 返回 ceo eof upload
一、隱式等待
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類內建的等待方法:
粘貼的映像816x232 3.37 KB
最常用的是第三個,判斷元素在頁面中是否存在: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>() {@Overridepublic 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>() {@Overridepublic String apply(WebDriver driver){return driver.findElement(By.xpath("//a[text()=‘關於搜狗‘]")).getText();}});System.out.println(text); //列印文本值}}
自訂顯式等待,要注意:
粘貼的映像915x200 9.17 KB
期望值是WebElement類型,那麼傳回值一定也是WebElement類型;
我們專註於持續整合,更多原創請關註:www.hordehome.com
基於Selenium2+Java的UI自動化(8)- 顯式等待和隱式等待