1. 程式人生 > >轉換selenium測試用例到tellnrium測試用例

轉換selenium測試用例到tellnrium測試用例


簡介:

selenium一般側重於單個的UI元素,比如連結和按鈕。Tellurium引入了“UI模組”的概念來實現一種全新的Web自動化測試方法。UI模組是一組UI元素的集合。通常,UI模組代表了以基本UI元素相巢狀的形式存在的一個符合UI物件。

轉換現有的selenium測試用例到tellurium用例很簡單。你能夠在保留selenium測試用例裡面的所有定位資訊locator的同時還可以使用tellurium的一些好功能,比如支援資料驅動測試,使用組定位來發現UI元件集合中的資訊 等

建立selenium測試用例:

使用google開始頁當例子,使用selenium IDE錄製並匯出一些java程式碼:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class NewTest extends SeleneseTestCase {
        public void setUp() throws Exception {
                setUp("http://www.google.com/", "*chrome");
        }
        public void testNew() throws Exception {
                selenium.open("/");
                selenium.type("//input[@name='q']", "tellurium automated test");
                selenium.click("//input[@name='btnG']");
                selenium.waitForPageToLoad("30000");
                selenium.type("//input[@name='q']", "tellurium automated testing");
                selenium.click("//input[@name='btnI']");
                selenium.waitForPageToLoad("30000");
        }

建立Tellurium UI模組

轉換的第一步是寫一個UI模組,因為這是一個搜尋UI,你可以建立一個繼承DslContext的GoogleStartPage類

class GoogleStartPage extends DslContext{

}
接著根據不同的功能模組把selenium測試用例中得UI物件分組,這裡只有一個搜尋UI,包括一個輸入框,和兩個按鈕。把他們列出來。
search UI:
  input_box: "//input[@name='q']"
  google_search_button: "//input[@name='btnG']"
  imfelling_lucky_button: "//input[@name='btnI']"

這些locators可以被Tellurium 直接使用。

Tellurium supports nested UI objects, but be aware that all the nested UI objects only use relative xpaths in Tellurium so that the actual xpath will be generated at run time. For Selenium, the XPaths are fixed once they are created, as a result, you have two options here

Tellurium 支援巢狀UI物件,但是我推薦所有的巢狀UI物件都去使用相對xpaths以便讓xpath在執行時候產生。

對於selenium,

  1. 建立一個空locator的container,所有在裡面的物件是flat的。比如:Create a container with empty locator and all the objects inside it are flat. For example, we can create the UI module as follows,
    ui.Container(uid: "GooglePage"){
       InputBox(uid: "InputBox", locator: "//input[@name='q']")
       Button(uid: "GoogleSearch", locator: "//input[@name='btnG']")
       Button(uid: "FeelingLucky", locator: "//input[@name='btnI']")
    }


  2. 建立單獨的物件Create individual objects such as
    InputBox(uid: "InputBox", locator: "//input[@name='q']")
    Button(uid: "GoogleSearch", locator: "//input[@name='btnG']")
    Button(uid: "FeelingLucky", locator: "//input[@name='btnI']")


The first one is recommended since it clusters the objects into functional modules. The other advantage is that it reduces the namespace collisions. For example, the input box in 1) is referred by "GooglePage.InputBox" and the one in 2) will be referred by "InputBox". What happens if you have many InputBoxs?

推薦使用第一種,它把物件集中在一個功能模組中,另外一個好處是降低了名稱空間的衝突。比如,在1)中得輸入框使用GooglePage.InputBox,2)中得輸入框使用InputBox,如果有多個InputBox怎麼辦?

The obvious advantage of the UI module is that you can use UID to refer objects, such as "GooglePage.InputBox", not the xpath itself. This way is much more intuitive. Compare the following two code sections. The first one is the original Selenium test code

使用UID去引用物件,比如GooglePage.InputBox,Selenium 程式碼

selenium.type("//input[@name='q']", "tellurium automated testing");
selenium.click("//input[@name='btnG']");
selenium.waitForPageToLoad("30000");
selenium.type("//input[@name='q']", "tellurium automated testing");
selenium.click("//input[@name='btnI']");
selenium.waitForPageToLoad("30000");


 Tellurium 程式碼

