一、问题的提出: 51Testing软件测试网t/}CM4bp@
使用selenium做自动化测试的一般步骤为:51Testing软件测试网1W.g*B6{8h
1) 通过By.id、name、xpath等方法定位页面元素
#w1Q @!YT02) 对定位到的页面元素执行相应的操作51Testing软件测试网@"}Q*I+X
K)_ St
3) 对操作后出现的结果和预期结果做一个比较51Testing软件测试网Ht4\7~.Q"K
G*m VE9m4n-Wai0 使用webdriver做过一段时间的测试就会发现一个对某一个页面的元素进行定位的时候,程序行间充斥着id()、name()、xpath()等方法,这样会造成测试程序的可读性较差,不便于后期的维护以及修改。51Testing软件测试网UzD8Znv
虽然我们可以通过添加注释的方法使程序便于理解,但是还是不可以从根本上解决这种问题。我们可以通过对这些方法进行二次封装来避免每次对这些方法的直接调用,通过方法的封装虽然可以实现复用。但是我们发现通过封装无法实现页面元素的逻辑处理和测试数据的独立。51Testing软件测试网.b|i
TO
@
不断地添加新的测试,而极少地去重构、利用原有测试。Selenium的执行速度比较慢(相对单元测试),随着测试逐渐的增多,运行时间会逐渐增加到不可忍受的程度。51Testing软件测试网&B7M4T+WU]x0]
思考过之后才知道,直接调用selenium提供的API方法或者对selenium的API进行二次封装后的调用,我们无非是通过调用这些方法来实现对测试页面细节程度的把控,可是真正的逻辑部分我们却未曾独立出来。51Testing软件测试网t'|,\Zf/tW(Y$z` ^
二、问题的解决办法:
1Ga|]c#z3BF4w0Page Object模式51Testing软件测试网-xXAz8u-j6l5`6]
Page
Object将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理。
#Gn(tH
wVd Rx01、例如没有使用Page
Object模式时对于163邮箱的登录操作代码如下:51Testing软件测试网Aq{Ou$k
packagecom.test;
oYf5{Kam0 51Testing软件测试网s9_g&Q%C6q
importorg.openqa.selenium.By;51Testing软件测试网6h+lE^5[-\JFlc%K
importorg.openqa.selenium.WebDriver;51Testing软件测试网S:g${K\/oM
importorg.openqa.selenium.WebElement;
S9D%F:jk6K)c0importorg.openqa.selenium.firefox.FirefoxDriver;
6EEl
Gu5_d0
%L2i'U_[O0publicclasstest163 {51Testing软件测试网}ON:A}n
publicstaticvoidmain(String[] args)
#n:Y&N
K}~v!c8g0 {
_O} GopT0 //启动浏览器,进入163邮箱首页
&ao|.^9I:N4Z5w7}0 WebDriver driver =newFirefoxDriver();
%J0n[6I Ft0 driver.get("http://mail.163.com/");
#e xh"o8Ux^ eF+m0
Ta$bqkEd2NQ5f0}3R#C0 Thread.sleep(5000);
{
w
NXK&e!ln5Q0 //输入用户名密码,登录邮箱
({ [$_7]"RW&g"n.J0 WebElement youxiangzhanghao_element = driver.findElement(By.id("idInput"));
X6lE_`0 youxiangzhanghao_element.clear();
:k }.ms;E0
:t6{:NAC;^0 youxiangzhanghao_element.sendKeys("justForYourTesting");
D*PE:n-Fe$\M0 51Testing软件测试网}T1h"|.eW
WebElement mima_element =
driver.findElement(By.id("pwdInput"));
/e7bn5N/t4P;{Hma Q1l)G0 mima_element.sendKeys("135135");51Testing软件测试网'ov5b m!c0[ ]$g
51Testing软件测试网 O.e:lu,Y5]8I4S{\
WebElement denglu_element =
driver.findElement(By.id("loginBtn"));
&w'?7i,hz0 denglu_element.click();
nw+z$mow0
L;o.N9Ag
M0 Thread.sleep(10000);
GT)} vE0
Xy3a'~:h.n~0 driver.close();
T|2HQ'N(tt-O1dT{0 }
e~3E&a0dr0}51Testing软件测试网C.W$V^X`(\s*g
2、使用FindBy注解51Testing软件测试网%K FJU|
package
com.mail163;
]]I%`
^3K,F!`^0
z"}{A8P
w$L K|F0import
org.openqa.selenium.WebDriver;51Testing软件测试网4V9bRqN+uV-]rZ
import
org.openqa.selenium.WebElement;
-`
n;Tt1H`b0~0import
org.openqa.selenium.support.FindBy;51Testing软件测试网*@Eyf\5@3MT)t
X-`(M
]7rj0public
class mail163 {
$I]\
Ou"g6?&c0 @FindBy(id="idInput" )
wWQ3Hb,A:A;R0 private WebElement username;51Testing软件测试网N6O.s+X{0}%FQ
.q3F0`
hSMi0 @FindBy(id="pwdInput" )
:t,Lj%Q*l+`I0 private WebElement password;51Testing软件测试网Gq&[LSt'x
? y
l
F DH9}d
pQLh,do0 @FindBy(id="loginBtn" )51Testing软件测试网B5V!`5]4o d|6X-j
private WebElement loginBtn;51Testing软件测试网%Q.p+?Z;Zy4|
,aQDv4EY)Es+i0 public void login(WebDriver dr,String
username,String pwd){
M8JZ)H+v
v0 dr.get("http://mail.163.com");
K#U]k.R+OXc3M'Z0 this.username.sendKeys(username);51Testing软件测试网 vbqR Z-E2L,m\o
R
this.password.sendKeys(password);51Testing软件测试网w1]?r A2O
i^
loginBtn.click();51Testing软件测试网/r+m"a&Ol2s
o4L
] e0]
}51Testing软件测试网SL7K6mFF$c
}
*_-QT9d'_-Em"T0通过FindBy每一个页面元素都被定义为一个类中的私有变量,通过调用login()方法即可实现登陆页面的登录操作。51Testing软件测试网3QA:po]C_.}dx
通过对比1、2明显可以看出,2的逻辑结果比1更加简洁,以每个page为单位(类),以每个page元素为类的属性,以方法login()来实现每个页面元素操作的封装。程序的逻辑独立性更强,也方便后期的维护和修改。
j"Zm#feC{03、对Login类的login方法的初始化
kA"w!_Qd+G0package com.test.java;51Testing软件测试网{Y
q,]e;Tlk
51Testing软件测试网A'MV6`Y%E!F
import org.openqa.selenium.WebDriver;
iA
lq._:@*SRM+qQ0import org.openqa.selenium.firefox.FirefoxDriver;51Testing软件测试网I#rR.K)r8LJ
import org.openqa.selenium.support.PageFactory;
]-kO*toP7@2w!Z(a0
cqzQB-r9Yo0public class testLogin3 {51Testing软件测试网D?eMfO
public void login1(String username, String password)51Testing软件测试网#qlnTx
kL,azy
{
2wpo c4v9? l)c0 WebDriver driver= new FirefoxDriver();51Testing软件测试网$d H4\B8Y+Ki
// 对页面元素的初始化
,_2zCb^;sP0 login m=PageFactory.initElements(driver, login.class);51Testing软件测试网4[7s#x {@5K
m.login(driver, username, password);51Testing软件测试网v|*?~V4S4w
}51Testing软件测试网s-i @;t+on({E5zc
51Testing软件测试网3E(X:XicK)z j
public static void main(String[] args)51Testing软件测试网D_9rA u
OXU$tC1a CY
{51Testing软件测试网4nq mu4I#pA
testLogin3 tl = new testLogin3();51Testing软件测试网8b6]S)S4@+s'hKO
tl.login1("justForYourTesting","135135");51Testing软件测试网 e#y A]H6]t D@
}51Testing软件测试网6Vc#OJ;WG
}51Testing软件测试网#O!M
b)l+R}2uH
}1t-i
f(w"`;|`0