浅析selenium的PageFactory模式
前面的文章介绍了selenium的PO模式,见文章:http://www.cnblogs.com/qiaoyeye/p/5220827.html。下面介绍一下PageFactory模式。
1.首先介绍FindBy类:
For example, these two annotations point to the same element:
@FindBy(id = "foobar") WebElement foobar;
@FindBy(how = How.ID, using = "foobar") WebElement foobar;
and these two annotations point to the same list of elements:
@FindBy(tagName = "a") List<WebElement> links;
@FindBy(how = How.TAG_NAME, using = "a") List<WebElement> links;
用来分别查找单个元素和多个元素的两种用法,支持的类型有:className、css、id、linkText、name、partialLinkText、tagName、xpath。
How支持的类型和上面差不多。
2.接着介绍PageFactory类
Factory class to make using Page Objects simpler and easier.
它提供的方法都是静态的,可以直接调用,我们在用完findby后,需要进行元素初始化,则需要调用下面的方法
initElements(ElementLocatorFactory factory, java.lang.Object page)、initElements(FieldDecorator decorator, java.lang.Object page)、initElements(WebDriver driver, java.lang.Class<T> pageClassToProxy)、initElements(WebDriver driver, java.lang.Object page)
我们在实际使用中可以这样用:
PageFactory.initElements(dr, XXX.class);
或者
PageFactory.initElements(new AjaxElementLocatorFactory(dr, 10) ,XXX.class);
后者加入了初始化元素时等待时间。
3.在代码中如何设计
a.新建一个BasePage类
class BasePage {
WebDriver dr;
private final int TIMEOUT = 10;
BasePage() {}
BasePage(WebDriver dr) {
this.dr = dr;
PageFactory.initElements(new AjaxElementLocatorFactory(dr, TIMEOUT) , this);
}
BasePage(WebDriver dr, final String title) {
this.dr = dr;
//如果不进行判断,
WebDriverWait wait = new WebDriverWait(dr,TIMEOUT);
try{
boolean flag = wait.until(new ExpectedCondition<Boolean>(){
@Override
public Boolean apply(WebDriver arg0) {
// TODO Auto-generated method stub
String acttitle = arg0.getTitle();
return acttitle.equals(title);
}});
}catch(TimeoutException te) {
throw new IllegalStateException("当前不是预期页面,当前页面title是:" + dr.getTitle());
}
PageFactory.initElements(new AjaxElementLocatorFactory(dr, TIMEOUT) , this);
}
void fun2() {}
void fun3() {}
}
b.建两个页面类LoginPage和HomePage,分别继承BasePage
class LoginPage extends BasePage{
@FindBy(id="UserName")
@CacheLookup //加入缓存,更新值的时候先从缓存中取
private WebElement input_username;
@FindBy(css="html body div.loginbox div#NoCodeLogin.formbox.errorBox div#ElongLogin div#password_tip.inputbox.login_pw input.blur")
@CacheLookup
private WebElement temp_input_password;
@FindBy(id="PassWord")
@CacheLookup
private WebElement input_password;
//WebDriver dr;
/*
@FindBy(name="captcha")
@CacheLookup
WebElement input_imgcode;
@FindBy(css="html.show-app-promotion-bar.cssanimations.csstransforms.csstransitions.flexbox.no-touchevents.no-mobile body.zhi.no-auth div.index-main div.index-main-body div.desk-front.sign-flow.clearfix.sign-flow-simple div.view.view-signin form div.group-inputs div.input-wrapper.captcha-module div.captcha-container img.js-refresh-captcha.captcha")
@CacheLookup
WebElement imgcode;*/
@FindBy(className="loginbtn")
@CacheLookup
private WebElement button_submit;
LoginPage(WebDriver dr) {
super(dr);
}
LoginPage(WebDriver dr, String titile) {
super(dr, titile);
}
HomePage login() {
typeusername("0000");
typepassword("111111");
button_submit.click();
return new HomePage(dr,"艺龙机票】机票查询,特价机票,打折飞机票-艺龙旅行网eLong.com");
}
void typeusername(String name) {
input_username.clear();
input_username.sendKeys(name);
}
void typepassword(String password) {
temp_input_password.click();
input_password.clear();
input_password.sendKeys(password);
}
void formsubmit() {
}
}
class HomePage extends BasePage {
HomePage(WebDriver dr) {
super(dr);
// TODO Auto-generated constructor stub
}
public HomePage(WebDriver dr, String title) {
// TODO Auto-generated constructor stub
super(dr, title);
}
@FindBy(id="btnSearch")
private WebElement btn_search;
}
c.建一个测试类,做为入口
package com.test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* @author qiaojiafei
* @version 创建时间:2016年4月22日 下午1:26:59
* 类说明
*/
public class TestFactory {
public static void main(String args[]) {
WebDriver dr = new SafariDriver();
dr.get("https://secure.elong.com/passport/login_cn.html");
String logintitle = "会员登录–elong.com艺龙旅行网";
LoginPage lp = new LoginPage(dr, logintitle);
HomePage hp = lp.login();
}
}
到底为止结束,在每个页面的构造方法,加入页面title的验证,是为了保证跳转的页面与预期的页面一致后,再进行页面元素的初始化,否则,页面都没跳转正确,就进行元素初始化时多余的。

