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