selenium测试框架篇,页面对象和元素对象的管理

发表于:2018-6-26 10:29

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

 作者:young    来源:博客园

分享:
  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);
  }
  }
  接下来封装元素,Webdriver的元素,每个元素都有相应的定位地址(xpath路径或css或id)等待时间和定位类型,默认为By.xpath
  package com.dbyl.libarary.utils;
  /**
  * This is for element library
  *
  * @author Young
  *
  */
  public class Locator {
  private String element;
  private int waitSec;
  /**
  * create a enum variable for By
  *
  * @author Young
  *
  */
  public enum ByType {
  xpath, id, linkText, name, className, cssSelector, partialLinkText, tagName
  }
  private ByType byType;
  public Locator() {
  }
  /**
  * defaut Locator ,use Xpath
  *
  * @author Young
  * @param element
  */
  public Locator(String element) {
  this.element = element;
  this.waitSec = 3;
  this.byType = ByType.xpath;
  }
  public Locator(String element, int waitSec) {
  this.waitSec = waitSec;
  this.element = element;
  this.byType = ByType.xpath;
  }
  public Locator(String element, int waitSec, ByType byType) {
  this.waitSec = waitSec;
  this.element = element;
  this.byType = byType;
  }
  public String getElement() {
  return element;
  }
  public int getWaitSec() {
  return waitSec;
  }
  public ByType getBy() {
  return byType;
  }
  public void setBy(ByType byType) {
  this.byType = byType;
  }
  }

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
42/4<1234>
100家互联网大公司java笔试题汇总,填问卷领取~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号