浅析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上查了查,觉 ...
随机推荐
- Redis所需内存 超过可用内存怎么办
爬虫和转载请注明原文地址:博客园蜗牛 http://www.cnblogs.com/tdws/p/5727633.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...
- 零基础如何学习java更有效呢?
零基础学java,不知道该如何入手?也不知道学习的方向,很多人会问零基础怎么样学习,有没有什么入门的书籍推荐:只要方法正确,零基础学好java也是有机会的哦. 一.理解Java思想 Java是一门面向 ...
- JVM-String比较-字节码分析
一道String字符串比较问题引发的字节码分析 public class a { public static void main(String[] args)throws Exception{ } p ...
- Javaweb——过滤器映射
什么是过滤器? 过滤器:从字面上看,可以理解为将具有杂质的水过滤,留下干净的水.那么从IT的角度上理解.过滤器:是处在源数据(数据库之类的)和目标数据(显示页面)的中间组件.对于Web应用来说,过滤器 ...
- 网页端实现input数字输入框
实现input输入框只能输入数字的效果: <input type="text" name="" id="phoneNum" value ...
- 【转】iOS夯实:ARC时代的内存管理
iOS夯实:ARC时代的内存管理 什么是ARC Automatic Reference Counting (ARC) is a compiler feature that provides autom ...
- IOS开发基础知识--碎片6
三十三:IOS多视图跳转方法 第一种: 跳转:[self presentModalViewController:control animated:YES]; 返回:[self dismissModal ...
- IOS开发之Bug--iOS7View被导航栏遮挡问题的解决
在实际开发中,遇到在UITextView的frame等于当前控制器的View的frame的情况下,然后运行的时候,发现控制器的Frame的高度y值会从导航条的位置64变化到0. 导致UITextVie ...
- fragment 监听返回
@Override public void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getVie ...
- C#命名规则和编码规范
用Pascal规则来命名属性.方法.事件和类名. public class HelloWorld { public void SayHello(string name) { } } Pascal规则是 ...