Tellurium自动化测试框架是由方剑在2007年6月创建的用于测试Web应用的框架,并在2008年6月移步到Google Code成为一个开源项目。它会定期发布版本,当前版本号是0.7.0。项目的核心已经历时两年,并繁衍出了多个子项目,包括:UDL、Core、Engine、Widget扩展、Maven Archetype、Trump、Tellurium IDE、TelluriumWorks以及参考项目。
这个框架是从Selenium框架发展而来,但又具有不同的测试理念。大多数Web测试框架,比如Selenium,主要致力于单独的UI元素。而Tellurium恰好相反,它把多个UI元素看作一个Widget整体,并将其称作UI module。
拿Google搜索的UI做个例子,这个界面用Tellurium表示成如下这样:
ui.Container(uid: " GoogleSearchModule " , clocator: [tag: " td " ]){
InputBox(uid: " Input " , clocator: [title: " Google Search " ])
SubmitButton(uid: " Search " , clocator: [name: " btnG " ,value: " GoogleSearch " ])
SubmitButton(uid: " ImFeelingLucky " , clocator: [value: " I'm Feeling Lucky " ])}
正如你在例子中看到的,UI module是嵌套的UI元素、tag以及attribute的集合。Tellurium在采用了UI module之后,更具表述性,对变化的响应也更加智能化。同时它也可以很容易地表示动态Web内容,并易于维护。
这个框架由以下组件组成:
主要特性包括:
type "GoogleSearchModule.Input","Tellurium test"click "GoogleSearchModule.Search"
Tellurium 0.7.0中的Santa算法通过一次定位整个的UI module,从而进一步提高测试的智能性。此外还使用了UI module部分匹配机制,在一定程度上适应属性的变化。
比如,UI和对应的测试方法定义在分离的Groovy类中。这样,测试代码就和UI module解耦了。
另外该框架还:
Selenium Web测试框架是最流行的开源自动化web测试框架之一。它是一款独创性的框架,提供了很多独一无二的特性和优势,比如:基于浏览器的测试、Selenium Grid以及使用Selenium IDE来“录制和回放”用户的动作。
然后,Selenium有点问题。拿下面这段测试代码举个例子:
setUp( " http://www.google.com/ " , " *chrome " );
selenium.open( " / " );
selenium.type( " q " , " Selenium test " );
selenium.click( " //input[@value='Google Search' and @type='button'] " );
如果有人不是很熟悉Google的搜索页面,他能根据这段代码,说出页面的UI是什么样子的吗?定位器q在这里是什么意思呢?
万一因为Web的变化,XPath //input[@value='Google Search' and @type='button']变成无效的了,怎么办?更有可能发生的是,这段代码需要逐行检查才能找出那几行需要更新的代码。
万一这段测试代码里面有几十上百个定位器怎么办?使用Selenium IDE生成测试代码,这可能在一开始比较容易,但归纳和重构起来就很困难了。
重构会是一个比从头生成新测试代码更乏味的过程。原因在于硬编码的定位器和使用的测试代码耦合太紧密了。因为测试代码没有结构化,维护代码就变得异常困难。
作为一个低层次Web测试驱动框架,Selenium是一个很好的框架。然而,它需要投入更多的努力才能创建出智能的测试代码。
Tellurium就是为解决Selenium中的大多数问题而诞生的。
Tellurium同时被设计用来解决Selenium的其他弱点。比如,IE性能一直是Selenium突出的问题。Tellurium的解决方案是,使用CSS选择器来作为缺省的定位器。定位器由UI module自动生成,并改善了测试速度。
另外,在采用了Tellurium UI module缓存以及基于新的Tellurium引擎的命令集之后,测试速度得到进一步的提升。Tellurium还支持对Ajax应用的测试:Tellurium的List和Table UI对象被用来在运行时表示动态Web内容。而option对象则被用来在运行时表示同一Web元素的两个不同UI。
与Selenium一样,Tellurium可以用来测试任何基于HTML DOM结构的Web应用。
Tellurium采用一种新的方式,通过UI module的概念来进行自动化测试。使用对象封装Web UI的元素,因此不再需要手动生成和重构UI的定位器。UI module是个简单的复合UI对象,由嵌套的基本UI对象组成。
这个框架可以在两种模式下运行。第一种模式是作为Selenium框架的wrapper来工作。也就是说,Tellurium core基于UI module中的UI对象属性,生成运行时定位器。生成的运行时定位器然后通过Tellurium扩展传递给Selenium core来调用。
Tellurium还在开发它自己的驱动引擎,即Tellurium Engine,以更好更有效地支持UI module。
下面这个例子,使用了该项目网站上的问题搜索UI,来表述框架背后的思想。
我们从为问题搜索的UI定义UI module开始吧:
ui.Form(uid: " issueSearch " , clocator: [action: " list " , method: " GET " ]){
Selector(uid: " issueType " , clocator: [name: " can " , id: " can " , direct: " true " ])
TextBox(uid: " searchLabel " , clocator: [tag: " span " , text: " for " ])
InputBox(uid: " searchBox " , clocator: [type: " text " , name: " q " , id: " q " ])
SubmitButton(uid: " searchButton " , clocator: [value: " Search " , direct: " true " ])}
然后使用下面这个测试方法:
public void searchIssue(String type, String issue){
select " issueSearch.issueType " , typekeyType
" issueSearch.searchBox " , issueclick " issueSearch.searchButton " waitForPageToLoad 30000 }
如果有一天,你需要把Selector修改成输入框,那我们只需要更新对应的UI module:
ui.Form(uid: " issueSearch " , clocator: [action: " list " , method: " GET " ]){
InputBox(uid: " issueType " , clocator: [name: " can " , direct: " true " ])
TextBox(uid: " searchLabel " , clocator: [tag: " span " , text: " for " ])
InputBox(uid: " searchBox " , clocator: [type: " text " , name: " q " , id: " q " ])
SubmitButton(uid: " searchButton " , clocator: [value: " Search " , direct: " true " ])}
然后修改命令:
select " issueSearch.issueType " , type
为:
type " issueSearch.issueType " , type
其余则保持不变。
如果有动态的Web内容,比如Google Books的网站,它包含了一个图书分类的列表,每个分类中包含一个图书列表。针对这样UI的UI module会出奇的简单:
ui.Container(uid: " GoogleBooksList " , clocator: [tag: " table " , id: " hp_table " ]){
List(uid: " subcategory " , clocator: [tag: " td " , class : " sidebar " ], separator: " div " ){
Container(uid: " {all} " ){TextBox(uid: " title " , clocator: [tag: " div " , class : " sub_cat_title " ])
List(uid: " links " , separator: " p " ){UrlLink(uid: " {all} " , clocator: [:]) }} }}
Tellurium UID描述语言为定义动态Web内容提供了更多的灵活性。我们来看个复杂点的例子。
ui.StandardTable(uid: " GT " , clocator: [id: " xyz " ], ht: " tbody " ){
TextBox(uid: " {header: first} as One " , clocator: [tag: " th " , text: " one " ], self: true )
TextBox(uid: " {header: 2} as Two " , clocator: [tag: " th " , text: " two " ], self: true )
TextBox(uid: " {header: last} as Three " , clocator: [tag: " th " , text: " three " ],self: true )
TextBox(uid: " {row: 1, column -> One} as A " , clocator: [tag: " div " , class : " abc " ])
Container(uid: " {row: 1, column -> Two} as B " ){
InputBox(uid: " Input " , clocator: [tag: " input " , class : " 123 " ])
Container(uid: " Some " , clocator: [tag: " div " , class : " someclass " ]){
Span(uid: " Span " , clocator: [tag: " span " , class : " x " ])
UrlLink(uid: " Link " , clocator: [:])} }
TextBox(uid: " {row: 1, column -> Three} as Hello " , clocator: [tag: " td " ], self: true )}
在这个例子中,我们是使用元数据“first”、数字和“last”来指定header的位置。元数据“{row: 1, column -> One} as A”意味着我们例子中的UI元素——一个TextBox,处于第一行,并和header“One”处在同一列。测试代码很干净,比如:
getText " GT.A " keyType " GT.B.Input " , inputclick " GT.B.Some.Link " waitForPageToLoad 30000
Tellurium是一个年轻而且创新的项目,它具有许多来自开发团队和用户社区的新颖思想。Tellurium希望在以下领域继续发展:
Tellurium框架还希望发展的其他领域包括:
关于作者
方剑毕业于乔治亚理工学院,电子和计算机工程专业博士学位。他的工作是某IT公司的高级软件工程师,致力于设计和实现企业级应用软件。他是Tellurium自动化测试框架项目的创建者。
查看英文原文:Introducing the Tellurium Automated Testing Framework