接下来就是登陆页面的类,这个登陆页面的元素,放在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),我们将立即处理。