转换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

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

and post your questions to Tellurium user group

http://groups.google.com/group/tellurium-users

Thanks.












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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值