一、问题的提出: 51Testing软件测试网'Eg.G
k%b^q
使用selenium做自动化测试的一般步骤为:
5c#ly)b'al01) 通过By.id、name、xpath等方法定位页面元素51Testing软件测试网Y\Er-r
2) 对定位到的页面元素执行相应的操作
$z5Y$@Q/\SX)r _03) 对操作后出现的结果和预期结果做一个比较
bS)]?\M(e0 51Testing软件测试网7]#]Z_0i5Q
dJ3d
使用webdriver做过一段时间的测试就会发现一个对某一个页面的元素进行定位的时候,程序行间充斥着id()、name()、xpath()等方法,这样会造成测试程序的可读性较差,不便于后期的维护以及修改。
.n*o`
x0n
t0虽然我们可以通过添加注释的方法使程序便于理解,但是还是不可以从根本上解决这种问题。我们可以通过对这些方法进行二次封装来避免每次对这些方法的直接调用,通过方法的封装虽然可以实现复用。但是我们发现通过封装无法实现页面元素的逻辑处理和测试数据的独立。51Testing软件测试网)ML1fP
p
不断地添加新的测试,而极少地去重构、利用原有测试。Selenium的执行速度比较慢(相对单元测试),随着测试逐渐的增多,运行时间会逐渐增加到不可忍受的程度。
$e*O[a3N0思考过之后才知道,直接调用selenium提供的API方法或者对selenium的API进行二次封装后的调用,我们无非是通过调用这些方法来实现对测试页面细节程度的把控,可是真正的逻辑部分我们却未曾独立出来。51Testing软件测试网3Ay%~ YL!x&M
二、问题的解决办法:51Testing软件测试网)w
M;u8i.R;Ns,FT4O:_5q
Page Object模式51Testing软件测试网e6plL?rs
Page
Object将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理。
4L*t[*]P_rb
r01、例如没有使用Page
Object模式时对于163邮箱的登录操作代码如下:51Testing软件测试网(}nhsf5t.|.rEa
packagecom.test;
/aT1f&Y$C0
4b4[8_W.[M's'K0importorg.openqa.selenium.By;
,hz"V[-T0importorg.openqa.selenium.WebDriver;
(`2Yq{nR4r0importorg.openqa.selenium.WebElement;
4BM[D A;n$g%D0importorg.openqa.selenium.firefox.FirefoxDriver;51Testing软件测试网
glb4v-Z&f!a?+u
uc[E
^+j3k'S0publicclasstest163 {51Testing软件测试网Urp7a+e@6S7^7P
U)C
publicstaticvoidmain(String[] args)51Testing软件测试网U.P)[i&S,FQ
{
8Ic6R q gW;O4U*jf0 //启动浏览器,进入163邮箱首页51Testing软件测试网;e&Gi T7a1GR.m8y2]
WebDriver driver =newFirefoxDriver();51Testing软件测试网8e z6T6iUj
driver.get("http://mail.163.com/");
7Y$Q~rcM$B;K~5s0IL0
#qZ#SRk6KJR0 Thread.sleep(5000);
1|&~IB5x0 //输入用户名密码,登录邮箱51Testing软件测试网'R k)Q:K)I
WebElement youxiangzhanghao_element = driver.findElement(By.id("idInput"));51Testing软件测试网;U
}z+G|M%F
youxiangzhanghao_element.clear();
X4cT(d#N0
L)rdkWv0 youxiangzhanghao_element.sendKeys("justForYourTesting");
SJD
h'j6DLXBHg0
}
[Py!Nvs:]5pSjY0 WebElement mima_element =
driver.findElement(By.id("pwdInput"));
}4LN)e]\0 mima_element.sendKeys("135135");
'Qp!^2Wo)g0 51Testing软件测试网1X-|.m.}"L:^C
WebElement denglu_element =
driver.findElement(By.id("loginBtn"));51Testing软件测试网,\0BJ Gs5fY0{LP/F
Q
denglu_element.click();51Testing软件测试网`'P__"d\
51Testing软件测试网L-EA
t:Gj-G
c1T4R N
Thread.sleep(10000);51Testing软件测试网"m#XFY @v6K
51Testing软件测试网A*?~+OAj
driver.close();51Testing软件测试网,FY!y3Q1b||p
}
(nzb/Y
r*m`Z1A0}51Testing软件测试网-\w{KC&F
2、使用FindBy注解51Testing软件测试网I/Z0M C'H
package
com.mail163;
~x]NX6x1D(v b)L0 51Testing软件测试网T2m b1UPu.c
import
org.openqa.selenium.WebDriver;
C4f5DM1m]lj9|+{0import
org.openqa.selenium.WebElement;51Testing软件测试网%\(v[C\#m8e w
import
org.openqa.selenium.support.FindBy;51Testing软件测试网:S9va-x*A
U@
m d1Y#k0public
class mail163 {
2k prD3{5F+Nu0 @FindBy(id="idInput" )
G8B1yoqX
p0 private WebElement username;51Testing软件测试网a6N+qazG,lw
51Testing软件测试网0G#`7_ FdV
ZT#nc[
@FindBy(id="pwdInput" )51Testing软件测试网wT&C2P Vl
private WebElement password;51Testing软件测试网j6hR
OH6h3ef
51Testing软件测试网L@a!U(U7t/R(Xm
@FindBy(id="loginBtn" )51Testing软件测试网5I(|}-~!Za
private WebElement loginBtn;
OvG)cR$f!LS0 51Testing软件测试网8[SW?a"vH*Ui/a
public void login(WebDriver dr,String
username,String pwd){
YS$d;Y"Cd C:Cz9b7qL]j0 dr.get("http://mail.163.com");51Testing软件测试网Vp%`j \0a5Y
this.username.sendKeys(username);
.Gc\7{7a$k#k0o0 this.password.sendKeys(password);51Testing软件测试网lWu {Xu4lqT4Z
loginBtn.click();51Testing软件测试网2Kj_rO`
}
,J8H6NwMNk*s0}51Testing软件测试网W,U|'RP'`m;~-Ok
通过FindBy每一个页面元素都被定义为一个类中的私有变量,通过调用login()方法即可实现登陆页面的登录操作。
H gq#o A X]0 通过对比1、2明显可以看出,2的逻辑结果比1更加简洁,以每个page为单位(类),以每个page元素为类的属性,以方法login()来实现每个页面元素操作的封装。程序的逻辑独立性更强,也方便后期的维护和修改。
;p/dmWec4I,x{03、对Login类的login方法的初始化
S%[hM%y2LI0package com.test.java;51Testing软件测试网|kF7nD,Z!x
51Testing软件测试网.[qL8Fco'xR
eN
import org.openqa.selenium.WebDriver;
3b6Tq&ZZOCK0import org.openqa.selenium.firefox.FirefoxDriver;51Testing软件测试网;F+tz"U&y*tb}
import org.openqa.selenium.support.PageFactory;
6lOC
OuWZ0^0
dSwR2Q7oN0public class testLogin3 {
otB'GwC0 public void login1(String username, String password)51Testing软件测试网W
Vt
H,A5v w
{51Testing软件测试网zzJ q
x
WebDriver driver= new FirefoxDriver();
En~ld l
]1B0 // 对页面元素的初始化51Testing软件测试网#?:W)JR E[qK0@
login m=PageFactory.initElements(driver, login.class);51Testing软件测试网s*Kv"_WDst
m.login(driver, username, password);
"zG2]J?-o0 }
w"FX9}6R"L+@q4l0
m2M[%J)y?o4k
T:b0 public static void main(String[] args)
(cX(W5Iu7XI0 {51Testing软件测试网#ISl.lGLh3X%tQ(e
testLogin3 tl = new testLogin3();51Testing软件测试网|,Ja-[&pw`CQO
tl.login1("justForYourTesting","135135");
\^i,yP^IzP0 }
)yw#PY`OOr0}51Testing软件测试网
]MQe#VBa
Pp't"K$UMeQr0