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

Tellurium自动化测试框架介绍

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

查看( 542 ) / 评论( 0 )
Tellurium自动化测试框架是由方剑在2007年6月创建的用于测试Web应用的框架,并在2008年6月移步到Google Code成为一个开源项目。它会定期发布版本,当前版本号是0.7.0。项目的核心已经历时两年,并繁衍出了多个子项目,包括:UDL、Core、Engine、Widget扩展、Maven Archetype、Trump、Tellurium IDE、TelluriumWorks以及参考项目。51Testing软件测试网 OYR/itS

+q e"\1G9]+A U*w0 这个框架是从Selenium框架发展而来,但又具有不同的测试理念。大多数Web测试框架,比如Selenium,主要致力于单独的UI元素。而Tellurium恰好相反,它把多个UI元素看作一个Widget整体,并将其称作UI module。
%xZ+v*xuZ)q1`O\0
%L'WL/Pl#nK"W0 拿Google搜索的UI做个例子,这个界面用Tellurium表示成如下这样:51Testing软件测试网,|QSa-`;^

,Z.F!f3bFDo?)EU0 ui.Container(uid: "GoogleSearchModule", clocator: [tag: "td"]){  51Testing软件测试网:i!Z0Yd'F y"u
        InputBox(uid: "Input", clocator: [title: "Google Search"])  51Testing软件测试网rg6Z&f!kL
        SubmitButton(uid: "Search", clocator: [name: "btnG", value: "Google51Testing软件测试网&dWr(uB:Q T ?$V
Search"])  
*\^O;o;jG P PT0         SubmitButton(uid: "ImFeelingLucky", clocator: [value: "I'm Feeling51Testing软件测试网.JE4Q%P`2d0cR8P
Lucky"])51Testing软件测试网)h1adGFQjXdW
}  51Testing软件测试网/|(X-KF~

p/hxO;bE0 正如你在例子中看到的,UI module是嵌套的UI元素、tag以及attribute的集合。Tellurium在采用了UI module之后,更具表述性,对变化的响应也更加智能化。同时它也可以很容易地表示动态Web内容,并易于维护。
_,hU:p,@QF0 51Testing软件测试网K:E3HP s2p
这个框架由以下组件组成:
"v z7w6H{&x{M Z U0
y/mq#JB}L,^v/[0     Trump——一个Firefox插件,全称是Tellurium UI Module Plugin,它可以在用户选择待测Web页面上的UI元素后,自动生成UI module。51Testing软件测试网&|\o$\m9O(H
    Tellurium IDE——另外一个Firefox插件,可以记录用户操作,并生成Tellurium测试脚本,包括UI module的定义、用户的动作以及断言。测试脚本是用Groovy写成的。
:z(k'_8Wj&J U6_#lq0     TelluriumWorks——一个单独的Java Swing应用,用于编辑和运行Tellurium测试脚本。另外还在开发一个针对IntelliJ IDEA的IDE插件。51Testing软件测试网Ky X&y!DQH3Et
    JavaScript Widget Extensions——针对流行JavaScript框架的扩展,比如Dojo和jQuery UI。这样用户就可以在项目中引用发布的Tellurium jar文件,并在UI module定义中,把UI widget当作一个普通的Tellurium对象。51Testing软件测试网9S&N `ln}h8F

Sg,lg(M)k~Lw0 特性51Testing软件测试网&]%jJ+o @_
51Testing软件测试网!Ubpvhg%JU Y
主要特性包括:
5oT3Ag D;L6N0
5L:_.CtN2L0     UI module清楚地表示了待测的UI。在Tellurium的测试代码中,并没有直接使用定位器(locator)。而是使用uid对象来引用UI元素,从而更具表现力。比如:
3y\4W4K?9X0 51Testing软件测试网v.suj'GK
    type "GoogleSearchModule.Input", "Tellurium test"
2[m"I(Sm8v hh3\f0     click "GoogleSearchModule.Search"
H:nTl*I)ax\0
.|:lj5j_]T"n0     使用UI属性而不是固定的定位器来描述UI。实际的定位器将会在运行时生成。如果属性改变了,新的运行时定位器也会由框架自动重新生成。Tellurium从而可以按需自适应UI的变化。51Testing软件测试网k"A/G5B%Oz1vc7n@l

e-Y K9b{HJTI }0     Tellurium 0.7.0中的Santa算法通过一次定位整个的UI module,从而进一步提高测试的智能性。此外还使用了UI module部分匹配机制,在一定程度上适应属性的变化。
e/qfw Q0     采用了Tellurium UI模板和Tellurium UID描述语言(UDL)来表示动态的web内容。
t!m;iZ*^s/m \ pe)o0     框架实行UI module和测试代码的分离,从而有利于重构。51Testing软件测试网"q8Im:LB1e\
51Testing软件测试网B4| Hq5^i!~aj3]v v
    比如,UI和对应的测试方法定义在分离的Groovy类中。这样,测试代码就和UI module解耦了。51Testing软件测试网8xy&Q|GL-r4`

[ObQ^e^k0 另外该框架还:51Testing软件测试网%Ued[)H
51Testing软件测试网 ^[`;[F-k
    使用抽象的UI对象来封装Web UI元素
So?V.E*b%B(r0     支持Widget以实现可重用性
WZ2F2X)X/|s0     为UI定义、动作和测试提供一门DSL51Testing软件测试网 @0r%JM,S)o0g:P,gD
    支持组定位,从而一次定位一组UI组件
.\XS#~$evWQHv z1k0     包括CSS选择器支持,以改善在IE中的测试速度51Testing软件测试网0DL9On t5dFS I^
    提供定位器缓存和命令集来提高测试速度51Testing软件测试网 v'Xz h6})M
    支持数据驱动测试
he$_ j|*Y@u0
1D,_vpIZ0 对比Selenium和Tellurium51Testing软件测试网}o5A*g-O OW0O d
51Testing软件测试网 w:I"r+poU
Selenium Web测试框架是最流行的开源自动化web测试框架之一。它是一款独创性的框架,提供了很多独一无二的特性和优势,比如:基于浏览器的测试、Selenium Grid以及使用Selenium IDE来“录制和回放”用户的动作。
5?p4U\S_0 51Testing软件测试网f~)K v.s0QJHa
然后,Selenium有点问题。拿下面这段测试代码举个例子:
?7`}%w"I9X0
0j LcQ8|9bV YO0 setUp("http://www.google.com/", "*chrome"); 51Testing软件测试网W-_ht+lLr$?x_;T
selenium.open("/"); 51Testing软件测试网7_ nY@0~(p2f\:K2^
selenium.type("q", "Selenium test");
r!b6h2d!@&uU&E QnS0 selenium.click("//input[@value='Google Search' and @type='button']");51Testing软件测试网;d-H!^ B9K @o,X6]
51Testing软件测试网_\*MEPT6e
如果有人不是很熟悉Google的搜索页面,他能根据这段代码,说出页面的UI是什么样子的吗?定位器q在这里是什么意思呢?51Testing软件测试网)o/d!mtaY {oZv~i

fTu1Z.F(Xhiq^ ]0 万一因为Web的变化,XPath //input[@value='Google Search' and @type='button']变成无效的了,怎么办?更有可能发生的是,这段代码需要逐行检查才能找出那几行需要更新的代码。51Testing软件测试网&~1j'F3Xx@E6m

,z-y]:k8L c|8J0 万一这段测试代码里面有几十上百个定位器怎么办?使用Selenium IDE生成测试代码,这可能在一开始比较容易,但归纳和重构起来就很困难了。51Testing软件测试网;],Hn.OC*b

4x-Nn$O%_/O5P'jWX0 重构会是一个比从头生成新测试代码更乏味的过程。原因在于硬编码的定位器和使用的测试代码耦合太紧密了。因为测试代码没有结构化,维护代码就变得异常困难。51Testing软件测试网Rf8f?|"g
51Testing软件测试网 B/\5o-hun
作为一个低层次Web测试驱动框架,Selenium是一个很好的框架。然而,它需要投入更多的努力才能创建出智能的测试代码。51Testing软件测试网*pzLuvv9r;k
51Testing软件测试网5w4E*X:dN@3m7d7B
Tellurium就是为解决Selenium中的大多数问题而诞生的。51Testing软件测试网aQT Rf
51Testing软件测试网nx,} ~ qL1Rnx$~;I
Tellurium同时被设计用来解决Selenium的其他弱点。比如,IE性能一直是Selenium突出的问题。Tellurium的解决方案是,使用CSS选择器来作为缺省的定位器。定位器由UI module自动生成,并改善了测试速度。
"f [(]2@a3hF0
^$_4i*H4V9I3}9N ^0 另外,在采用了Tellurium UI module缓存以及基于新的Tellurium引擎的命令集之后,测试速度得到进一步的提升。Tellurium还支持对Ajax应用的测试:Tellurium的List和Table UI对象被用来在运行时表示动态Web内容。而option对象则被用来在运行时表示同一Web元素的两个不同UI。51Testing软件测试网 M_$z,K5^A7b1e

m7c-c)?9fz|0 与Selenium一样,Tellurium可以用来测试任何基于HTML DOM结构的Web应用。
wh:wjsZ0 测试方法
.gU"R4H+c)k"cz3IS0 51Testing软件测试网!jww&c~8d#[]J
Tellurium采用一种新的方式,通过UI module的概念来进行自动化测试。使用对象封装Web UI的元素,因此不再需要手动生成和重构UI的定位器。UI module是个简单的复合UI对象,由嵌套的基本UI对象组成。51Testing软件测试网:TX'kD2K

_ x\.NV0 这个框架可以在两种模式下运行。第一种模式是作为Selenium框架的wrapper来工作。也就是说,Tellurium core基于UI module中的UI对象属性,生成运行时定位器。生成的运行时定位器然后通过Tellurium扩展传递给Selenium core来调用。51Testing软件测试网']j"Cu4A.Yg

#@ M8nZ%@R D0 Tellurium还在开发它自己的驱动引擎,即Tellurium Engine,以更好更有效地支持UI module。
)?/y:u dgp s N0 51Testing软件测试网eY ~m Z Y$L
    首先,Tellurium Core把UI module转换成JSON的表示形式。
IHw)~s*R0     然后在使用UI module时,JSON表示的数据被第一次传递给Tellurium Engine。
@O'L9M/r0x:y0     接着Tellurium Engine使用Santa算法,定位整个UI module,并将其存在缓存中。51Testing软件测试网k1_3MhL
    在接下来的调用中,会直接使用缓存的UI module,而不需要重新定位了。
