使用WebDriver遇到的那些坑(2)

发表于:2014-07-16来源:DiggerPlus作者:陈永达点击数: 标签:WebDriver
12345678910111213 protected Function WebDriver, Boolean isPageLoaded ( ) { return new Function WebDriver, Boolean ( ) { @Override public Boolean apply ( WebDriver driver ) { return ( ( JavascriptExecu

1
2
3
4
5
6
7
8
9
10
11
12
13
protected Function<WebDriver, Boolean> isPageLoaded() {
        return new Function<WebDriver, Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };
    }
 
    public void waitForPageLoad() {
        WebDriverWait wait = new WebDriverWait(webDriver, 30);
        wait.until(isPageLoaded());
    }

  如果页面有Ajax操作,需要写一个Wait方法等待Ajax操作完成。方式与上一条中的基本相同。比如一个Ajax操作是用于向DropDownList中填充数据,则写一个方法判断该DropDownList中元素是否多余0个。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Function<WebDriver, Boolean> haveMoreThanOneOption(final By element) {
        return new Function<WebDriver, Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                WebElement webElement = driver.findElement(element);
                if (webElement == null) {
                    return false;
                } else {
                    int size = webElement.findElements(By.tagName("option")).size();
                    return size >= 1;
                }
            }
        };
    }
 
    public void waitForDropDownListLoaded() {
        WebDriverWait wait = new WebDriverWait(webDriver, 30);
        wait.until(isPageLoaded());
    }

原文转自:http://www.huangbowen.net/blog/2013/06/25/practice-of-webdriver/