Issue #15 Selenium2.0使用升级
使用WebDriverWait与ExpectedConditions实现灵活等待。 取消对Selenium1.0的依赖,通过加大implicitlyWait解决waitForPageLoad的问题。
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
package org.springside.modules.test.functional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebDriverBackedSelenium;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.springside.modules.utils.Threads;
|
||||
|
||||
import com.thoughtworks.selenium.Selenium;
|
||||
import org.openqa.selenium.support.ui.ExpectedCondition;
|
||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
/**
|
||||
* 融合了Selenium 1.0 API与Selenium 2.0的By选择器的API.
|
||||
@@ -23,32 +23,30 @@ import com.thoughtworks.selenium.Selenium;
|
||||
*/
|
||||
public class Selenium2 {
|
||||
|
||||
public static final int DEFAULT_TIMEOUT = 5000;
|
||||
public static final int DEFAULT_PAUSE_TIME = 250;
|
||||
|
||||
public static int DEFAULT_WAIT_TIME = 20;
|
||||
private WebDriver driver;
|
||||
private Selenium selenium;
|
||||
private int defaultTimeout = DEFAULT_TIMEOUT;
|
||||
private String baseUrl;
|
||||
|
||||
public Selenium2(WebDriver driver, String baseUrl) {
|
||||
this.driver = driver;
|
||||
this.selenium = new WebDriverBackedSelenium(driver, baseUrl);
|
||||
this.baseUrl = baseUrl;
|
||||
setImplicitlyWait(DEFAULT_WAIT_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不设置baseUrl的构造函数, 调用open函数时必须使用绝对路径.
|
||||
*/
|
||||
public Selenium2(WebDriver driver) {
|
||||
this(driver, "");
|
||||
this(driver, null);
|
||||
}
|
||||
|
||||
// Driver 函數 //
|
||||
/**
|
||||
* 打开地址,如果url为相对地址, 自动添加baseUrl.
|
||||
* @param url
|
||||
*/
|
||||
public void open(String url) {
|
||||
selenium.open(url);
|
||||
waitForPageToLoad();
|
||||
final String urlToOpen = url.indexOf("://") == -1 ? baseUrl + (!url.startsWith("/") ? "/" : "") + url : url;
|
||||
driver.get(urlToOpen);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,12 +57,28 @@ public class Selenium2 {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取页面地址.
|
||||
* 退出Selenium.
|
||||
*/
|
||||
public String getLocation() {
|
||||
return driver.getCurrentUrl();
|
||||
public void quit() {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置如果查找不到Element时的默认最大等待时间。
|
||||
*/
|
||||
public void setImplicitlyWait(int seconds) {
|
||||
driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取WebDriver实例, 调用未封装的函数.
|
||||
*/
|
||||
public WebDriver getDriver() {
|
||||
return driver;
|
||||
}
|
||||
|
||||
//Elemenet 函數//
|
||||
|
||||
/**
|
||||
* 查找Element.
|
||||
*/
|
||||
@@ -79,17 +93,10 @@ public class Selenium2 {
|
||||
return driver.findElements(by);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断页面内是否存在文本内容.
|
||||
*/
|
||||
public boolean isTextPresent(String text) {
|
||||
return selenium.isTextPresent(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断页面内是否存在Element.
|
||||
*/
|
||||
public boolean isElementPresent(By by) {
|
||||
public boolean isPresent(By by) {
|
||||
try {
|
||||
driver.findElement(by);
|
||||
return true;
|
||||
@@ -121,14 +128,6 @@ public class Selenium2 {
|
||||
driver.findElement(by).click();
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击Element, 跳转到新页面.
|
||||
*/
|
||||
public void clickTo(By by) {
|
||||
driver.findElement(by).click();
|
||||
waitForPageToLoad();
|
||||
}
|
||||
|
||||
/**
|
||||
* 选中Element.
|
||||
*/
|
||||
@@ -168,7 +167,7 @@ public class Selenium2 {
|
||||
*/
|
||||
public boolean isChecked(By by) {
|
||||
WebElement element = driver.findElement(by);
|
||||
return element.isSelected();
|
||||
return isChecked(element);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,6 +198,47 @@ public class Selenium2 {
|
||||
return element.getAttribute("value");
|
||||
}
|
||||
|
||||
// WaitFor 函數 //
|
||||
/**
|
||||
* 等待Element的内容可见, timeout单位为秒.
|
||||
*/
|
||||
public void waitForVisible(By by, int timeout) {
|
||||
(new WebDriverWait(driver, timeout)).until(ExpectedConditions.visibilityOfElementLocated(by));
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待Element的内容为text, timeout单位为秒.
|
||||
*/
|
||||
public void waitForTextPresent(By by, String text, int timeout) {
|
||||
(new WebDriverWait(driver, timeout)).until(ExpectedConditions.textToBePresentInElement(by, text));
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待Element的value值为value, timeout单位为秒.
|
||||
*/
|
||||
public void waitForValuePresent(By by, String value, int timeout) {
|
||||
(new WebDriverWait(driver, timeout)).until(ExpectedConditions.textToBePresentInElementValue(by, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待其他Conditions, timeout单位为秒.
|
||||
*
|
||||
* @see ExpectedConditions
|
||||
*/
|
||||
public void waitForCondition(ExpectedCondition conditon, int timeout) {
|
||||
(new WebDriverWait(driver, timeout)).until(conditon);
|
||||
}
|
||||
|
||||
// Selenium1.0 函數 //
|
||||
|
||||
/**
|
||||
* 简单判断页面内是否存在文本内容.
|
||||
*/
|
||||
public boolean isTextPresent(String text) {
|
||||
String bodyText = driver.findElement(By.tagName("body")).getText();
|
||||
return bodyText.contains(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得单元格的内容, 序列从0开始, Selnium1.0的常用函数.
|
||||
*/
|
||||
@@ -212,61 +252,4 @@ public class Selenium2 {
|
||||
public String getTable(By by, int rowIndex, int columnIndex) {
|
||||
return getTable(driver.findElement(by), rowIndex, columnIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待页面载入完成, timeout时间为defaultTimeout的值.
|
||||
*/
|
||||
public void waitForPageToLoad() {
|
||||
waitForPageToLoad(defaultTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待页面载入完成, timeout单位为毫秒.
|
||||
*/
|
||||
public void waitForPageToLoad(int timeout) {
|
||||
selenium.waitForPageToLoad(String.valueOf(timeout));
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待Element的内容展现, timeout单位为毫秒.
|
||||
*/
|
||||
public void waitForVisible(By by, int timeout) {
|
||||
long timeoutTime = System.currentTimeMillis() + timeout;
|
||||
while (System.currentTimeMillis() < timeoutTime) {
|
||||
if (isDisplayed(by)) {
|
||||
return;
|
||||
}
|
||||
Threads.sleep(DEFAULT_PAUSE_TIME);
|
||||
}
|
||||
throw new RuntimeException("waitForVisible timeout");
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出Selenium.
|
||||
*/
|
||||
public void quit() {
|
||||
driver.close();
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取WebDriver实例, 调用未封装的函数.
|
||||
*/
|
||||
public WebDriver getDriver() {
|
||||
return driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Selenium实例,调用未封装的函数.
|
||||
*/
|
||||
public Selenium getSelenium() {
|
||||
return selenium;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认页面超时.
|
||||
*/
|
||||
public void setDefaultTimeout(int defaultTimeout) {
|
||||
this.defaultTimeout = defaultTimeout;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user