做自动化框架,不可避免的就是对象库。
有一个好的对象库,可以让整个测试体系:
- 更容易维护
- 大大增加代码重用
- 增加测试系统的稳定性
这里先了解一下我所说的对象库:
所谓的页面对象,是指每一个真是的页面是一个对象。
比如zhihu的登陆页面是一个页面对象,http://www.zhihu.com/#signin
这个页面对象主要包含一个输入邮箱的输入框(一个元素对象),一个输入密码的密码框
一个登陆框。当然,zhihu不止一个页面,有无数页面,每一个页面都可以封装为一个对象。而每个
页面的元素,也可以封装成一个个元素对象。
为什么要封装成一个个对象?
还是以这个登陆页面为例,如果有一天zhihu改版,登陆界面UI变了,(但是需要输入用户名和密码还有登陆按钮不会消失吧)。
登陆页面的元素的位置也相应改变,如果你的测试用例没有封装过页面和元素, 每个页面都是拿webdriver 直接写,页面元素定位
也分布到测试用例中,这要维护起来要全部改掉测试用例。如果你封装了页面,封装了元素,再封装一个对应的登陆Action,你的每个
测试用例是调用的login.action()。 这样,你只需要改变你对象库的内容就完美解决UI变化,而不必一个个修改测试用例。
测试框架目录如下:
接下来一这个登陆为例:
首先封装一个BasePage的类,毕竟所有的页面都有共同的东西,每个页面都有元素,每个页面元素都有相应的方法
这里简单封装了几个方法,如type
package com.dbyl.libarary.utils;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BasePage {
protected WebDriver driver;
protected String[][] locatorMap;
protected BasePage(WebDriver driver) throws IOException {
this.driver = driver;
locatorMap = ReadExcelUtil.getLocatorMap();
}
protected void type(Locator locator, String values) throws Exception {
WebElement e = findElement(driver, locator);
e.sendKeys(values);
}
protected void click(Locator locator) throws Exception {
WebElement e = findElement(driver, locator);
e.click();
}
protected void clickAndHold(Locator locator) throws IOException {
WebElement e = findElement(driver, locator);
Actions actions = new Actions(driver);
actions.clickAndHold(e).perform();
}
public WebDriver getDriver() {
return driver;
}
public void setDriver(WebDriver driver) {
this.driver = driver;
}
public WebElement getElement(Locator locator) throws IOException {
return getElement(this.getDriver(), locator);
}
/**
* get by parameter
*
* @author Young
* @param driver
* @param locator
* @return
* @throws IOException
*/
public WebElement getElement(WebDriver driver, Locator locator)
throws IOException {
locator = getLocator(locator.getElement());
WebElement e;
switch (locator.getBy()) {
case xpath:
e = driver.findElement(By.xpath(locator.getElement()));
break;
case id:
e = driver.findElement(By.id(locator.getElement()));
break;
case name:
e = driver.findElement(By.name(locator.getElement()));
break;
case cssSelector:
e = driver.findElement(By.cssSelector(locator.getElement()));
break;
case className:
e = driver.findElement(By.className(locator.getElement()));
break;
case tagName:
e = driver.findElement(By.tagName(locator.getElement()));
break;
case linkText:
e = driver.findElement(By.linkText(locator.getElement()));
break;
case partialLinkText:
e = driver.findElement(By.partialLinkText(locator.getElement()));
break;
default:
e = driver.findElement(By.id(locator.getElement()));
}
return e;
}
public boolean isElementPresent(WebDriver driver, Locator myLocator,
int timeOut) throws IOException {
final Locator locator = getLocator(myLocator.getElement());
boolean isPresent = false;
WebDriverWait wait = new WebDriverWait(driver, 60);
isPresent = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return findElement(d, locator);
}
}).isDisplayed();
return isPresent;
}
/**
* This Method for check isPresent Locator
*
* @param locator
* @param timeOut
* @return
* @throws IOException
*/
public boolean isElementPresent(Locator locator, int timeOut)
throws IOException {
return isElementPresent(driver,locator, timeOut);
}
/**
*
* @param driver
* @param locator
* @return
*/
public WebElement findElement(WebDriver driver, final Locator locator) {
WebElement element = (new WebDriverWait(driver, locator.getWaitSec()))
.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
return getElement(driver, locator);
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
}
});
return element;
}
public Locator getLocator(String locatorName) throws IOException {
Locator locator;
for (int i = 0; i < locatorMap.length; i++) {
if (locatorMap[i][0].endsWith(locatorName)) {
return locator = new Locator(locatorMap[i][1]);
}
}
return locator = new Locator(locatorName);
}
}