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

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

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

 作者:young    来源:博客园

分享:
  接下来就是登陆页面的类,这个登陆页面的元素,放在excel统一管理,要获取元素的信息,首先从excel读取。
  读取excel的页面元素是使用POI开源框架
  package com.dbyl.libarary.utils;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.DateUtil;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.ss.usermodel.Sheet;
  public class ReadExcelUtil {
  static String path;
  /**
  * @author Young
  * @return
  * @throws IOException
  */
  public static String[][] getLocatorMap() throws IOException {
  path = "C:/Users/Young/workspace/Demo/src/com/dbyl/libarary/pageAction/UILibrary.xls";
  File f1 = new File(path);
  FileInputStream in = new FileInputStream(f1);
  HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(in));
  Sheet sheet = wb.getSheetAt(0);
  Row header = sheet.getRow(0);
  String[][] locatorMap = new String[sheet.getLastRowNum() + 1][header
  .getLastCellNum()];
  for (int rownum = 0; rownum <= sheet.getLastRowNum(); rownum++) {
  // for (Cell cell : row)
  Row row = sheet.getRow(rownum);
  if (row == null) {
  continue;
  }
  String value;
  for (int cellnum = 0; cellnum <= row.getLastCellNum(); cellnum++) {
  Cell cell = row.getCell(cellnum);
  if (cell == null) {
  continue;
  } else {
  value = "";
  }
  switch (cell.getCellType()) {
  case Cell.CELL_TYPE_STRING:
  value = cell.getRichStringCellValue().getString();
  break;
  case Cell.CELL_TYPE_NUMERIC:
  if (DateUtil.isCellDateFormatted(cell)) {
  value = cell.getDateCellValue().toString();
  } else {
  value = Double.toString((int) cell
  .getNumericCellValue());
  }
  break;
  case Cell.CELL_TYPE_BOOLEAN:
  value = Boolean.toString(cell.getBooleanCellValue());
  break;
  case Cell.CELL_TYPE_FORMULA:
  value = cell.getCellFormula().toLowerCase();
  break;
  default:
  value = " ";
  System.out.println();
  }
  locatorMap[rownum][cellnum] = value;
  }
  }
  in.close();
  wb.close();
  return locatorMap;
  }
  }
  页面类
  package com.dbyl.libarary.pageAction;
  import java.io.IOException;
  import java.util.concurrent.TimeUnit;
  import org.openqa.selenium.WebDriver;
  import com.dbyl.libarary.utils.BasePage;
  import com.dbyl.libarary.utils.Locator;
  public class LoginPage extends BasePage {
  WebDriver driver;
  public WebDriver getDriver() {
  return driver;
  }
  public LoginPage(WebDriver driver) throws IOException {
  super(driver);
  driver.get("http://www.zhihu.com/#signin");
  }
  Locator loginEmailInputBox = new Locator("loginEmailInputBox");
  Locator loginPasswordInputBox = new Locator("loginPasswordInputBox");
  Locator loginButton = new Locator("loginButton");
  Locator profile = new Locator(
  "profile");
  public void typeEmailInputBox(String email) throws Exception {
  type(loginEmailInputBox, email);
  }
  public void typePasswordInputBox(String password) throws Exception {
  type(loginPasswordInputBox, password);
  }
  public void clickOnLoginButton() throws Exception {
  click(loginButton);
  }
  public boolean isPrestentProfile() throws IOException {
  return isElementPresent(profile, 20);
  }
  public void waitForPageLoad() {
  super.getDriver().manage().timeouts()
  .pageLoadTimeout(30, TimeUnit.SECONDS);
  }
  }

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
43/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号