開源自動化測試框架 Tellurium

jopen 14年前發布 | 77K 次閱讀 測試 測試工具

Tellurium是什么?

Tellurium是一種自動化的web測試框架。雖然它是在selemium的階段上建立起來的,但兩者之間有許多概念上的差異,Tellurium的主要特點如下:

 

開源自動化測試框架 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?

開源自動化測試框架 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將我們構建完成的項目導入。完成后項目結構如下:

 

開源自動化測試框架 Tellurium

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

 

 

 

我們打開人人網的注冊頁面,填寫個人信息,Tellurium IDE會自動記錄我的操作。

 

Record :錄制按鈕。打開時默認是按下的,再次點擊將取消錄制狀態。

 

Step :單步運行。點擊一次,運行一步。

 

Run : 運行按鈕。點擊之后將會把腳本從頭到尾運行一遍。

 

Clear : 清楚腳本。清楚錄制的腳本。

 

 

 

本例子錄制了一個人人網的注冊頁面(不完整,只是填寫了注冊信息,并被“提交”注冊)。

 

我們切換到Source View標簽,可查看錄制的代碼。

 

點擊菜單欄File 可選擇將代碼以不同的形式導出或保存到剪切版上。 

 

開源自動化測試框架 Tellurium

 

 

在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


來自:http://www.cnblogs.com/fnng/archive/2012/01/14/2322355.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!