selenium测试框架篇
2016-05-31 11:27
239 查看
做自动化框架,不可避免的就是对象库。
有一个好的对象库,可以让整个测试体系:
更容易维护
大大增加代码重用
增加测试系统的稳定性
这里先了解一下我所说的对象库:
页面类
接下来就是登陆的Action
至此为止,已经封装完毕
接下来就能在测试用例直接调用者
demo的下载地址:https://github.com/tobecrazy/Demo
有一个好的对象库,可以让整个测试体系:
更容易维护
大大增加代码重用
增加测试系统的稳定性
这里先了解一下我所说的对象库:
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); } }
接下来就是登陆的Action
package com.dbyl.libarary.action; import org.openqa.selenium.WebDriver; import org.testng.Assert; import com.dbyl.libarary.pageAction.HomePage; import com.dbyl.libarary.pageAction.LoginPage; public class CommonLogin { private static WebDriver driver; public static WebDriver getDriver() { return driver; } static LoginPage loginPage; 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 new HomePage(getDriver()); } public static HomePage login() throws Exception { return CommonLogin.login("seleniumcookies@126.com", "cookies123"); } public static void setDriver(WebDriver driver) { CommonLogin.driver = driver; } }
至此为止,已经封装完毕
接下来就能在测试用例直接调用者
demo的下载地址:https://github.com/tobecrazy/Demo
相关文章推荐
- 河南第五届省赛 B 最强DE 战斗力 -- (大数乘法 c++ OR java)
- 数据库 数据仓库 概念模型 逻辑模型 物理模型
- hbase中报错:java.io.IOException: Connection reset by peer at sun.nio.ch.FileDispatcher.read0(Native
- 二叉排序树(BST)的思路及C语言实现
- 临时挂载USBwiif的驱动指令
- 恢复丢失的VMFS5分区-适用于esxi5
- 【Get深一度】CButtonST v3.9 (MFC Flat buttons)
- 跨境电商进口业务的通关模式
- linux新手常用命令(系列二)
- ImportError No module named MySQLdb
- div布局 position
- Android 系统默认参数的修改
- php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案
- java后台生成登录验证码
- YSlow的使用,以及Yahoo军规的几条定律
- PHP程序员如何理解IoC/DI
- C++延时函数
- linux新手常用命令(系列一)
- 插入排序
- 史上最完整跨境电商投资分析报告!