C-|N ?)L3Ny0     此外,Tellurium Core把多条命令合并成一条批处理命令,叫做宏命令,然后在一次调用中把这条批处理发送给Tellurium Engine。这样可以减少请求/响应带来的延迟。51Testing软件测试网n.h-] eg3KLd
51Testing软件测试网3L3F1O+g,[
下面这个例子,使用了该项目网站上的问题搜索UI,来表述框架背后的思想。51Testing软件测试网 w6}W&V T B,srX S
51Testing软件测试网 m9A,E*i2pZ
我们从为问题搜索的UI定义UI module开始吧:
9e}$CbjT+R0
e8["v/h/G-S0 ui.Form(uid: "issueSearch", clocator: [action: "list", method: "GET"]) {51Testing软件测试网s7e@,{c4S(rVS-z
        Selector(uid: "issueType", clocator: [name: "can", id: "can", direct:51Testing软件测试网6i+@V:w;]@ V-t
        "true"])51Testing软件测试网9iM,LBA8p7n&v
        TextBox(uid: "searchLabel", clocator: [tag: "span", text: "for"])
Zw$^V+O\-W fbi0         InputBox(uid: "searchBox", clocator: [type: "text", name: "q", id: "q"])
Q4S+XD$zl0         SubmitButton(uid: "searchButton", clocator: [value: "Search", direct:51Testing软件测试网,~ uE"b#M/S![ G,J
        "true"])51Testing软件测试网cx}#zsQ3C
}
+f1Kzkn!g/O:Y0 51Testing软件测试网pBs,V-X$e1m7v
然后使用下面这个测试方法:
8LG4L9i Rn]*dX0
dX_)C T0 public void searchIssue(String type, String issue){51Testing软件测试网i6Te$H2C\
        select "issueSearch.issueType", type51Testing软件测试网`N+O@5o.s
        keyType "issueSearch.searchBox", issue51Testing软件测试网+k7|exC*I7]O6pK
        click "issueSearch.searchButton"
4^EKS#G oc6c0         waitForPageToLoad 3000051Testing软件测试网sJ\V/y Lsq
}
yA8X%B? t AG1b0
o tBpG7B0 如果有一天,你需要把Selector修改成输入框,那我们只需要更新对应的UI module:51Testing软件测试网 V\`%e4gk`9i
51Testing软件测试网 uma~W
ui.Form(uid: "issueSearch", clocator: [action: "list", method: "GET"]) {51Testing软件测试网{eh fx9KJ T3t8\
        InputBox(uid: "issueType", clocator: [name: "can", direct: "true"])51Testing软件测试网l)mQ;D.^i
        TextBox(uid: "searchLabel", clocator: [tag: "span", text: "for"])51Testing软件测试网0{K/h`:Zv r u
        InputBox(uid: "searchBox", clocator: [type: "text", name: "q", id: "q"])51Testing软件测试网yZ;tsN5zV
        SubmitButton(uid: "searchButton", clocator: [value: "Search", direct: "true"])51Testing软件测试网1D3d!R6qq
}
0Q*o'nPJ*`9?`D0 51Testing软件测试网d'd&Mx0d {Nl r
然后修改命令:51Testing软件测试网R(CVJ|VMB k
51Testing软件测试网\JSr u6G7u/y
select "issueSearch.issueType", type51Testing软件测试网 _1ChQ _)| s

W[+bXya$G+O0 为:
nfA5t0i k'{kH0 51Testing软件测试网%z3o dc~*W7q
type "issueSearch.issueType", type
cF Mo/sE0 51Testing软件测试网/Hsd+] N^gy
其余则保持不变。
IFP*qfS!h1Vc4ISw0
8f&`0n$v&L?8k J0 如果有动态的Web内容,比如Google Books的网站,它包含了一个图书分类的列表,每个分类中包含一个图书列表。针对这样UI的UI module会出奇的简单:51Testing软件测试网[TDlZ

:t${._4Z,B{ I9B0 ui.Container(uid: "GoogleBooksList", clocator: [tag: "table", id: "hp_table"]) {51Testing软件测试网R6o,ojl7p _
        List(uid: "subcategory", clocator: [tag: "td", class: "sidebar"], separator:
suZw mQ%LnyF0         "div") {
6]3V])T$NSu%t&V0         Container(uid: "{all}") {
r-fz#N5YF+D$Y0                 TextBox(uid: "title", clocator: [tag: "div", class: "sub_cat_title"])51Testing软件测试网n5i#D o+Wu#S
                List(uid: "links", separator: "p") {51Testing软件测试网7fk$?^_!M
                        UrlLink(uid: "{all}", clocator: [:])
[{%\;z s jk0                         }
]v-_ q`:IFq9j"S8YC0                 }51Testing软件测试网Jbgq!Q
        }}
.ghK"n9c0
ts s4TT-W6ecf0 Tellurium UID描述语言为定义动态Web内容提供了更多的灵活性。我们来看个复杂点的例子。51Testing软件测试网.l3ulZm^8}%^
51Testing软件测试网Fp4PHl}
ui.StandardTable(uid: "GT", clocator: [id: "xyz"], ht: "tbody"){
F4aa I9J&eE%x A.U0         TextBox(uid: "{header: first} as One", clocator: [tag: "th", text: "one"], self:
Rb$r:~$x0         true)
o"F8vbr0         TextBox(uid: "{header: 2} as Two", clocator: [tag: "th", text: "two"], self: true)51Testing软件测试网n'c+] fb1L
        TextBox(uid: "{header: last} as Three", clocator: [tag: "th", text: "three"],
6Ry KH u0         self: true)
-z/\7e'h;V pd5f.G(L0         TextBox(uid: "{row: 1, column -> One} as A", clocator: [tag: "div", class: "abc"])51Testing软件测试网$gR1I;Q.ujY*A
        Container(uid: "{row: 1, column -> Two} as B"){
dh1_*p%i2M)`r"|3I0                 InputBox(uid: "Input", clocator: [tag: "input", class: "123"])
-s"T"n ?M'HA M0                 Container(uid: "Some", clocator: [tag: "div", class: "someclass"]){
/N$p-TG0P$G d4I0                         Span(uid: "Span", clocator: [tag: "span", class: "x"])
%LQJZ)x0                         UrlLink(uid: "Link", clocator: [:])
6Ar3zKNb0                         }
!S)c.QVqL#V0                    }51Testing软件测试网 E/h6\Nq)K7D
                TextBox(uid: "{row: 1, column -> Three} as Hello", clocator: [tag: "td"], self:51Testing软件测试网8t$U C&k Ou*s^
                true)51Testing软件测试网:u }"m,W3`)\]N
        }
x;q } Yz5v,Tm/c0 51Testing软件测试网)jL X-|+PC;J
在这个例子中,我们是使用元数据“first”、数字和“last”来指定header的位置。元数据“{row: 1, column -> One} as A”意味着我们例子中的UI元素——一个TextBox,处于第一行,并和header“One”处在同一列。测试代码很干净,比如:
,U$|!jv-R}"i0
+NdG,B$J/X.Q0 getText "GT.A"
:Xy$NR2uTHl2q0 keyType "GT.B.Input", input51Testing软件测试网6y'nFl)W.ZE}!^:F*Y4p;O
click "GT.B.Some.Link"
ot9N#t6`bx0 waitForPageToLoad 3000051Testing软件测试网 n/aJz5x'?5e+rmbJ4l
51Testing软件测试网,Yt4yqxF
未来计划
.R)Ffa]/j/J0
y6}7W,Q ^0m&F0 Tellurium是一个年轻而且创新的项目,它具有许多来自开发团队和用户社区的新颖思想。Tellurium希望在以下领域继续发展:
$k,R&d,Bcb2Y0 51Testing软件测试网R Bg2B9G!e2NQ4zP)B
    Tellurium 0.7.0已经实现了一个新的使用jQuery的测试驱动引擎。这个引擎的主要特性包括:UI module组定位、UI module缓存、命令集处理、用jQuery重新实现的Selenium API以及新的Tellurium API。Tellurium会继续开发这个新引擎直至完备。
