前期已经做好使用Jenkins做buildhttp://www.cnblogs.com/tobecrazy/p/4529399.html

做自动化框架,不可避免的就是对象库。

有一个好的对象库,可以让整个测试体系:

  • 更容易维护
  • 大大增加代码重用
  • 增加测试系统的稳定性

这里先了解一下我所说的对象库:

所谓的页面对象,是指每一个真是的页面是一个对象。

比如zhihu的登陆页面是一个页面对象,http://www.zhihu.com/#signin

这个页面对象主要包含一个输入邮箱的输入框(一个元素对象),一个输入密码的密码框

一个登陆框。当然,zhihu不止一个页面,有无数页面,每一个页面都可以封装为一个对象。而每个

页面的元素,也可以封装成一个个元素对象。

为什么要封装成一个个对象?

还是以这个登陆页面为例,如果有一天zhihu改版,登陆界面UI变了,(但是需要输入用户名和密码还有登陆按钮不会消失吧)。

登陆页面的元素的位置也相应改变,如果你的测试用例没有封装过页面和元素, 每个页面都是拿webdriver 直接写,页面元素定位

也分布到测试用例中,这要维护起来要全部改掉测试用例。如果你封装了页面,封装了元素,再封装一个对应的登陆Action,你的每个

测试用例是调用的login.action()。  这样,你只需要改变你对象库的内容就完美解决UI变化,而不必一个个修改测试用例。

测试框架目录如下:

接下来一这个登陆为例:

首先封装一个BasePage的类,毕竟所有的页面都有共同的东西,每个页面都有元素,每个页面元素都有相应的方法

这里简单封装了几个方法,如type

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;
} }

接下来就是登陆页面的类,这个登陆页面的元素,放在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);
} }

接下来就是登陆的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;
} }

至此为止,已经封装完毕

接下来就能在测试用例直接调用者

package com.dbyl.tests;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import com.dbyl.libarary.action.ViewHomePage;
import com.dbyl.libarary.utils.DriverFactory;
import com.dbyl.libarary.utils.UITest; public class loginTest extends UITest{ WebDriver driver=DriverFactory.getChromeDriver();
@BeforeMethod(alwaysRun=true)
public void init()
{
super.init(driver);
ViewHomePage.setDriver(driver);
//CommonLogin.setDriver(driver);
}
@Test(groups="loginTest")
public void loginByUerName() throws Exception
{
//CommonLogin.login("seleniumcookies@126.com","cookies123");
ViewHomePage.viewMyProfile();
} @AfterMethod(alwaysRun=true)
public void stop() {
super.stop();
} }

  

demo的下载地址:https://github.com/tobecrazy/Demo

selenium测试框架篇,页面对象和元素对象的管理的更多相关文章

  1. selenium测试框架篇

    做自动化框架,不可避免的就是对象库. 有一个好的对象库,可以让整个测试体系: 更容易维护 大大增加代码重用 增加测试系统的稳定性 这里先了解一下我所说的对象库: 所谓的页面对象,是指每一个真是的页面是 ...

  2. selenium测试框架使用xml作为对象库

    之前已经写过一篇: selenium测试框架篇,页面对象和元素对象的管理 上次使用的excel作为Locator对象管理,由于excel处理不够方便,有以下缺点: 不能实现分page 加载Locato ...

  3. selenium怎么操作web页面常见的元素

    总结一下selenium怎么操作web页面常见的元素. 主要有: 上传 alter dialog prompt dialog confirm dialog select list radio box ...

  4. selenium 测试框架中使用grid

    之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...

  5. 『心善渊』Selenium3.0基础 — 2、Selenium测试框架环境搭建(Windows)

    目录 1.浏览器安装 2.浏览器驱动下载 (1)ChromeDriver for Chrome (2)Geckodriver for Firefox (3)IEDriverServer for IE ...

  6. Python Web框架篇:Django Model ORM(对象关系映射)

    一,基本操作 用于实现面向对象编程语言里不同类型系统的数据之间的转换,换言之,就是用面向对象的方式去操作数据库的创建表以及增删改查等操作. 1.增(create , save): from app01 ...

  7. Selenium自动化测试框架Ride使用XLRD对于Excel测试数据的管理和操作

      Python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 一.安装xlrd模块 到python官网下载http://pypi.pytho ...

  8. 《Selenium自动化测试实战:基于Python》Selenium自动化测试框架入门

    第1章  Selenium自动化测试框架入门 1.1  Selenium自动化测试框架概述 说到目前流行的自动化测试工具,相信只要做过软件测试相关工作,就一定听说过Selenium. 图1-1是某企业 ...

  9. HTML DOM Document对象 元素对象 属性对象 事件对象

    DOM Document对象 DOM 元素 对象 DOM 属性 对象 DOM 事件 菜鸟教程上 总结挺全的,就不多废话,链接点进去即可.. 后期对经常用到的会在此更新一些总结..... 开学了...自 ...

随机推荐

  1. Silverlight 后台设置 button 纯色背景

    silverlight Button直接设置其background为某一颜色往往达不到效果.因为其内置模板把按钮背景弄成一个渐变画刷.所以想要纯色的背景就修改其模板. 在后台修改模板的代码如下: St ...

  2. 使用Eclipse进行远程调试

    转自:http://blog.csdn.net/sunyujia/article/details/2614614 今天决定做件有意义的事,写篇图文并茂的blog,为什么要图文并茂?因为很多事可能用语言 ...

  3. Android Studio解决未识别Java文件(出现红J)问题

    1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...

  4. AnjularJS系列3 —— 数据的双向绑定

    第三篇,双向的数据绑定 数据绑定是AnguarJS的特性之一,避免书写大量的初始代码从而节约开发时间 数据绑定指令提供了你的Model投射到view的方法.这些投射可以无缝的,毫不影响的应用到web应 ...

  5. [Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能

    很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化&l ...

  6. 通过SmartGit把java maven项目传到码云

    一.首先先在码云上新建一个项目 二.复制项目的链接 三.打开SmartGit,点击clone 4.把复制的项目链接粘上去 5.然后点两次next,选择一个路径,finish 6.打开刚刚选择的路径,我 ...

  7. C#------数字转中文

    转载: http://www.jb51.net/article/8061.htm 方法二: static string ConvertToChinese(double x) { string s = ...

  8. RabbitMQ学习系列(二): RabbitMQ安装与配置

    上一篇,简单介绍了RabbitMQ的情况还有一些相关的概念,这一篇,会讲讲 RabbitMQ安装与配置. 1.安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitM ...

  9. 【先定一个小目标】怎么解决mysql不允许远程连接的错误

    最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to ...

  10. Java集合之ArrayList

    ArrayList ArrayList是最常见以及每个Java开发者最熟悉的集合类了,顾名思义,ArrayList就是一个以数组形式实现的集合,以一张表格来看一下ArrayList里面有哪些基本的元素 ...