開源自動(dòng)化測(cè)試框架Tellurium
Tellurium是什么?
Tellurium是一種自動(dòng)化的web測(cè)試框架。雖然它是在selemium的階段上建立起來(lái)的,但兩者之間有許多概念上的差異,Tellurium的主要特點(diǎn)如下:
· 不是單一的“記錄和播放”風(fēng)格。
· 基于UI模塊,也就是說(shuō),它側(cè)重于UI元素
· 讓你有結(jié)構(gòu)化的代碼執(zhí)行用戶界面和測(cè)試代碼之間的解耦
· 魯棒性的變化,Tellurium達(dá)到使用復(fù)合定位建立在運(yùn)行時(shí)和組的定位器定位,刪除里面的UI模塊和外部UI元素的UI元素之間的依賴
· 表達(dá)所使用Groovy動(dòng)態(tài)語(yǔ)言特性和DSL
· 可重復(fù)使用,用戶界面模塊可重復(fù)使用相同的應(yīng)用程序和Tellurium部件,可用于不同的應(yīng)用
· 地址在網(wǎng)絡(luò)上的動(dòng)態(tài)因素。UI模板使用數(shù)據(jù)網(wǎng)格和Tellurium UI對(duì)象的回應(yīng)屬性可以處理JavaScript事件
· 核心框架是在Groovy實(shí)現(xiàn)和測(cè)試,可以在Groovy中的JUnit,TestNG的,或純DSL腳本書面
· 支持?jǐn)?shù)據(jù)驅(qū)動(dòng)測(cè)試
· 提供Maven原型
如何使用Tellurium?
使用Maven創(chuàng)建一個(gè)新的Tellurium 測(cè)試項(xiàng)目
首先,你需要安裝maven ,確保本機(jī)有maven環(huán)境。關(guān)于maven環(huán)境的搭建,可以參考的我的博客,關(guān)于maven的文章:
http://www.cnblogs.com/fnng/category/345480.html
找到maven目錄下的settings.xml 文件,我本機(jī)的路徑在: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> |
打開的你的命令提示符,切換到你的工作空間的目錄下,運(yùn)行下列Maven命令來(lái)創(chuàng)建一個(gè)新的Tellurium測(cè)試方案(項(xiàng)目)"demo"
mvn archetype:generate -DgroupId=example -DartifactId=demo -DarchetypeArtifactId=tellurium-junit-archetype -DarchetypeGroupId=tellurium -DarchetypeVersion=0.6.0
然后切換到該項(xiàng)目的目錄下執(zhí)行:mvn eclipse:eclipse 構(gòu)建成我們的eclipse所能識(shí)別的項(xiàng)目結(jié)構(gòu)。
打開Eclipse將我們構(gòu)建完成的項(xiàng)目導(dǎo)入。完成后項(xiàng)目結(jié)構(gòu)如下:
在Telluriumconfig.groovy文件中包含Tellurium項(xiàng)目設(shè)置,你能根據(jù)你的需求對(duì)它進(jìn)行自定義設(shè)置
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() 兩個(gè)方法是增加定期谷歌搜索和谷歌“手氣不錯(cuò)”搜索。
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 } } |
因?yàn)門ellurium只支持groovy語(yǔ)言,所以無(wú)groovy語(yǔ)言無(wú)法直接在Eclipse IDE中運(yùn)行,需要Eclipse安裝對(duì)groovy語(yǔ)言支持的插件。
Groovy-Eclipse 2.5.· 插件下載地址:
http://www.oschina.net/news/·9279/groovy-eclipse-25·
當(dāng)然,你也可以使用IntelliJ IDEA 工具,它同樣也運(yùn)行java語(yǔ)言非常優(yōu)秀的IDE。 而且IntelliJ IDEA本身是支持groovy語(yǔ)言。
Tellurium IDE 插件
這個(gè)同樣也是基于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打開上面的鏈接后點(diǎn)擊“add to firefox”根據(jù)提示,瀏覽器開始下載安裝重啟。
在菜單欄---工具----Tellurium IDE打開插件。
我們打開人人網(wǎng)的注冊(cè)頁(yè)面,填寫個(gè)人信息,Tellurium IDE會(huì)自動(dòng)記錄我的操作。
Record :錄制按鈕。打開時(shí)默認(rèn)是按下的,再次點(diǎn)擊將取消錄制狀態(tài)。
Step :?jiǎn)尾竭\(yùn)行。點(diǎn)擊一次,運(yùn)行一步。
Run : 運(yùn)行按鈕。點(diǎn)擊之后將會(huì)把腳本從頭到尾運(yùn)行一遍。
Clear : 清楚腳本。清楚錄制的腳本。
本例子錄制了一個(gè)人人網(wǎng)的注冊(cè)頁(yè)面(不完整,只是填寫了注冊(cè)信息,并被“提交”注冊(cè))。
我們切換到Source View標(biāo)簽,可查看錄制的代碼。
點(diǎn)擊菜單欄File 可選擇將代碼以不同的形式導(dǎo)出或保存到剪切版上。
在Eclipse中運(yùn)行測(cè)試代碼
我們?cè)贓clipse中創(chuàng)建一個(gè)NewUiModule.groovy 的文件。并把我Tellurium IDE中錄制的代碼插入,內(nèi)容如下:
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: "帳號(hào)", id: "xid_reg_handle"]) UrlLink(uid: "A", clocator: [tag: "a", text: "手機(jī)號(hào)"]) } 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=·0··3&rt=27" type "Regform.RegEmail", "dddd" type "Regform.RegEmail", "chongshi" type "Regform.Pwd", "·23456" 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", "已經(jīng)工作了" 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 } } |
編寫一個(gè)測(cè)試類對(duì)上面的方法時(shí)行測(cè)試:
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[·].contains("All Downloads")); app.selectDownloadType(allTypes[·]); app.searchDownload("TrUMP"); } } |
編譯項(xiàng)目并運(yùn)行新的測(cè)試用例.
TestNG創(chuàng)建項(xiàng)目
如果我們想創(chuàng)建一個(gè)testNG的項(xiàng)目,可以使用maven通過(guò)下面的命令進(jìn)行創(chuàng)建。
mvn archetype:generate-DgroupId=example -DartifactId=demo -DarchetypeArtifactId=tellurium-testng-archetype -DarchetypeGroupId=tellurium -DarchetypeVersion=0.6.0
后記:
偶然在infoq上看到了關(guān)于這個(gè)自動(dòng)化測(cè)試框架的介紹,本人對(duì)于陌生的測(cè)試技術(shù)有莫大的熱情,于是,開始查找它的相關(guān)資料,發(fā)現(xiàn)關(guān)于這個(gè)框架的資料很少。中文的更是簡(jiǎn)單的介紹。因?yàn)槭侵袊?guó)人做的這個(gè)框架,在開源軟件方面,老外嘲笑中國(guó)技術(shù)員只知道索取,沒(méi)有開創(chuàng)精神。這使我更產(chǎn)生了好奇,于是花費(fèi)了點(diǎn)時(shí)間對(duì)這個(gè)框架了解了一番。發(fā)現(xiàn)做的還是挺不錯(cuò)的,如果熟悉selenium的話,學(xué)習(xí)這個(gè)框架應(yīng)該不是很難。不過(guò)這個(gè)框架也使用了一些非主流的技術(shù),如groovy語(yǔ)言,我之前就沒(méi)停過(guò),可能我孤陋寡聞,由于網(wǎng)上關(guān)于groovy語(yǔ)言的資料不多。
當(dāng)然,這個(gè)框架還有很多不足,沒(méi)有見有公司用這個(gè)測(cè)試框架進(jìn)行測(cè)試。雖然,它的提出的一些技術(shù)是比selenium優(yōu)秀的。但還需項(xiàng)目來(lái)驗(yàn)證。沒(méi)有自己的官方網(wǎng)站,目前只寄托在google code上面。極其缺乏中文資料??吹阶钚碌陌姹竞托侣勔彩侨ツ甑?,貌似今年一年都沒(méi)什么動(dòng)靜。希望別太監(jiān)了。我寫這篇文檔也是希望更多的測(cè)試人員來(lái)關(guān)注這個(gè)自動(dòng)化測(cè)試框架。
作者花費(fèi)那么多時(shí)間和精力來(lái)做這個(gè)自動(dòng)化測(cè)試框架,不管他做的如何,是否能應(yīng)用我們的項(xiàng)目中,給我們帶來(lái)利益,但他的精神是值得我們學(xué)習(xí)。
posted on 2014-03-07 10:49 順其自然EVO 閱讀(350) 評(píng)論(0) 編輯 收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄 、selenium and watir webdrivers 自動(dòng)化測(cè)試學(xué)習(xí)