Selenium测试框架使用xml作为对象库

发表于:2015-9-11 11:01

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:to be crazy    来源:51Testing软件测试网采编

分享:
  那么怎么获取page的class路径?
  可以通过反射机制获取:
  locatorMap = xmlUtils.readXMLDocument(path,
  this.getClass().getCanonicalName());
  这样每次只按照page对象去加载其页面的Locator对象,而不是一次性全部加载到内存
  hashMap可以通过key去获取Locator,这样也是极好的,比之前二维数组全部遍历好多了
  封装一个basePage去处理Locator对象
package com.dbyl.libarary.utils;
import java.io.IOException;
import java.util.HashMap;
import org.openqa.selenium.Alert;
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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BasePage {
protected WebDriver driver;
// protected String[][] locatorMap;
HashMap<String, Locator> locatorMap;
String path = "C:/Users/Young/workspace/Demo/src/com/dbyl/libarary/pageAction/UILibrary.xml";
protected Log log = new Log(this.getClass());
protected BasePage(WebDriver driver) throws Exception {
this.driver = driver;
log.debug(this.getClass().getCanonicalName());
// locatorMap = ReadExcelUtil.getLocatorMap();
locatorMap = xmlUtils.readXMLDocument(path,
this.getClass().getCanonicalName());
}
protected void type(Locator locator, String values) throws Exception {
WebElement e = findElement(driver, locator);
log.info("type value is:  " + values);
e.sendKeys(values);
}
protected void click(Locator locator) throws Exception {
WebElement e = findElement(driver, locator);
log.info("click button");
e.click();
}
protected void select(Locator locator, String value) throws Exception {
WebElement e = findElement(driver, locator);
Select select = new Select(e);
try {
log.info("select by Value " + value);
select.selectByValue(value);
} catch (Exception notByValue) {
log.info("select by VisibleText " + value);
select.selectByVisibleText(value);
}
}
protected void alertConfirm() {
Alert alert = driver.switchTo().alert();
try {
alert.accept();
} catch (Exception notFindAlert) {
throw notFindAlert;
}
}
protected void alertDismiss() {
Alert alert = driver.switchTo().alert();
try {
alert.dismiss();
} catch (Exception notFindAlert) {
throw notFindAlert;
}
}
protected String getAlertText() {
Alert alert = driver.switchTo().alert();
try {
return alert.getText();
} catch (Exception notFindAlert) {
throw notFindAlert;
}
}
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:
log.debug("find element By xpath");
e = driver.findElement(By.xpath(locator.getElement()));
break;
case id:
log.debug("find element By id");
e = driver.findElement(By.id(locator.getElement()));
break;
case name:
log.debug("find element By name");
e = driver.findElement(By.name(locator.getElement()));
break;
case cssSelector:
log.debug("find element By cssSelector");
e = driver.findElement(By.cssSelector(locator.getElement()));
break;
case className:
log.debug("find element By className");
e = driver.findElement(By.className(locator.getElement()));
break;
case tagName:
log.debug("find element By tagName");
e = driver.findElement(By.tagName(locator.getElement()));
break;
case linkText:
log.debug("find element By linkText");
e = driver.findElement(By.linkText(locator.getElement()));
break;
case partialLinkText:
log.debug("find element By 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
log.error("can't find element "
+ locator.getElement());
return null;
}
}
});
return element;
}
/**
* @author Young
*
* @param locatorName
* @return
* @throws IOException
*/
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);
locator = locatorMap.get(locatorName);
if (locator == null) {
locator = new Locator(locatorName);
}
return locator;
}
}
  接下来就可以在pageAction使用,如果使用页面跳转,可以使用反射机制,封装一个PageFactory,根据传入的Page类class创建对象
  PageFactory如下:
  package com.dbyl.libarary.utils;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  import org.openqa.selenium.WebDriver;
  public class PageFactory {
  public synchronized static Object getPage(Class<?> key,WebDriver d)
  throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
  String test = key.getCanonicalName();
  System.out.println(test);
  Class<?> clazz = Class.forName(test);
  Object obj = null;
  try {
  Constructor<?> constructor = clazz.getConstructor(WebDriver.class);
  obj = constructor.newInstance(d);
  } catch (InstantiationException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (IllegalAccessException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  return obj;
  }
  }
  使用方法:
  public static HomePage login(String email, String password)
  throws Exception {
  loginPage = new LoginPage(getDriver());
  loginPage.waitForPageLoad();
  loginPage.typeEmailInputBox(email);
  loginPage.typePasswordInputBox(password);
  loginPage.clickOnLoginButton();
  Assert.assertTrue(loginPage.isPrestentProfile(), "login failed");
  return (HomePage) PageFactory.getPage(HomePage.class, getDriver());
  }
  这样,这个框架能够实现一些基本操作,下一步还需要实现失败重试截图,配合虚拟机
  下载地址:https://github.com/tobecrazy/Demo
33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号