代码改变世界

开源自动化测试框架Tellurium

2012-01-14 14:03  虫师  阅读(17286)  评论(3编辑  收藏  举报

 

Tellurium是什么?

Tellurium是一种自动化的web测试框架。虽然它是在selemium的阶段上建立起来的,但两者之间有许多概念上的差异,Tellurium的主要特点如下:

 

 

  • l 不是单一的“记录和播放”风格。
  • l 基于UI模块,也就是说,它侧重于UI元素
  • l 让你有结构化的代码执行用户界面和测试代码之间的解耦
  • l 鲁棒性的变化,Tellurium达到使用复合定位建立在运行时和组的定位器定位,删除里面的UI模块和外部UI元素的UI元素之间的依赖
  • l 表达所使用Groovy动态语言特性和DSL
  • l 可重复使用,用户界面模块可重复使用相同的应用程序和Tellurium部件,可用于不同的应用
  • l 地址在网络上的动态因素。UI模板使用数据网格和Tellurium UI对象的回应属性可以处理JavaScript事件
  • l 核心框架是在Groovy实现和测试,可以在Groovy中的JUnit,TestNG的,或纯DSL脚本书面
  • l 支持数据驱动测试
  • l 提供Maven原型

 

如何使用Tellurium?

 

 

使用Maven创建一个新的Tellurium 测试项目

  首先,你需要安装maven ,确保本机有maven环境。关于maven环境的搭建,可以参考的我的博客,关于maven的文章:

http://www.cnblogs.com/fnng/category/345480.html

找到maven目录下的settings.xml 文件,我本机的路径在:F:\maven\apache-maven-3.0.3\conf\目录下。

打开文件,并在<profiles>....</profiles>之间添加如下信息:

 

<parofiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>kungfuters-public-snapshots-repo</id>
<name>Kungfuters.org Public Snapshot Repository</name>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>http://maven.kungfuters.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>kungfuters-public-releases-repo</id>
<name>Kungfuters.org Public Releases Repository</name>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>http://maven.kungfuters.org/content/repositories/releases</url>
</repository>
</repositories>
</profile>
<parofiles>

 

 

打开的你的命令提示符,切换到你的工作空间的目录下,运行下列Maven命令来创建一个新的Tellurium测试方案(项目)"demo"

mvn archetype:generate -DgroupId=example -DartifactId=demo -DarchetypeArtifactId=tellurium-junit-archetype -DarchetypeGroupId=tellurium -DarchetypeVersion=0.6.0

然后切换到该项目的目录下执行:mvn  eclipse:eclipse   构建成我们的eclipse所能识别的项目结构。

 

打开Eclipse将我们构建完成的项目导入。完成后项目结构如下:

 

Telluriumconfig.groovy文件中包含Tellurium项目设置,你能根据你的需求对它进行自定义设置

 

tellurium{  
//embedded selenium server configuration
embeddedserver {
//port number
port = "4444"
//whether to use multiple windows
useMultiWindows = false
//whether to run the embedded selenium server. If false, you need to manually set up a selenium server
runInternally = true
//profile location
profile = ""
//user-extension.js file, for example, "target/test-classes/extension/user-extensions.js"
userExtension = ""
}
//event handler
eventhandler{
//whether we should check if the UI element is presented
checkElement = false
//wether we add additional events like "mouse over"
extraEvent = true
}
//data accessor
accessor{
//whether we should check if the UI element is presented
checkElement = true
}
connector{
//selenium server host
//please change the host if you run the Selenium server remotely
serverHost = "localhost"
//server port number the client needs to connect
port = "4444"
//base URL
baseUrl = "http://localhost:8080"
//Browser setting, valid options are
// *firefox [absolute path]
// *iexplore [absolute path]
// *chrome
// *iehta
browser = "*chrome"
//user's class to hold custom selenium methods associated with user-extensions.js
//should in full class name, for instance, "com.mycom.CustomSelenium"
customClass = ""
}
datadriven{
dataprovider{
//specify which data reader you like the data provider to use
//the valid options include "PipeFileReader", "CVSFileReader" at this point
reader = "PipeFileReader"
}
}
test{
//at current stage, the result report is only for tellurium data driven testing
//we may add the result report for regular tellurium test case
result{
//specify what result reporter used for the test result
//valid options include "SimpleResultReporter", "XMLResultReporter", and "StreamXMLResultReporter"
reporter = "XMLResultReporter"
//the output of the result
//valid options include "Console", "File" at this point
//if the option is "File", you need to specify the file name, other wise it will use the default
//file name "TestResults.output"
output = "Console"
//test result output file name
filename = "TestResult.output"
}
exception{
//whether Tellurium captures the screenshot when exception occurs.
//Note that the exception is the one thrown by Selenium Server
//we do not care the test logic errors here
captureScreenshot = true
//we may have a series of screenshots, specify the file name pattern here
//Here the ? will be replaced by the timestamp and you might also want to put
//file path in the file name pattern
filenamePattern = "Screenshot?.png"
}
}
uiobject{
builder{
//user can specify custom UI objects here by define the builder for each UI object
//the custom UI object builder must extend UiObjectBuilder class
//and implement the following method:
//
// public build(Map map, Closure c)
//
//For container type UI object, the builder is a bit more complicated, please
//take the TableBuilder or ListBuilder as an example
//example:
// Icon="org.tellurium.builder.IconBuilder"
}
}
widget{
module{
//define your widget modules here, for example Dojo or ExtJs //
included="dojo, extjs"
included=""
}
}

}

 

  GoogleSearchModule.groovy是用户界面模块的谷歌搜索,它自化生成Tellurium 所需要的火狐浏览器插件TrUMP.  doGoogleSearch() 和 doImFeelingLucky() 两个方法是增加定期谷歌搜索和谷歌手气不错搜索。

 

public class GoogleSearchModule extends DslContext {    
public void defineUi() {
ui.Container(uid: "Google", clocator: [tag: "table"]) {
InputBox(uid: "Input", clocator: [tag: "input", title: "Google Search", name: "q"])
SubmitButton(uid: "Search", clocator: [tag: "input", type: "submit", value: "Google Search", name: "btnG"])
SubmitButton(uid: "ImFeelingLucky", clocator: [tag: "input", type: "submit", value: "I'm Feeling Lucky", name: "btnI"])
}
}
public void doGoogleSearch(String input) {
keyType "Google.Input", input
pause 500
click "Google.Search"
waitForPageToLoad 30000
}
public void doImFeelingLucky(String input) {
type "Google.Input", input
pause 500
click "Google.ImFeelingLucky"
waitForPageToLoad 30000
}
}

 

因为Tellurium只支持groovy语言,所以无groovy语言无法直接在Eclipse IDE中运行,需要Eclipse安装对groovy语言支持的插件。

 

Groovy-Eclipse 2.5.1 插件下载地址:

 

http://www.oschina.net/news/19279/groovy-eclipse-251 

 

当然,你也可以使用IntelliJ IDEA 工具,它同样也运行java语言非常优秀的IDE。 而且IntelliJ IDEA本身是支持groovy语言。

 

 

 

Tellurium IDE 插件

 

 

这个同样也是基于firefox浏览器的插件有,功能与selenium IDE类似,如果你熟悉selenium IDE的话,Tellurium IDE就很容易操作。

 

Tellurium IDE 插件安装地址: 

 

https://addons.mozilla.org/en-US/firefox/addon/tellurium-ide/?src=search

 

注意:本插件不支持最新的firefox 9 firefox这小子一年换版本比翻书还快,本人使用的是firefox 3.6 版本,用firefox打开上面的链接后点击“add  to  firefox”根据提示,浏览器开始下载安装重启。

 

在菜单栏---工具----Tellurium IDE打开插件。

 

 

 

 

我们打开人人网的注册页面,填写个人信息,Tellurium IDE会自动记录我的操作。

 

Record :录制按钮。打开时默认是按下的,再次点击将取消录制状态。

 

Step :单步运行。点击一次,运行一步。

 

Run : 运行按钮。点击之后将会把脚本从头到尾运行一遍。

 

Clear : 清楚脚本。清楚录制的脚本。

 

 

 

本例子录制了一个人人网的注册页面(不完整,只是填写了注册信息,并被“提交”注册)。

 

我们切换到Source View标签,可查看录制的代码。

 

点击菜单栏File 可选择将代码以不同的形式导出或保存到剪切版上。 

 

 

 

在Eclipse中运行测试代码

 

 

我们在Eclipse中创建一个NewUiModule.groovy 的文件。并把我Tellurium IDE中录制的代码插入,内容如下:

 

class NewUiModule extends DslContext {  
public void defineUi() {
ui.Form(uid: "Regform", clocator: [tag: "form", action: "/s-c-i-reg.do", name: "regform", id: "regform", method: "post"]){
InputBox(uid: "RegEmail", clocator: [tag: "input", type: "text", class: "inputtext", id: "regEmail", name: "regEmail"])
InputBox(uid: "Pwd", clocator: [tag: "input", type: "password", class: "inputtext", id: "pwd", name: "pwd"])
InputBox(uid: "Name", clocator: [tag: "input", type: "text", class: "inputtext", id: "name", name: "name"])
RadioButton(uid: "Female", clocator: [tag: "input", type: "radio", value: "女生", id: "female", name: "gender"])
Selector(uid: "Birth_year", clocator: [tag: "select", name: "birth_year"])
Selector(uid: "Birth_month", clocator: [tag: "select", name: "birth_month"])
Selector(uid: "Birth_day", clocator: [tag: "select", name: "birth_day"])
Selector(uid: "Stage", clocator: [tag: "select", name: "stage", id: "stage"])
InputBox(uid: "Icode", clocator: [tag: "input", type: "text", class: "inputtext validate-code", id: "icode", name: "icode"])
Container(uid: "D_email", clocator: [tag: "dl", direct: "true", id: "d_email"]){
UrlLink(uid: "Xid_reg_handle", clocator: [tag: "a", text: "帐号", id: "xid_reg_handle"])
UrlLink(uid: "A", clocator: [tag: "a", text: "手机号"])
}
Container(uid: "Dl_gender", clocator: [tag: "dl", direct: "true", class: "dl_gender"]){
RadioButton(uid: "Male", clocator: [tag: "input", type: "radio", value: "男生", id: "male", name: "gender"])
}
}

connectSeleniumServer()
connectUrl "http://reg.renren.com/xn6245.do?ss=10113&rt=27"
type "Regform.RegEmail", "dddd"
type "Regform.RegEmail", "chongshi"
type "Regform.Pwd", "123456"
type "Regform.Name", "小三"
click "Regform.Female"
selectByLabel "Regform.Birth_year", "80后"
selectByLabel "Regform.Birth_month", "7"
selectByLabel "Regform.Birth_day", "8"
selectByLabel "Regform.Birth_day", "7"
selectByLabel "Regform.Stage", "已经工作了"
type "Regform.Icode", "漂亮宝贝"

} //Add your methods here
public void searchDownload(String keyword) {
keyType "TelluriumDownload.Input", keyword
click "TelluriumDownload.Search"
waitForPageToLoad 30000
}
public String[] getAllDownloadTypes() {
return getSelectOptions("TelluriumDownload.DownloadType")
}
public void selectDownloadType(String type) {
selectByLabel "TelluriumDownload.DownloadType", type
}
}

 

编写一个测试类对上面的方法时行测试:

public class NewTestCase extends TelluriumJavaTestCase {    
private static NewUiModule app;
@BeforeClass
public static void initUi() {
app = new NewUiModule();
app.defineUi(); }
@Before
public void setUpForTest() {
connectUrl("http://code.google.com/p/aost/downloads/list");
}
@Test
public void testTelluriumProjectPage() {
String[] allTypes = app.getAllDownloadTypes();
assertNotNull(allTypes);
assertTrue(allTypes[1].contains("All Downloads"));
app.selectDownloadType(allTypes[1]);
app.searchDownload("TrUMP");
}
}

 

编译项目并运行新的测试用例.

 

TestNG创建项目

如果我们想创建一个testNG的项目,可以使用maven通过下面的命令进行创建。

 

mvn archetype:generate-DgroupId=example -DartifactId=demo -DarchetypeArtifactId=tellurium-testng-archetype -DarchetypeGroupId=tellurium -DarchetypeVersion=0.6.0

 

相关资料阅读:

网站:

http://code.google.com/p/aost/

 

一个简易的文档:

http://aost.googlecode.com/files/Ten.Minutes.To.Tellurium.pdf

 

一段教你如何使用的视频

 

http://aost.googlecode.com/files/TenMinutesToTellurium.ogg

 

 

 

 

后记:

 

  偶然在infoq上看到了关于这个自动化测试框架的介绍,本人对于陌生的测试技术有莫大的热情,于是,开始查找它的相关资料,发现关于这个框架的资料很少。中文的更是简单的介绍。因为是中国人做的这个框架,在开源软件方面,老外嘲笑中国技术员只知道索取,没有开创精神。这使我更产生了好奇,于是花费了点时间对这个框架了解了一番。发现做的还是挺不错的,如果熟悉selenium的话,学习这个框架应该不是很难。不过这个框架也使用了一些非主流的技术,如groovy语言,我之前就没停过,可能我孤陋寡闻,由于网上关于groovy语言的资料不多。

     当然,这个框架还有很多不足,没有见有公司用这个测试框架进行测试。虽然,它的提出的一些技术是比selenium优秀的。但还需项目来验证。没有自己的官方网站,目前只寄托在google code上面。极其缺乏中文资料。看到最新的版本和新闻也是去年的,貌似今年一年都没什么动静。希望别太监了。我写这篇文档也是希望更多的测试人员来关注这个自动化测试框架。

 

  作者花费那么多时间和精力来做这个自动化测试框架,不管他做的如何,是否能应用我们的项目中,给我们带来利益,但他的精神是值得我们学习。

 

Web Page Counters
Computer Desks