浅析selenium的PageFactory模式的更多相关文章
- 浅析selenium的PageFactory模式 PageFactory初始化pageobject
1.首先介绍FindBy类: For example, these two annotations point to the same element: @FindBy(id = "foob ...
- 在Python中实现PageFactory模式
关于 PageFactory 的概念主要是Java中内置了PageFactory类. import org.openqa.selenium.support.PageFactory; …… 例子,htt ...
- selenium page object模式
页面对象模式将测试代码和被测试页面的元素及操作进行分离,以降低页面元素的变化对测试代码的影响.每个被测试的页面都会被定义一个类,类中会定位元素和操作. 如果不使用page object模式,则相同的操 ...
- 转在Python中实现PageFactory模式
转自: http://www.cnblogs.com/fnng/p/5092383.html 关于 PageFactory 的概念主要是Java中内置了PageFactory类. import org ...
- Selenium+java - PageFactory设计模式
前言 上一小节我们已经学习了Page Object设计模式,优势很明显,能更好的体现java的面向对象思想和封装特性.但同时也存在一些不足之处,那就是随着这种模式使用,随着元素定位获取,元素定位与页面 ...
- 记我的第二次自动化尝试——selenium+pageobject+pagefactory实现自动化下单、退款、撤销回归测试
需求: 系统需要做下单.退款.撤销的回归测试,有下单页面,所以就想到用selenium做WEB UI 自动化 项目目录结构: common包上放通用的工具类方法和浏览器操作方法 pageobject包 ...
- 浅析selenium的page object模式
selenium目前比较流行的设计模式就是page object,那么到底什么是page object呢,简单来说,就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好 ...
- Selenium的PageFactory在大型项目中的应用
出路出路,走出去了,总是会有路的:困难苦难,困在家里就是难. 因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手 ...
- Selenium的PageFactory & PageObject 在大型项目中的应用
因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手动的操作步骤去转换,近日看到一个文章,又去wiki上查了查,觉 ...
随机推荐
- HTTP文件断点续传的原理
前几天一个同事跑过来找我说,我们在广告素材视频这块想做断点续传,就是这次某个视频缓存到一半,下次不用重头开始,可以在原来停留得位置开始继续下载.以提供更好的用户体验. 同时说需要我们支持吐素材地址的业 ...
- C#基础-邮件发送
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Windows Server 2012 R2在线安装.NET Framework3.5
Windows Server 2012 (R2) 默认没有安装 .NET Framework 3.5,但可以通过在线安装或指定备用源路径方式. 之前在这个 在Win Server 2012中安装.NE ...
- javascript 百度地图API - demo
<!DOCTYPE html> <html> <head> <meta name="viewport" content="ini ...
- Java实现Mysql数据库自动备份
最近项目中有一个这样的需求,要求定时备份数据库.看了一些网上的资料,了解到主要思路是是使用java中的Runtime类的exec()方法,可以直接调用windows的cmd命令,参数就是Mysql的备 ...
- 从头开始搭建分布式日志平台的docker环境
上篇(spring mvc+ELK从头开始搭建日志平台)分享了从头开始搭建基于spring mvc+redis+logback+logstash+elasticsearch+kibana的分布式日志平 ...
- 第一个web框架tornado
简介 tornado,是我学到的第一个web框架是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google ...
- 2-1 git合并 打tag
tag常用命令(仅供参考): git tag -a V1.4 -m "1.4" 创建一个tag git tag 查看本地tag git tag -d V1.1 删除本地tag g ...
- [Javascript]利用当前时间生成yyyymmddhhmmss这样的字符串
function pad2(n) { return n < 10 ? '0' + n : n } function generateTimeReqestNumber() { var date = ...
- Java Web中请求转发和请求包含
1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...