Us(Z5wBQ!O0     Tellurium IDE module插件0.8.0 RC1刚刚发布,它包含了很多新的特性。录制和生成测试脚本的Tellurium IDE的RC版本也已发布。它们是Tellurium成功的关键,因此会得到持续的改进。除了Trump和Tellurium IDE,Tellurium还在计划改善TelluriumWorks,让它可以编辑、完成语法检查,并运行Tellurium DSL测试脚本。51Testing软件测试网*Z|B N1kZ"[@
    Tellurium作为云测试工具是另外一个非常重要的发展方向。项目团队正在计划重新考虑整个架构,使其可以更直接地并行执行测试。通过P2P技术,只需最少的管理,就让测试服务器在云的环境中具备自组织和自协调的能力,这是件极具挑战性的工作。51Testing软件测试网4qh `gsF
51Testing软件测试网U%jpGj)CL6t!t
Tellurium框架还希望发展的其他领域包括:51Testing软件测试网"X,rh4V/X @&HopU\
51Testing软件测试网%v(t.A%dlmb2{$c
    创建可重用的Dojo、ExtJS和jQuery UI Tellurium widget。这样可以让其他人只需要在自己的项目中包含对应的jar文件,就可以重用这些widget。
(tt)t#[ v0     支持行为驱动测试。
Zd*sy?S0     Web安全测试51Testing软件测试网uXy0L,T ^7Q
    支持其他的语言,比如Ruby51Testing软件测试网]9x0J?^$r\GC,f{

0s:O Q%|z0_0 关于作者
'c"KL s#r zd/`/iZ uw Y5~0
R'_WZ~ A!y;j0 方剑毕业于乔治亚理工学院,电子和计算机工程专业博士学位。他的工作是某IT公司的高级软件工程师,致力于设计和实现企业级应用软件。他是Tellurium自动化测试框架项目的创建者。51Testing软件测试网"s!t qU{)m j6Dl3R
51Testing软件测试网]2P/^&E+mc,ehZ!T
查看英文原文:http://www.infoq.com/articles/tellurium_intro

TAG:

我来说两句

(可选)

Open Toolbar