type "GooglePage.InputBox", "tellurium automated testing"
click "GooglePage.GoogleSearch"
waitForPageToLoad 30000
type "GooglePage.InputBox", "tellurium automated testing"
click "GooglePage.FeelingLucky"
waitForPageToLoad 30000


很大區別吧?

Test Methods

測試方法

After the UI module is created, you should define test method inside the module class. Here, we have two methods, one is use "GoogleSearch" button to search, the other one is to use "FeelingLucky". Hence, we have

UI模組建立之後,你需要在模組類中定義測試方法,下面有2個方法,一個是使用GoogleSearch按鈕去搜索,一個是使用FeelingLucky

public void doGoogleSearch(String input){
       type "GooglePage.InputBox", input
       click "GooglePage.GoogleSearch"
       waitForPageToLoad 30000    
   }
  
    public void doFeelingLucky(String input){
        type "GooglePage.InputBox", input
        pause 500
        click "GooglePage.FeelingLucky"
        waitForPageToLoad 30000    
    }

By defining test methods in the module class, you can keep re-using them. Compare the following code

在模組類中定義了測試方法,你可以重用這些方法

selenium程式碼

  selenium.type("//input[@name='q']", "tellurium automated testing");
   selenium.click("//input[@name='btnG']");
   selenium.waitForPageToLoad("30000");
   selenium.type("//input[@name='q']", "tellurium automated testing");
   selenium.click("//input[@name='btnI']");
   selenium.waitForPageToLoad("30000");

Tellurium 程式碼

   doGoogleSearch("tellurium automated testing")
   doFeelingLucky("tellurium automated testing")


UI Module Class

UI模組類

Once the above two methods are defined, we have the UI module class as follows,

一旦上面的兩個方法被定義,我們就有了下面這樣的UI模組類:

class GoogleStartPage extends DslContext{
   public void defineUi() {
        ui.Container(uid: "GooglePage"){
            InputBox(uid: "InputBox", locator: "//input[@name='q']")
            Button(uid: "GoogleSearch", locator: "//input[@name='btnG']")
            Button(uid: "FeelingLucky", locator: "//input[@name='btnI']")
        }
   }
 
   public void doGoogleSearch(String input){
        type "GooglePage.InputBox", input
        click "GooglePage.GoogleSearch"
        waitForPageToLoad 30000    
   }
  
   public void doFeelingLucky(String input){
        type "GooglePage.InputBox", input
        click "GooglePage.FeelingLucky"
        waitForPageToLoad 30000    
   }
}


Create Tellurium Test Case

建立Tellurium測試用例

The next step will be to create the Tellurium test case and use the UI module defined in the GoogleStartPage class by extending TelluriumJavaTestCase if you want to use JUnit 4 or TelluriumTestNGTestCase for TestNG. The test case is very simple.

下一步就是建立Tellurium測試用例,使用UI模組定義的GoogleStartPage 類繼承

public class GoogleStartPageJavaTestCase extends TelluriumJavaTestCase {

    protected static GoogleStartPage gsp;

    @BeforeClass
    public static void initUi() {
        gsp = new GoogleStartPage();
        gsp.defineUi();
    }

    @Test
    public void testGoogleSearch() {
        connectUrl("http://www.google.com");
        gsp.doGoogleSearch("tellurium automated testing");
    }

    @Test
    public void testGoogleSearchFeelingLucky() {
        connectUrl("http://www.google.com");
        gsp.doFeelingLucky("tellurium automated testing");
    }
}


Then you are done.

完畢

Summary

概要

By the above steps, you can use Selenium locators directly in Tellurium but take advantage of Tellurium's UID reference, clean UI module, and JUnit/TestNG support. If you want to use more advanced Tellurium features, please check the details from Tellurium project web site

and post your questions to Tellurium user group

Thanks.


原文:http://code.google.com/p/aost/wiki/HowToConvertSeleniumTestToTellurium