浅析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上查了查,觉 ...
随机推荐
- asp.net 301重定向代码
/// <summary> ///重定向代码 /// </summary> /// <param name="sender"></para ...
- 爱上MVC~图表的使用Chart
回到目录 图表在一个系统中是必须的,MVC架构把它当然是一个扩展集成了进来,通过简单的几句话就可以生成一个风格多样的图表,这给报表的开发带来了很大的方便,大叔的项目中也做了一个测试,把主要的代码贴出来 ...
- 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)
[源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...
- ZHA profile与ZLL profile的一个例子
ZHA Coordinator 如何控制ZLL Light/Philips Hue Light 缩写: ZHA: ZigBee Home Automation profile ZLL: ZigBee ...
- 《数据结构与算法Python语言描述》习题第二章第三题(python版)
ADT Rational: #定义有理数的抽象数据类型 Rational(self, int num, int den) #构造有理数num/den +(self, Rational r2) #求出本 ...
- HttpClient通过Post上传多个文件
public static String sendFilesPost(String url, String fileNames) { HttpClient httpClient = null; Htt ...
- C#中 反射中的Assembly(装载程序集):
反射中的Assembly(装载程序集):可以通过Assembly的信息来获取程序的类,实例等编程需要用到的信息. String assemblyName = @"NamespaceRef& ...
- 【IScroll深入学习】突破移动端黑暗的利器(上)
前言 在去年,我们对IScroll的源码进行了学习,并且分离出了一段代码自己使用,在使用学习过程中发现几个致命问题: ① 光标移位 ② 文本框找不到(先让文本框获取焦点,再滑动一下,输入文字便可重现) ...
- hexo博客进阶-相册和独立域名
之前我已经写了一篇文章详细的讲述了如何使用hexo搭建github博客.如果还没有看的可以去看看,hexo搭建博客 其实,根据这篇文章的过程我们就能够搭建一个专属于自己,并且非常美观的博客了.但是如果 ...
- tomcat远程调试javaweb
当把一个本地项目部署到远程服务器后有可能出现意想不到错误,这个时候通过远程调试能够更清楚的找到bug所在位置. 目前百度tomcat绝大多数方法都是在startup.sh或者catalina.sh增加 ...