Selenium 等待元素的方法,獲取元素的方法封裝


    ObjectMap 類和 objectMap.properties 文件將保存的元素成功的轉化成了WebElement對象。但是還不夠,接下來我們引入一下同步點的概念,就是在調用locator時,保證locator是顯示在頁面上的,webdriver中有個WebDriverWait對象。

    新建 WaitUtil 類,封裝等待的各種方法,方便在測試過程中進行調用,類的具體代碼如下:

package cn.hx.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WaitUtil {
    // 用於測試執行過程中暫停程序執行的休眠方法
    public static void sleep(long millisecond) {
        try {
            Thread.sleep(millisecond);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 顯示等待頁面元素出現的封裝方法,參數為表現頁面元素的By對象
    public static WebElement waitWebElement(WebDriver driver, final By by, int second) {
        WebElement element = null;
        WebDriverWait wait = new WebDriverWait(driver, second);
        try {
            // 創建一個新的ExpectedCondition接口,實現apply方法
            element = wait.until(new ExpectedCondition<WebElement>() {
                public WebElement apply(WebDriver d) {
                    return d.findElement(by);
                }
            });
        } catch (Exception e) {
            System.out.println(by.toString() + " is not exist until " + second);
        }
        return element;
    }

    // watiWebElement這個方法,返回的WebElement對象包括隱藏的,如果是隱藏的,那么在操作的時候,自然而然會報錯,所以,我們得把隱藏的去掉,只顯示displayed的元素對象
    public static boolean waitElementToBeDisplayed(WebDriver driver, final By by, int second) {
        boolean wait = false;
        WebElement element = driver.findElement(by);
        if (element == null)
            return wait;
        try {
            wait = new WebDriverWait(driver, second).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return element.isDisplayed();
                }
            });
        } catch (Exception e) {
            System.out.println(element.toString() + " is not displayed");
        }
        return wait;
    }

    // 等待元素消失
    public static boolean waitElementToBeNoDisplayed(WebDriver driver, final By by, int second) {
        boolean wait = false;
        WebElement element = driver.findElement(by);
        if (element == null)
            return wait;
        try {
            wait = new WebDriverWait(driver, second).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return !element.isDisplayed();
                }
            });
        } catch (Exception e) {
            System.out.println("Locator [" + element.toString() + "] is also displayed");
        }
        return wait;
    }

}

    獲取頁面元素類 GetWebElement ,可根據需求選擇獲取頁面元素的方法:

package cn.hx.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class GetWebElement {

    // 指定頁面元素定位表達式配置文件的絕對路徑
    private ObjectMap objectMap = new ObjectMap("D:/MobileSpeedTestProj/objectMap.properties");
    private WebDriver driver;

    public GetWebElement(WebDriver driver) {
        super();
        this.driver = driver;
    }

    // 等待並判斷頁面元素出現后獲取頁面元素
    public WebElement getElement(String key, int seconds) throws Exception {
        By by = objectMap.getLocator(key);
        if (!(WaitUtil.waitElementToBeDisplayed(driver, by, seconds))) {
            System.out.println("Locator為" + key + "的元素不顯示。");
        }
        return WaitUtil.waitWebElement(driver, by, seconds);
    }

    // 等待並獲取頁面元素,包括不顯示的頁面元素
    public WebElement getNotDisplayElement(String key,int seconds) throws Exception {
        By by = objectMap.getLocator(key);
        WebElement element = WaitUtil.waitWebElement(driver, by, seconds);
        return element;
    }
    
    // 獲取頁面元素By定位
    public By getElementBy(String key) throws Exception {
        By by = objectMap.getLocator(key);
        return by;
    }

}

    創建類 BeforeLineLoginPage ,用於實現獲取頁面 PageObject 對象,代碼如下:

package cn.hx.pageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import cn.hx.util.GetWebElement;

public class BeforeLineLoginPage {
    private WebElement element;
    private WebDriver driver;

    public BeforeLineLoginPage(WebDriver driver) {
        super();
        this.driver = driver;
    }

    // new一個GetWebElement類的實例,並調用driver
    public GetWebElement getWebElement() {
        GetWebElement getWebElement = new GetWebElement(driver);
        return getWebElement;
    }

    // 獲取首頁【開始測速】按鈕
    public WebElement startBtn() throws Exception {
        element = getWebElement().getElement("speedTest.beforeLineLogin.startBtn", 2000);
        return element;
    }

    // 獲取用戶須知頁面【我同意】按鈕
    public WebElement agreeBtn() throws Exception {
        element = getWebElement().getElement("speedTest.beforeLineLogin.agreeBtn", 2000);
        return element;
    }

}

 

 

    參考博客:http://www.cnblogs.com/zhangfei/p/3456159.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM