第八章 Selenium Webdriver —等待类型
1.隐式等待
创建driver时,为浏览器对象设置一个等待时间,默认为0。这个方法是得不到某个元素就等待一段时间,在设定的时间内不断的刷新页面,直到拿到某个元素的位置。
package waittyps;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class ImplicitWaitDemo {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new InternetExplorerDriver();
baseUrl = "https://www.yahoo.com";
// Maximize the browser's window
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//针对全局的
}
@Test()
public void test() throws Exception {
driver.get(baseUrl);
driver.findElement(By.id("uh-signin")).click();
driver.findElement(By.id("login-username")).sendKeys("test");
}
@After
public void tearDown() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
2.显示等待
针对特定元素,为某个元素定制的。让元素等待直到某个条件才执行。
显示等待是我们可以定义的,等待某个条件发生,然后再继续执行代码。
elementToBeClickable 可以点击
presenceOfElementLocated 存在
visibilityOfElementLocated 显示存在
titleContains 标题中存在
package waittyps;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWaitDemo {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() {
driver = new InternetExplorerDriver();
baseUrl = "https://www.yahoo.com";
// Maximize the browser's window
driver.manage().window().maximize();
}
@Test()
public void test() {
driver.get(baseUrl);
WebElement loginLink = driver.findElement(By.id("uh-signin"));
loginLink.click();
WebDriverWait wait = new WebDriverWait(driver,3);//一般元素加载非常慢的时候才使用
WebElement emailField = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("login-username")));
emailField.sendKeys("test");
// driver.findElement(By.id("login-username")).sendKeys("test");
}
@After
public void tearDown() throws Exception{
Thread.sleep(3000);
driver.quit();
}
}
3.常见问题
设置隐式等待10秒和显示等待20秒,Selenium webdriver会等待的时间不确定 20秒会超时
4.封装显示等待
package utility;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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.ie.InternetExplorerDriver;
import utility.WaitTypes;
public class ExplicitWaitWithUtilityDemo {
private WebDriver driver;
private String baseUrl;
private WaitTypes wt;
@Before
public void setUp() {
driver = new ChromeDriver();
baseUrl = "https://www.yahoo.com";
wt = new WaitTypes(driver);
// Maximize the browser's window
driver.manage().window().maximize();
}
@Test()
public void test() {
driver.get(baseUrl);
// WebElement loginLink = driver.findElement(By.xpath("//a[@class='_yb_khsbl']"));
// loginLink.click();
wt.clickWhenReady(By.id("//a[@class='_yb_khsbl']"), 3);
WebElement emailField = wt.waitForElement(By.id("login-username"), 3);
// WebDriverWait wait = new WebDriverWait(driver,3);
// WebElement emailField = wait.until(
// ExpectedConditions.visibilityOfElementLocated(By.id("login-username")));
// driver.findElement(By.id("login-username")).sendKeys("test");
}
@After
public void tearDown() throws Exception{
Thread.sleep(3000);
driver.quit();
}
}
调用:
package utility;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WaitTypes {
WebDriver driver;
public WaitTypes(WebDriver driver) {
this.driver = driver;
}
public WebElement waitForElement(By locator, int timeout) {
WebElement element = null;
try {
System.out.println("最长等待了" + timeout + "秒元素可用");
WebDriverWait wait = new WebDriverWait(driver, timeout);
element = wait.until(
ExpectedConditions.visibilityOfElementLocated(locator));
System.out.println("元素在页面上出现了");
} catch (Exception e) {
System.out.println("元素没有在页面出现");
}
return element;
}
public void clickWhenReady(By locator, int timeout) {
try {
WebElement element = null;
System.out.println("最长等待了" + timeout + "秒元素可点击");
WebDriverWait wait = new WebDriverWait(driver, 3);
element = wait.until(
ExpectedConditions.elementToBeClickable(locator));
element.click();
System.out.println("在页面上点击了元素");
} catch (Exception e) {
System.out.println("元素没有在页面出现");
}
}
}