浅析selenium的PageFactory模式 PageFactory初始化pageobject
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);
后者加入了初始化元素时等待时间。
PageFactory是为了支持页面设计模式而开发出来的,它的方法在selenium.support库里面。
PageFactory它提供初始化页面元素的方法,如果页面存在大量的AJAX的技术,只要页面更新一次,它就好重新查找一次元素,所以不会出现StaleElementException这个error,
如果你不想要它每次都更新,你可以加上@CacheLookup.
页面设计模式,可以提供你一个接口,然后你在这个接口上面,构建你自己项目中的页面对象,使用PageFactory使得测试更简单,更少的代码编写。
如果@FindBy没有指定,它会默认的查找id的属性,然后是name属性,如果还没有找到就会报错。 如果这个元素存在,我们不用担心它, pageFactory会初始化这个元素,不会报任何错误。
先看个列子:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BaiduSearchPage { WebDriver driver; @FindBy(id = "kw")
@CacheLookup
WebElement searchField; @FindBy(id = "su")
@CacheLookup
WebElement searchButton; public BaiduSearchPage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
} public void inputText(String search){ searchField.sendKeys(search); } public void clickButton(){ searchButton.click(); } } import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test { public static void main(String[] args) { BaiduSearchPage searchPage;
WebDriver driver =new FirefoxDriver(); driver.manage().window().maximize();
driver.get("http://www.baidu.com"); searchPage =new BaiduSearchPage(driver); searchPage.inputText("selenium");
searchPage.clickButton(); } }
我们平时写查找元素,喜欢倾向于driver.findElement(by)这种方法,但是有的时候我们需要考虑查找失败,或者AJAX的情况,但是pageFactory就不需要,这使得查找页面元素更简单,快速。
基于页面设计对象, 我们可以编写更少的代码去完成更多的测试案例。
浅析selenium的PageFactory模式 PageFactory初始化pageobject的更多相关文章
- 浅析selenium的PageFactory模式
前面的文章介绍了selenium的PO模式,见文章:http://www.cnblogs.com/qiaoyeye/p/5220827.html.下面介绍一下PageFactory模式. 1.首先介绍 ...
- Selenium之使用PageFactory初始化pageobject
使用PageFactory初始化pageobject有什么作用呢,下面举个例子来说明 public BaiduPage baiduPage = PageFactory.initElements(dri ...
- [小北De编程手记] : Lesson 08 - Selenium For C# 之 PageFactory & 团队构建
本文想跟大家分享的是Selenium对PageObject模式的支持和自动化测试团队的构建.<Selenium For C#>系列的文章写到这里已经接近尾声了,如果之前的文章你是一篇篇的读 ...
- 在Python中实现PageFactory模式
关于 PageFactory 的概念主要是Java中内置了PageFactory类. import org.openqa.selenium.support.PageFactory; …… 例子,htt ...
- 转在Python中实现PageFactory模式
转自: http://www.cnblogs.com/fnng/p/5092383.html 关于 PageFactory 的概念主要是Java中内置了PageFactory类. import org ...
- 浅析selenium的page object模式
selenium目前比较流行的设计模式就是page object,那么到底什么是page object呢,简单来说,就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好 ...
- Selenium系列(二十) - PageObject模式的详细介绍
如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...
- selenium page object模式
页面对象模式将测试代码和被测试页面的元素及操作进行分离,以降低页面元素的变化对测试代码的影响.每个被测试的页面都会被定义一个类,类中会定位元素和操作. 如果不使用page object模式,则相同的操 ...
- Selenium的PO模式(Page Object Model)[python版]
Page Object Model 简称POM 普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...
随机推荐
- 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...
- jquery设置cors跨域
老版写法 $.support.cors = true; 新版写法 crossDomain: true
- c#后台List转为js对象(在前台操作)
<!-- 用html标签防止html转义 (html.raw方法转双引号中的双引号时不会转义) --><input id="json" type="hi ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【Debian】ftp安装
http://www.2cto.com/os/201107/98311.html http://jingyan.baidu.com/article/adc815133476bdf723bf7393.h ...
- MathType与Office公式编辑器有什么不同
说到在论文中编辑公式,有经验的人都会知道要用公式编辑器来编辑,没经验的人也会被安利使用公式编辑器.然而在使用公式编辑器时,又有了两种选择,一种是使用Office自带的公式编辑器,一种是MathType ...
- yii 常用orm
yii2 orwhere andwhere的复杂写法:https://www.codercto.com/a/6513.html $files = XXXX::find() ->andWhere( ...
- shrink-to-fit(自适应宽度)
自适应宽度是指当未明白设定容器的宽度(或外边距设为auto)时,在特定的情况下容器的宽度会依据情况自行设定.而设定的结果往往并非我们想要的. W3C规范中描写叙述了几种shrink-to-fit的情况 ...
- 编程之美 set 3 最大公约数问题
解法 1. f(x,y) = f(y, y%x) (y>0) 辗转相除法 2. 取模运算较为耗时, 将取模变成相减. 但对极端数据效果很差, 比如 gcd(1000,1) 3. 分析公约数的特点 ...
- sys.argv 详细用法
sys.argv 用于获取命令行参数,用法如下: [root@localhost ~]$ cat 1.py #!/usr/bin/env python #-*- coding:utf-8 -*- im ...