selenium开源功能测试工具
上一篇 / 下一篇 2012-03-26 15:33:50 / 个人分类:Selenium
Selenium分为Core与RC(Remote Controll)两个部分,其中Core是基础的,直接在HTML Table里编写测试代码的模块,而Remote Controll则支持用Java等语言编写测试用例,并自动调用FireFox1.5来运行。
L2h*n!S%_gs x&h02.Better Practice51Testing软件测试网B(qwC0AV,S'mQ[Nf
1. Never use Selenium FIT mode51Testing软件测试网wuNy.nM
Selenium分为两种运行模式,Driven Mode(现在叫Selenium Remote Control)和FIT Mode(现在叫Selenium Core)。
51Testing软件测试网?v8bMg \ @
FIT Mode顾名思义,就是类似FIT Testing Framework那种使用方式,主要用于QA等非技术人员编写Web应用的功能测试。FIT Mode的Selenium测试使用HTML来组织测试用例。例如我要测试一个web应用的登陆功能。我可能写出这样的HTML 表格。
FIT Mode顾名思义,就是类似FIT Testing Framework那种使用方式,主要用于QA等非技术人员编写Web应用的功能测试。FIT Mode的Selenium测试使用HTML来组织测试用例。例如我要测试一个web应用的登陆功能。我可能写出这样的HTML 表格。
d&H+}-T&_1E0 1 < table >
Pa R Tc"R2C5Q0 2 < tr >
z3l$v4i9I-P:K6l0 3 < td > open </ td >
4 < td >http://localhost:8080/login</ td >51Testing软件测试网AW^H0Gk6p,aA
5 < td ></ td >
B;W9}k'E?$Nv0 6 </ tr >
%w'c!QY0I"J0 7 < tr >
)_L.|Uz8[K0 8 < td > type </ td >
'tht y ?ip1KC0 9 < td > id=username </ td >
5 < td ></ td >
B;W9}k'E?$Nv0 6 </ tr >
%w'c!QY0I"J0 7 < tr >
)_L.|Uz8[K0 8 < td > type </ td >
'tht y ?ip1KC0 9 < td > id=username </ td >
10 < td > someuser </ td >
MD$xY o sk011 </ tr >51Testing软件测试网Q5QD e!|1T
12 < tr >51Testing软件测试网/u(M!tJ+b
13 < td > type </ td >51Testing软件测试网P)\0uP|i
14 < td > id=password </ td >51Testing软件测试网'XID+} b.pD[P
15 < td > password </ td >
Se'K Qm3^4@|k016 </ tr >
MD$xY o sk011 </ tr >51Testing软件测试网Q5QD e!|1T
12 < tr >51Testing软件测试网/u(M!tJ+b
13 < td > type </ td >51Testing软件测试网P)\0uP|i
14 < td > id=password </ td >51Testing软件测试网'XID+} b.pD[P
15 < td > password </ td >
Se'K Qm3^4@|k016 </ tr >
17 < tr >51Testing软件测试网'B5wT)kg7x{
18 < td > click </ td >
E+qSQX Vm019 < td > id=login_button </ td >
18 < td > click </ td >
E+qSQX Vm019 < td > id=login_button </ td >
7| J\.C*zu7RN([7z020 < td ></ td >51Testing软件测试网0A6~8KR(Fr
21 </ tr >51Testing软件测试网V8UY{h5d2fz,^+N
22 < tr >
{:Rm-]I+m j GR-s%x023 < td > assertTextPresent </ td >51Testing软件测试网 QYqv"s7N^H
24 < td > Welcome to xxxx </ td >51Testing软件测试网e ?vE"?;e@
25 < td ></ td >51Testing软件测试网heT-])jO}K5x*ZF.u
26 </ tr >51Testing软件测试网2J)C k/[U
27 </ table >
0\HM:\gjQ0不同于FIT,Selenium内置了一系列的命令,如上例中的open, type, click以及assertTextPresent,因此QA可以完全抛开DEV独立地编写测试(FIT需要DEV提供Behavior. Fixture)。因此FIT Mode是相当容易使用的,哪怕不会使用HTML的QA,也可以使用FrontPage画出三列表格,依次填入数据。
然而对于大多数team而言——尤其是敏捷team,FIT Mode平易的外表下是令人恐惧的泥沼。大多数团队往往选择使用Selenium作为功能测试和集成测试工具而不仅仅是QA测试工具,在不同的迭代间遇到功能流程或UI变化时,必须要重构Selenium测试,或者说,Functional Test Migration。令人遗憾的是,HTML based的Selenium FIT Testing的重构竟然令人难以置信的困难。我们可以使用include等Selenium FIT扩展,使得它可以重用详细的功能(Log in, Log out诸如此类)。即便如此,在一个真实的项目中,Selenium Test的数量往往在200-500之间(我目前所处的项目在改用Driven Mode前已达350+),对于这么大基数的Selenium测试,手工重构几乎是不可想象的,而目前尚没有HTML代码重构工具。即便存在泛泛意义上的HTML重构工具,对于Selenium测试重构的有效性尚待商榷。而使用Driven Mode上述代码可以写为:
1 public void testShouldShowAWeclomeMessageAfterUserLoggedIn() {51Testing软件测试网:mf)P1NT*O3WRw3p
2 selenium.open( "http://localhost:8080/login" );
2 selenium.open( "http://localhost:8080/login" );
3 selenium.type( " id=username " , " someuser " );
"Jn{;Y-B p/o1V0\04 selenium.type( " id=password " , " password " );
g&J? vB1e?D+X05 selenium.click( " id=login_button " );
&i'o,}*Fz$Z VZV06 assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));
e|X)Z&R1RzD07 }
"Jn{;Y-B p/o1V0\04 selenium.type( " id=password " , " password " );
g&J? vB1e?D+X05 selenium.click( " id=login_button " );
&i'o,}*Fz$Z VZV06 assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));
e|X)Z&R1RzD07 }
很自然,一个训练有素的程序员会重构出如下代码:
51Testing软件测试网Nk,~v9g#gLc$`S
1 public void login(String username, String password) {
R ^:jXRZ*nh0 2 selenium.open( "http://localhost:8080/login" );
EhS MfPH"R0 3 selenium.type( " id=username " ,username);51Testing软件测试网X0moexFP|
4 selenium.type( " id=password " , password);
c|"a[@ Xf0 5 selenium.click( " id=login_button " );
.g9nl}kHG:{0 6 }51Testing软件测试网_};}X+Bg1[ f
7
:};{+lD^SC0 8 public void testShouldShowAWeclomeMessageAfterUserLoggedIn() {51Testing软件测试网4vf1{E7C
9 login( " someuser " , " password " );
(p5G?hF1A!e|0a010 assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));51Testing软件测试网~ M,A8k|8w}
11 }
Ai&l{ mub+D0之后无论是pull up到公共基类还是extact到Utils class都是很容易的事情。由于Java在代码重构上便利,Java Selenium Remote Control成为使用Selenium的最佳方式。在这一点上,纵使Ruby语法上比Java简单灵活得多,它仍不是编写Selenium测试的最佳载体(当然一个经过精心设计的ruby selenium dsl wrapper还是具有非凡的价值的,这个我们后面会涉及到)。
1 public void login(String username, String password) {
R ^:jXRZ*nh0 2 selenium.open( "http://localhost:8080/login" );
EhS MfPH"R0 3 selenium.type( " id=username " ,username);51Testing软件测试网X0moexFP|
4 selenium.type( " id=password " , password);
c|"a[@ Xf0 5 selenium.click( " id=login_button " );
.g9nl}kHG:{0 6 }51Testing软件测试网_};}X+Bg1[ f
7
:};{+lD^SC0 8 public void testShouldShowAWeclomeMessageAfterUserLoggedIn() {51Testing软件测试网4vf1{E7C
9 login( " someuser " , " password " );
(p5G?hF1A!e|0a010 assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));51Testing软件测试网~ M,A8k|8w}
11 }
Ai&l{ mub+D0之后无论是pull up到公共基类还是extact到Utils class都是很容易的事情。由于Java在代码重构上便利,Java Selenium Remote Control成为使用Selenium的最佳方式。在这一点上,纵使Ruby语法上比Java简单灵活得多,它仍不是编写Selenium测试的最佳载体(当然一个经过精心设计的ruby selenium dsl wrapper还是具有非凡的价值的,这个我们后面会涉及到)。
2. Using the name user, system, page instead of selenium
6?O?*U#^)t-kY0i3Z0观察上面提到的代码,其中使用selenium来操纵web应用的行为,这在Remote Control里是常见的做法,但是仍然不够好,我们可以做一些小的变化以得到更好的测试:
6?O?*U#^)t-kY0i3Z0观察上面提到的代码,其中使用selenium来操纵web应用的行为,这在Remote Control里是常见的做法,但是仍然不够好,我们可以做一些小的变化以得到更好的测试:
+T3V E h3n};z#j K0 1 protected void setup() {51Testing软件测试网1r%]9[0z$[B%A
2 selenium = // intialize selenium instance
51Testing软件测试网BZn:_(n` ?
3 user = selenium;51Testing软件测试网 KfW7t'I"^Z'J
4 currentPage = selenium;51Testing软件测试网 kdn!DK:L
5 }
.k\4SI.e$R*P(D0 6 51Testing软件测试网!_Ov8Go2q
7 public void login(String username, String password) {
3 user = selenium;51Testing软件测试网 KfW7t'I"^Z'J
4 currentPage = selenium;51Testing软件测试网 kdn!DK:L
5 }
.k\4SI.e$R*P(D0 6 51Testing软件测试网!_Ov8Go2q
7 public void login(String username, String password) {
51Testing软件测试网'Eu7t8yM-qa'`"^
8 user.open( "http://localhost:8080/login" );
c3A$~`P4G0 9 user.type( " id=username " ,username);
(v4c#?L)D_010 user.type( " id=password " , password);51Testing软件测试网+Ab(T)c"i,p!GX
11 user.click( " id=login_button " ); 51Testing软件测试网#Q](J[6r*U
12 }51Testing软件测试网K;dqb(]Js
13 51Testing软件测试网]8f6HS(a+Tag
14 public void testShouldShowAWeclomeMessageAfterUserLoggedIn() {51Testing软件测试网 D1p9fBz2W J
15 login( " some guy " , " password " );51Testing软件测试网LX;Gn!@,[8~w l`s
16 assertTrue(currentPage.isTextPresent( " Welcome to xxxx " ));
nZB E+s sJ017 }51Testing软件测试网4h(D0ut1V:w
基本上这只不过是"另一种写法"而已,但是它更好的表达了"用户的行为",如login代码所示。以及"系统的正确相应",即currentPage.isTextPresent()。这种是典型的对编译器无意义对人有意义的代码,也就是普遍意义上好的代码。
8 user.open( "http://localhost:8080/login" );
c3A$~`P4G0 9 user.type( " id=username " ,username);
(v4c#?L)D_010 user.type( " id=password " , password);51Testing软件测试网+Ab(T)c"i,p!GX
11 user.click( " id=login_button " ); 51Testing软件测试网#Q](J[6r*U
12 }51Testing软件测试网K;dqb(]Js
13 51Testing软件测试网]8f6HS(a+Tag
14 public void testShouldShowAWeclomeMessageAfterUserLoggedIn() {51Testing软件测试网 D1p9fBz2W J
15 login( " some guy " , " password " );51Testing软件测试网LX;Gn!@,[8~w l`s
16 assertTrue(currentPage.isTextPresent( " Welcome to xxxx " ));
nZB E+s sJ017 }51Testing软件测试网4h(D0ut1V:w
基本上这只不过是"另一种写法"而已,但是它更好的表达了"用户的行为",如login代码所示。以及"系统的正确相应",即currentPage.isTextPresent()。这种是典型的对编译器无意义对人有意义的代码,也就是普遍意义上好的代码。
3. Creating a DSL base on your test codes51Testing软件测试网;R+~5jO'`
懂得HTML的QA可以在没有DEV的帮助下使用Selenium FIT mode,然而却不能在没有DEV的帮助下使用Driven Mode。于是最自然也是最fashion的做法,就是在已有的test codes之上提供Testing DSL或者Scripting Language,让FIT mode变得更加FIT。这方面内容是一个更大的主题,以后再详细展开吧。
懂得HTML的QA可以在没有DEV的帮助下使用Selenium FIT mode,然而却不能在没有DEV的帮助下使用Driven Mode。于是最自然也是最fashion的做法,就是在已有的test codes之上提供Testing DSL或者Scripting Language,让FIT mode变得更加FIT。这方面内容是一个更大的主题,以后再详细展开吧。
4. Hacking Selenium Object to support FIT command
Selenium FIT mode和RC mode下的命令有些许差异,比如FIT中的assertTextPresent,在RC中变成了isTextPresent。同样还有FIT中最实用的命令clickAndWait,在RC中变成了click和waitForPageToLoad。在RC中使用FIT mode中的命令也非难事,找到com.thoughtworks.selenium.Selenium,添加方法:
51Testing软件测试网r{*tE"DK
public void doCommand(String commmand, String parameters);51Testing软件测试网.D|tU'V.p_
然后在com.thoughtworks.selenium.DefaultSelenium中添加实现:
public void doCommand(String commmand, String parameters);51Testing软件测试网.D|tU'V.p_
然后在com.thoughtworks.selenium.DefaultSelenium中添加实现:
51Testing软件测试网/E \/O
X:dk6S!b
1 public void doCommand(String commmand, String parameters) {
|"tPPK02 String[] paras = new String[] { "" , "" , "" }51Testing软件测试网6at0B^;r` e
3 for ( int i = 0 ; i < parameters.length && i < 3 ; i ++ )
5~ U-K8k)|`TYw#C04 paras[i] = parameters[i];
1 public void doCommand(String commmand, String parameters) {
|"tPPK02 String[] paras = new String[] { "" , "" , "" }51Testing软件测试网6at0B^;r` e
3 for ( int i = 0 ; i < parameters.length && i < 3 ; i ++ )
5~ U-K8k)|`TYw#C04 paras[i] = parameters[i];
51Testing软件测试网e|Rv;H#Ki5L&g
5 commandProcessor.doCommand(command, paras);51Testing软件测试网I4O+Z~/M4e
6 }
j#I0b[ bR0然后试验一下:
5 commandProcessor.doCommand(command, paras);51Testing软件测试网I4O+Z~/M4e
6 }
j#I0b[ bR0然后试验一下:
6j!Y3w9G&` j4M6?N0 selenium.doCommand( " clickAndWait " );51Testing软件测试网(xRaJ+uZz"w9y
在我们使用纯RC mode之前曾经用过一段中间方案,将rc code转化为fit code来跑(因为rc不支持https),由于不是真正的rc mode,像isTextPresent之类的方法都没有办法使用,只能使用FIT mode command。因此如果因为一些特殊的原因(https, chrome起不来,hta bug多等等),你没有办法使用RC mode,但是有希望得到RC可重构的好处,那么这个tricky的技巧倒是不错的选择。
5. Using chrome and IE hta lanucher to support https
iA&M*Y?8Pb06. Run test using different browser lanucher to test browser compatibility
$P$|z0Cu%x"wn0这两个都是和browser lanucher相关的,Selenium和JWebUnit最大的不同在于它使用真实的浏览器来跑测试,从而可以更加真实地考察系统在不同浏览器中的表现。因此使用不同的浏览器lanucher来运行测试,可以更好测试应用的浏览器兼容性,这对于web 2.0应用而言是很有帮助的。此外,使用rc提供的试验性lanucher,chrome和hta可以解决跨domain测试和https的问题。不过目前hta还是有很多bug的,推荐使用chrome。当然,最希望的还是澳洲的同事可以早日在selenium里提供https支持。
iA&M*Y?8Pb06. Run test using different browser lanucher to test browser compatibility
$P$|z0Cu%x"wn0这两个都是和browser lanucher相关的,Selenium和JWebUnit最大的不同在于它使用真实的浏览器来跑测试,从而可以更加真实地考察系统在不同浏览器中的表现。因此使用不同的浏览器lanucher来运行测试,可以更好测试应用的浏览器兼容性,这对于web 2.0应用而言是很有帮助的。此外,使用rc提供的试验性lanucher,chrome和hta可以解决跨domain测试和https的问题。不过目前hta还是有很多bug的,推荐使用chrome。当然,最希望的还是澳洲的同事可以早日在selenium里提供https支持。
TAG:
不要让那些真正对你好的人,慢慢的从你的生活中消失,无论爱情还是友情,都需要用心经营。
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 3340605
- 日志数: 1640
- 建立时间: 2011-12-07
- 更新时间: 2019-12-24