PageObject模式实现页面元素分离

上一篇 / 下一篇  2013-01-05 09:20:40 / 个人分类:Selenium

 一、问题的提出: 51Testing软件测试网t/}CM4b p@

 使用selenium自动化测试的一般步骤为:51Testing软件测试网1W.g*B6{8h

1) 通过By.idnamexpath等方法定位页面元素

#w1Q@!Y T0

2) 对定位到的页面元素执行相应的操作51Testing软件测试网@"}Q*I+X K)_ St

3) 对操作后出现的结果和预期结果做一个比较51Testing软件测试网Ht4\7~.Q"K

 

G*m VE9m4n-Wai0

  使用webdriver做过一段时间的测试就会发现一个对某一个页面的元素进行定位的时候,程序行间充斥着id()name()xpath()等方法,这样会造成测试程序的可读性较差,不便于后期的维护以及修改。51Testing软件测试网U zD8Znv

虽然我们可以通过添加注释的方法使程序便于理解,但是还是不可以从根本上解决这种问题。我们可以通过对这些方法进行二次封装来避免每次对这些方法的直接调用,通过方法的封装虽然可以实现复用。但是我们发现通过封装无法实现页面元素的逻辑处理和测试数据的独立。51Testing软件测试网.b|i TO @

不断地添加新的测试,而极少地去重构、利用原有测试。Selenium的执行速度比较慢(相对单元测试),随着测试逐渐的增多,运行时间会逐渐增加到不可忍受的程度。51Testing软件测试网&B7M4T+WU]x0]

思考过之后才知道,直接调用selenium提供的API方法或者对seleniumAPI进行二次封装后的调用,我们无非是通过调用这些方法来实现对测试页面细节程度的把控,可是真正的逻辑部分我们却未曾独立出来。51Testing软件测试网t'|,\Zf/tW(Y$z` ^

二、问题的解决办法:

1Ga|]c#z3BF4w0

Page Object模式51Testing软件测试网-xXAz8u-j6l5`6]

Page Object将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理。

#Gn(t H wVd Rx0

1、例如没有使用Page Object模式时对于163邮箱的登录操作代码如下:51Testing软件测试网Aq{Ou$k

packagecom.test;

oYf5{ Ka m0

 51Testing软件测试网s9_g&Q%C6q

importorg.openqa.selenium.By;51Testing软件测试网6h+lE^5[-\J Flc%K

importorg.openqa.selenium.WebDriver;51Testing软件测试网S:g${K\/oM

importorg.openqa.selenium.WebElement;

S9D%F:jk6K)c0

importorg.openqa.selenium.firefox.FirefoxDriver;

6E E l Gu5_d0

 

%L2i'U_[O0

publicclasstest163 {51Testing软件测试网}ON:A}n

   publicstaticvoidmain(String[] args)

#n:Y&N K}~v!c8g0

 {

_O} Go pT0

      //启动浏览器,进入163邮箱首页

&a o|.^9I:N4Z5w7}0

      WebDriver driver =newFirefoxDriver();

%J0n[6IFt0

      driver.get("http://mail.163.com/");

#e xh"o8Ux^ eF+m0

      

Ta$bqkEd2NQ5f0}3R#C0

      Thread.sleep(5000);

{ w N XK&e!ln5Q0

      //输入用户名密码,登录邮箱

({ [$_7]"RW&g"n.J0

      WebElement youxiangzhanghao_element = driver.findElement(By.id("idInput"));

X6l E_`0

      youxiangzhanghao_element.clear();

:k }.ms;E0

  

:t6{:NA C;^0

       youxiangzhanghao_element.sendKeys("justForYourTesting");

D*PE:n-Fe$\ M0

       51Testing软件测试网}T1h"|.eW

       WebElement mima_element = driver.findElement(By.id("pwdInput"));

/e7bn5N/t4P;{HmaQ1l)G0

       mima_element.sendKeys("135135");51Testing软件测试网'ov5bm!c0[ ]$g

               51Testing软件测试网O.e:lu,Y5]8I4S{\

       WebElement denglu_element = driver.findElement(By.id("loginBtn"));

&w'?7i,hz0

       denglu_element.click();

nw+z$mo w0

       

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| F0

import org.openqa.selenium.WebDriver;51Testing软件测试网4V9bRqN+uV-]rZ

import org.openqa.selenium.WebElement;

-` n;Tt1H`b0~0

import org.openqa.selenium.support.FindBy;51Testing软件测试网*@E yf\5@3MT)t

 

X-`(M ]7rj0

public 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&[LS t'x ?y

 

l F DH9}d pQLh,do0

      @FindBy(id="loginBtn" )51Testing软件测试网B5V!`5]4od|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软件测试网 vbqRZ-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

    通过对比12明显可以看出,2的逻辑结果比1更加简洁,以每个page为单位(类),以每个page元素为类的属性,以方法login()来实现每个页面元素操作的封装。程序的逻辑独立性更强,也方便后期的维护和修改。

j"Zm#feC{0

3、对Login类的login方法的初始化

kA"w!_ Qd+G0

package 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)r8L J
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软件测试网#q lnTx kL,azy
    {
2wpoc4v9? l)c0    WebDriver driver= new FirefoxDriver();51Testing软件测试网$d H4\B8Y+Ki
    // 对页面元素的初始化
,_2zCb^;s P0    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)zj
    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

TAG:

引用 删除 dy20082250   /   2017-12-25 21:56:28
引用 删除 dy20082250   /   2017-12-25 21:55:49
5
引用 删除 jiahaoqi   /   2016-07-28 10:10:09
5
janewash的个人空间 引用 删除 janewash   /   2016-07-13 17:03:51
5
引用 删除 灰灰渣渣   /   2016-03-24 16:04:24
5
yychenyuan的个人空间 引用 删除 yychenyuan   /   2015-11-25 16:08:11
点错了。。。居然没输入验证码就过了
yychenyuan的个人空间 引用 删除 yychenyuan   /   2015-11-25 16:07:19
-5
zdqyyf的个人空间 引用 删除 zdqyyf   /   2015-06-10 16:18:34
5
huhainan的个人空间 引用 删除 huhainan   /   2014-11-20 21:55:38
5
晓成 引用 删除 kulelyc   /   2014-08-20 12:30:35
5
Mr.南柯 引用 删除 bob123654   /   2014-08-01 23:23:01
靠 你有病吧?不想评分就别评分 别人辛辛苦苦写的东西评-1分 你大爷的
原帖由mildshark于2014-06-12 14:32:34发表
引用 删除 mildshark   /   2014-06-12 14:32:34
-1
chendoing的个人空间 引用 删除 chendoing   /   2013-10-24 13:07:27
5
Mr.南柯 引用 删除 bob123654   /   2013-01-06 09:01:49
原帖由blueskyzs于2013-01-05 21:57:17发表
那复杂的页面,N个元素,那岂不是要N个变量??

那你自己想呗 ,可以一个页面的元素放在同一个类里边
引用 删除 blueskyzs   /   2013-01-05 21:57:17
那复杂的页面,N个元素,那岂不是要N个变量??
引用 删除 ioriliu2006   /   2013-01-05 17:23:03
5
 

评分:0

我来说两句

Open Toolbar