十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

Tellurium自动化测试框架介绍

上一篇 / 下一篇  2010-11-07 16:10:56 / 个人分类:测试经验

查看( 543 ) / 评论( 0 )
Tellurium自动化测试框架是由方剑在2007年6月创建的用于测试Web应用的框架,并在2008年6月移步到Google Code成为一个开源项目。它会定期发布版本,当前版本号是0.7.0。项目的核心已经历时两年,并繁衍出了多个子项目,包括:UDL、Core、Engine、Widget扩展、Maven Archetype、Trump、Tellurium IDE、TelluriumWorks以及参考项目。51Testing软件测试网9G`#H.p'yya
51Testing软件测试网Dc@Y c} G
这个框架是从Selenium框架发展而来,但又具有不同的测试理念。大多数Web测试框架,比如Selenium,主要致力于单独的UI元素。而Tellurium恰好相反,它把多个UI元素看作一个Widget整体,并将其称作UI module。
"z@'U0|W"k0f`0 51Testing软件测试网;Q&rWg5dR^
拿Google搜索的UI做个例子,这个界面用Tellurium表示成如下这样:
ey]FDm0
9s @ l)|9~ S+Z+Y0 ui.Container(uid: "GoogleSearchModule", clocator: [tag: "td"]){  
op$p\ Vq Z,Q0         InputBox(uid: "Input", clocator: [title: "Google Search"])  
[ VxD(DM0         SubmitButton(uid: "Search", clocator: [name: "btnG", value: "Google
+}o@b mu9y0 Search"])  
LSp Of Y0         SubmitButton(uid: "ImFeelingLucky", clocator: [value: "I'm Feeling51Testing软件测试网?y f2Vb;A])jq
Lucky"])51Testing软件测试网xyi~@7^
}  51Testing软件测试网9AY|A7j?&Yl
51Testing软件测试网4O5~%g[&V(LQI f
正如你在例子中看到的,UI module是嵌套的UI元素、tag以及attribute的集合。Tellurium在采用了UI module之后,更具表述性,对变化的响应也更加智能化。同时它也可以很容易地表示动态Web内容,并易于维护。
se(XFQk7Uw9t)x0 51Testing软件测试网{o9[oKGRB
这个框架由以下组件组成:51Testing软件测试网]+@o'F8|1nF2Dld

6]6w%s;fwEi8T$\x0     Trump——一个Firefox插件,全称是Tellurium UI Module Plugin,它可以在用户选择待测Web页面上的UI元素后,自动生成UI module。
k h B!h6bZx.Pu0     Tellurium IDE——另外一个Firefox插件,可以记录用户操作,并生成Tellurium测试脚本,包括UI module的定义、用户的动作以及断言。测试脚本是用Groovy写成的。51Testing软件测试网3_|W0l;i*R,^Fq Q
    TelluriumWorks——一个单独的Java Swing应用,用于编辑和运行Tellurium测试脚本。另外还在开发一个针对IntelliJ IDEA的IDE插件。
.Z8^a w)d/R*a0     JavaScript Widget Extensions——针对流行JavaScript框架的扩展,比如Dojo和jQuery UI。这样用户就可以在项目中引用发布的Tellurium jar文件,并在UI module定义中,把UI widget当作一个普通的Tellurium对象。
d i/}O]C` KY0 51Testing软件测试网8V+Q/er-^0{A:I.j?
特性
j l)Ca2VX8U?0 51Testing软件测试网PS_@X3FKk K
主要特性包括:51Testing软件测试网K_~f7^,s,_7Bv

Vk'M/K-T9vd`0     UI module清楚地表示了待测的UI。在Tellurium的测试代码中,并没有直接使用定位器(locator)。而是使用uid对象来引用UI元素,从而更具表现力。比如:51Testing软件测试网2y:g&w GX'H/SG!So

Ex-i7a`G0     type "GoogleSearchModule.Input", "Tellurium test"
Wl1}8o\!l^'q0     click "GoogleSearchModule.Search"51Testing软件测试网x p/Z2}+yO9yf
51Testing软件测试网c%Y4pa9q5k g
    使用UI属性而不是固定的定位器来描述UI。实际的定位器将会在运行时生成。如果属性改变了,新的运行时定位器也会由框架自动重新生成。Tellurium从而可以按需自适应UI的变化。51Testing软件测试网-g*o z/?@.e
51Testing软件测试网6a e(r/gf f:D%et
    Tellurium 0.7.0中的Santa算法通过一次定位整个的UI module,从而进一步提高测试的智能性。此外还使用了UI module部分匹配机制,在一定程度上适应属性的变化。51Testing软件测试网9n7W[4CX$Aj
    采用了Tellurium UI模板和Tellurium UID描述语言(UDL)来表示动态的web内容。51Testing软件测试网({ZjG5Dh|
    框架实行UI module和测试代码的分离,从而有利于重构。51Testing软件测试网'y*h RbVx1{
51Testing软件测试网qjYx7tQ hnC
    比如,UI和对应的测试方法定义在分离的Groovy类中。这样,测试代码就和UI module解耦了。51Testing软件测试网,tC`2J1FYh
51Testing软件测试网j!|1px9E!O%u9o#s
另外该框架还:
x:H9B1h*U&}wx0 51Testing软件测试网W a{@h7pr }
    使用抽象的UI对象来封装Web UI元素51Testing软件测试网h&?cB*hqD8e6hvT
    支持Widget以实现可重用性51Testing软件测试网dSx]p-F a
    为UI定义、动作和测试提供一门DSL
B(b;EicMI0     支持组定位,从而一次定位一组UI组件51Testing软件测试网*T,{N7x*T7L\w
    包括CSS选择器支持,以改善在IE中的测试速度51Testing软件测试网(o1Rv u+U&V e'y v
    提供定位器缓存和命令集来提高测试速度
u0ac{|;m0     支持数据驱动测试51Testing软件测试网2F1d1c-J/Qe_*]

ak%^ N,BB1z A i0 对比Selenium和Tellurium51Testing软件测试网4y+?fA @.qj
51Testing软件测试网BC]1L'?dO%^e*O$O1n
Selenium Web测试框架是最流行的开源自动化web测试框架之一。它是一款独创性的框架,提供了很多独一无二的特性和优势,比如:基于浏览器的测试、Selenium Grid以及使用Selenium IDE来“录制和回放”用户的动作。
IOq.S*Z"]y2A0
+m xTX#P w0 然后,Selenium有点问题。拿下面这段测试代码举个例子:
!UbV2}Y7Rf eu0 51Testing软件测试网!^1~+PBzN!S_^
setUp("http://www.google.com/", "*chrome"); 51Testing软件测试网2OBZe;Q7~
selenium.open("/"); 51Testing软件测试网mz*oc sIge:P
selenium.type("q", "Selenium test"); 51Testing软件测试网g,Q1s8rt rs)Oa
selenium.click("//input[@value='Google Search' and @type='button']");
L \A!P9Q-p0 51Testing软件测试网.H%W~zC
如果有人不是很熟悉Google的搜索页面,他能根据这段代码,说出页面的UI是什么样子的吗?定位器q在这里是什么意思呢?
Ip}u6P!|g |0 51Testing软件测试网I6r z {\,|
万一因为Web的变化,XPath //input[@value='Google Search' and @type='button']变成无效的了,怎么办?更有可能发生的是,这段代码需要逐行检查才能找出那几行需要更新的代码。
+Aw0A+{G4N| K#y0 51Testing软件测试网RY3`P#UV
万一这段测试代码里面有几十上百个定位器怎么办?使用Selenium IDE生成测试代码,这可能在一开始比较容易,但归纳和重构起来就很困难了。51Testing软件测试网q;A(mD&Zs c

Z*E9[2?p7Os4^0 重构会是一个比从头生成新测试代码更乏味的过程。原因在于硬编码的定位器和使用的测试代码耦合太紧密了。因为测试代码没有结构化,维护代码就变得异常困难。51Testing软件测试网8I ^P`hu

j"MBx|B?0 作为一个低层次Web测试驱动框架,Selenium是一个很好的框架。然而,它需要投入更多的努力才能创建出智能的测试代码。51Testing软件测试网*H@r+?3[[
51Testing软件测试网`%MKWu6a I
Tellurium就是为解决Selenium中的大多数问题而诞生的。51Testing软件测试网:m4qS Rm0C B L

sf%ri W"m0 Tellurium同时被设计用来解决Selenium的其他弱点。比如,IE性能一直是Selenium突出的问题。Tellurium的解决方案是,使用CSS选择器来作为缺省的定位器。定位器由UI module自动生成,并改善了测试速度。
-}9l0pa%ihM0
gA@PRzG0 另外,在采用了Tellurium UI module缓存以及基于新的Tellurium引擎的命令集之后,测试速度得到进一步的提升。Tellurium还支持对Ajax应用的测试:Tellurium的List和Table UI对象被用来在运行时表示动态Web内容。而option对象则被用来在运行时表示同一Web元素的两个不同UI。51Testing软件测试网LH6Mb t my S

~PY Ld9D0 与Selenium一样,Tellurium可以用来测试任何基于HTML DOM结构的Web应用。
7e9B f5G&rE^1H'm:]0 测试方法
4Swk1R'o~0 51Testing软件测试网6m2NUF!Y-i%_
Tellurium采用一种新的方式,通过UI module的概念来进行自动化测试。使用对象封装Web UI的元素,因此不再需要手动生成和重构UI的定位器。UI module是个简单的复合UI对象,由嵌套的基本UI对象组成。
ZsPz9o,b|0
#V}+|-e*EF~0 这个框架可以在两种模式下运行。第一种模式是作为Selenium框架的wrapper来工作。也就是说,Tellurium core基于UI module中的UI对象属性,生成运行时定位器。生成的运行时定位器然后通过Tellurium扩展传递给Selenium core来调用。51Testing软件测试网3W;Oy&x"s6q:Hvh+I

3c.[)G \@0H#jp0 Tellurium还在开发它自己的驱动引擎,即Tellurium Engine,以更好更有效地支持UI module。
4JR-\W$]"s&wv'he3O0
zLhH~8rUs0     首先,Tellurium Core把UI module转换成JSON的表示形式。51Testing软件测试网"JT@^&i2g6ct
    然后在使用UI module时,JSON表示的数据被第一次传递给Tellurium Engine。51Testing软件测试网wIBnux
    接着Tellurium Engine使用Santa算法,定位整个UI module,并将其存在缓存中。
.d(~zYkV0     在接下来的调用中,会直接使用缓存的UI module,而不需要重新定位了。
'ar3Y J8otC1F0     此外,Tellurium Core把多条命令合并成一条批处理命令,叫做宏命令,然后在一次调用中把这条批处理发送给Tellurium Engine。这样可以减少请求/响应带来的延迟。51Testing软件测试网}z%FaJ1f(VC)}

$l*U,Hl8r;?(m.^0 下面这个例子,使用了该项目网站上的问题搜索UI,来表述框架背后的思想。
4OJl[:D6F0
@ZChX2D~0 我们从为问题搜索的UI定义UI module开始吧:51Testing软件测试网V&fqhk+PwU z
51Testing软件测试网z*V#e8r#yjGv
ui.Form(uid: "issueSearch", clocator: [action: "list", method: "GET"]) {
I5d-c%TQy+zP+vy0         Selector(uid: "issueType", clocator: [name: "can", id: "can", direct:
"n PU D{2i;CNm\0         "true"])
7Eu W*[W`6^5A;H0         TextBox(uid: "searchLabel", clocator: [tag: "span", text: "for"])
!z[+}:Sl}0         InputBox(uid: "searchBox", clocator: [type: "text", name: "q", id: "q"])
'P_$B g'?hAj0         SubmitButton(uid: "searchButton", clocator: [value: "Search", direct:51Testing软件测试网X K`3b,F9@9z2{!b
        "true"])
.BcQ'qeYX&V.m*c0 }51Testing软件测试网{'G/t#ed$KY
51Testing软件测试网^ M3g/Z tB \X
然后使用下面这个测试方法:51Testing软件测试网(t qQD X kw
51Testing软件测试网$~cZ!d0cK
public void searchIssue(String type, String issue){
\0l vP s&L,h1}ku c-G0         select "issueSearch.issueType", type
:?)f6m g;b8\lT0         keyType "issueSearch.searchBox", issue51Testing软件测试网6q'U%xMX!Z dre&N
        click "issueSearch.searchButton"51Testing软件测试网 OK6z;x&t4J
        waitForPageToLoad 30000
[%T)v4a#qC0 }51Testing软件测试网0Im ~r4@.REJ
51Testing软件测试网.i/a F1d8i2Q2G c4d
如果有一天,你需要把Selector修改成输入框,那我们只需要更新对应的UI module:51Testing软件测试网)?'c Ys6\n f#J0p
51Testing软件测试网2a5v3N/]-f9tK5f.vyj
ui.Form(uid: "issueSearch", clocator: [action: "list", method: "GET"]) {
9CN5qa0Z:E0YV$B.U:^0         InputBox(uid: "issueType", clocator: [name: "can", direct: "true"])
ws6tk,f5~ xp0         TextBox(uid: "searchLabel", clocator: [tag: "span", text: "for"])
!W#t)i4]:ONN0         InputBox(uid: "searchBox", clocator: [type: "text", name: "q", id: "q"])51Testing软件测试网v!yO4YW8l4T
        SubmitButton(uid: "searchButton", clocator: [value: "Search", direct: "true"])
+ST"S)mJ,_G Gp*E0 }51Testing软件测试网5B8Y"DiZU8B

.o^lv_s]pM0 然后修改命令:
UvTo;_D/y0 51Testing软件测试网h4f D `_.C a!qMJ
select "issueSearch.issueType", type51Testing软件测试网Mx2Z{`!`\
51Testing软件测试网"N~W&Q5?q-w$W7u y
为:
R'Gm7JB(otJ0 51Testing软件测试网6AeO4{0{6\;O
type "issueSearch.issueType", type51Testing软件测试网 WE*M$CZ

L|W [H7}Ep3f0 其余则保持不变。51Testing软件测试网7f @,d&XO,W3^~*u7O-l1J
51Testing软件测试网VBp+@`
如果有动态的Web内容,比如Google Books的网站,它包含了一个图书分类的列表,每个分类中包含一个图书列表。针对这样UI的UI module会出奇的简单:
-x_{ Z'|mS'wnl0
aRH'r']M0 ui.Container(uid: "GoogleBooksList", clocator: [tag: "table", id: "hp_table"]) {51Testing软件测试网a/} s`2I#bD Z
        List(uid: "subcategory", clocator: [tag: "td", class: "sidebar"], separator:
`1E]*KGM!cB U0         "div") {
l"bfib8nl0         Container(uid: "{all}") {51Testing软件测试网!rf^,Z ^$QdG
                TextBox(uid: "title", clocator: [tag: "div", class: "sub_cat_title"])
I0AKP'@ v"F0                 List(uid: "links", separator: "p") {51Testing软件测试网2`3hs)W!d
                        UrlLink(uid: "{all}", clocator: [:])
0q*B^$~n ~z?0                         }51Testing软件测试网 L!PHtcT+M@
                }51Testing软件测试网6_ W b~;m w"}Q
        }}51Testing软件测试网8qM(I+Ts+F
51Testing软件测试网8G{ S2U#FF/{
Tellurium UID描述语言为定义动态Web内容提供了更多的灵活性。我们来看个复杂点的例子。
Fk2EyYZ8k[9f+`0 51Testing软件测试网Ir|%pmj
ui.StandardTable(uid: "GT", clocator: [id: "xyz"], ht: "tbody"){
\ T{5@C g0         TextBox(uid: "{header: first} as One", clocator: [tag: "th", text: "one"], self:
mB uI;R7v$B0         true)
dq,l#nrji%L0         TextBox(uid: "{header: 2} as Two", clocator: [tag: "th", text: "two"], self: true)
:e4D!dP&Iz$T$S0         TextBox(uid: "{header: last} as Three", clocator: [tag: "th", text: "three"],
7C4khm }Z{ ] U0         self: true)
!`:O)|v9V#n%Q0         TextBox(uid: "{row: 1, column -> One} as A", clocator: [tag: "div", class: "abc"])
,` w*J%cH[t0         Container(uid: "{row: 1, column -> Two} as B"){
/sR2K9Rd/x0                 InputBox(uid: "Input", clocator: [tag: "input", class: "123"])51Testing软件测试网ZAKiQ3YI
                Container(uid: "Some", clocator: [tag: "div", class: "someclass"]){
0~;N+wjR;^7C0KX0                         Span(uid: "Span", clocator: [tag: "span", class: "x"])51Testing软件测试网J!i9C"k/a c{U
                        UrlLink(uid: "Link", clocator: [:])51Testing软件测试网*pFw(I)^,Q%I
                        }
"d fA r;{U:GG0`H+`5E0                    }
ss M)Z!M0y0                 TextBox(uid: "{row: 1, column -> Three} as Hello", clocator: [tag: "td"], self:
}Ddv)k&@[0                 true)
3S)T#G b3N0         }51Testing软件测试网"F#W-g0w)S'`

Ov'wfD_0 在这个例子中,我们是使用元数据“first”、数字和“last”来指定header的位置。元数据“{row: 1, column -> One} as A”意味着我们例子中的UI元素——一个TextBox,处于第一行,并和header“One”处在同一列。测试代码很干净,比如:
twh0qJ i0 51Testing软件测试网J V.g LP]sQ4h*e1h
getText "GT.A"
GKP/`cl0 keyType "GT.B.Input", input51Testing软件测试网S X*{g#K
click "GT.B.Some.Link"
|ABU!b0 waitForPageToLoad 3000051Testing软件测试网v]"iK-wnP!H

ca%l{.~ wB)M0 未来计划
2d,}Qx#NQ,z)[~;~0 51Testing软件测试网K1c#K,o^
Tellurium是一个年轻而且创新的项目,它具有许多来自开发团队和用户社区的新颖思想。Tellurium希望在以下领域继续发展:51Testing软件测试网HZ]'}ads'G'D x

pO,l(v:L5W2Uh0     Tellurium 0.7.0已经实现了一个新的使用jQuery的测试驱动引擎。这个引擎的主要特性包括:UI module组定位、UI module缓存、命令集处理、用jQuery重新实现的Selenium API以及新的Tellurium API。Tellurium会继续开发这个新引擎直至完备。
u-A(FQx\&[B0     Tellurium IDE module插件0.8.0 RC1刚刚发布,它包含了很多新的特性。录制和生成测试脚本的Tellurium IDE的RC版本也已发布。它们是Tellurium成功的关键,因此会得到持续的改进。除了Trump和Tellurium IDE,Tellurium还在计划改善TelluriumWorks,让它可以编辑、完成语法检查,并运行Tellurium DSL测试脚本。
!p!Y G%V TH0     Tellurium作为云测试工具是另外一个非常重要的发展方向。项目团队正在计划重新考虑整个架构,使其可以更直接地并行执行测试。通过P2P技术,只需最少的管理,就让测试服务器在云的环境中具备自组织和自协调的能力,这是件极具挑战性的工作。
LYy5z|-M0 51Testing软件测试网/^tiqN*O
Tellurium框架还希望发展的其他领域包括:51Testing软件测试网fk:o!r|%h]G
51Testing软件测试网)S4u I!vm"T7NG WL
    创建可重用的Dojo、ExtJS和jQuery UI Tellurium widget。这样可以让其他人只需要在自己的项目中包含对应的jar文件,就可以重用这些widget。
G%w/u^0o)Y`B0     支持行为驱动测试。51Testing软件测试网0?'uT4XBO t\f
    Web安全测试
@/a a}|;MBVyp0     支持其他的语言,比如Ruby
VL6Kg)c0
_(]%Z(_;BX;^0 关于作者
6Vm BAD-J w0
Z`7`Tf0 方剑毕业于乔治亚理工学院,电子和计算机工程专业博士学位。他的工作是某IT公司的高级软件工程师,致力于设计和实现企业级应用软件。他是Tellurium自动化测试框架项目的创建者。
S'mD4mk;P0
J:XkJ4p"L2f O4P?0 查看英文原文:http://www.infoq.com/articles/tellurium_intro

TAG:

我来说两句

(可选)

Open Toolbar