您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
selenium開源功能測試工具
作者:網(wǎng)絡轉(zhuǎn)載 發(fā)布時間:[ 2012/12/12 14:33:07 ] 推薦標簽:

1.概述

Selenium是非常非常有用的,對JavaScript支持良好的Web層功能測試,集成測試工具。

Selenium分為Core與RC(Remote Controll)兩個部分,其中Core是基礎(chǔ)的,直接在HTML Table里編寫測試代碼的模塊,而Remote Controll則支持用Java等語言編寫測試用例,并自動調(diào)用FireFox1.5來運行。

具體的語法見http://www.openqa.org/selenium-core/usage.html
2.Better Practice
1. Never use Selenium FIT mode

Selenium分為兩種運行模式,Driven Mode(現(xiàn)在叫Selenium Remote Control)和FIT Mode(現(xiàn)在叫Selenium Core)。

FIT Mode顧名思義,是類似FIT Testing Framework那種使用方式,主要用于QA等非技術(shù)人員編寫Web應用的功能測試。FIT Mode的Selenium測試使用HTML來組織測試用例。例如我要測試一個web應用的登陸功能。我可能寫出這樣的HTML 表格。
 1  < table >
 2  < tr >
 3    < td > open </ td >
 4           < td > http://localhost:8080/login </ td >
 5           < td ></ td >
 6  </ tr >
 7  < tr >
 8    < td > type </ td >
 9           < td > id=username </ td >
10           < td > someuser </ td >
11  </ tr >
12  < tr >
13    < td > type </ td >
14           < td > id=password </ td >
15           < td > password </ td >
16  </ tr >
17  < tr >
18    < td > click </ td >
19           < td > id=login_button </ td >
20           < td ></ td >
21  </ tr >
22  < tr >
23    < td > assertTextPresent </ td >
24           < td > Welcome to xxxx </ td >
25           < td ></ td >
26  </ tr >
27  </ table >

不同于FIT,Selenium內(nèi)置了一系列的命令,如上例中的open, type, click以及assertTextPresent,因此QA可以完全拋開DEV獨立地編寫測試(FIT需要DEV提供Behavior Fixture)。因此FIT Mode是相當容易使用的,哪怕不會使用HTML的QA,也可以使用FrontPage畫出三列表格,依次填入數(shù)據(jù)。

然而對于大多數(shù)team而言——尤其是敏捷team,F(xiàn)IT Mode平易的外表下是令人恐懼的泥沼。大多數(shù)團隊往往選擇使用Selenium作為功能測試和集成測試工具而不僅僅是QA測試工具,在不同的迭代間遇到功能流程或UI變化時,必須要重構(gòu)Selenium測試,或者說,F(xiàn)unctional Test Migration。令人遺憾的是,HTML based的Selenium FIT Testing的重構(gòu)竟然令人難以置信的困難。我們可以使用include等Selenium FIT擴展,使得它可以重用詳細的功能(Log in, Log out諸如此類)。即便如此,在一個真實的項目中,Selenium Test的數(shù)量往往在200-500之間(我目前所處的項目在改用Driven Mode前已達350+),對于這么大基數(shù)的Selenium測試,手工重構(gòu)幾乎是不可想象的,而目前尚沒有HTML代碼重構(gòu)工具。即便存在泛泛意義上的HTML重構(gòu)工具,對于Selenium測試重構(gòu)的有效性尚待商榷。而使用Driven Mode上述代碼可以寫為:

1   public   void  testShouldShowAWeclomeMessageAfterUserLoggedIn()   {
2      selenium.open( " http://localhost:8080/login " );
3      selenium.type( " id=username " , " someuser " );
4      selenium.type( " id=password " ,  " password " );
5      selenium.click( " id=login_button " );
6      assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));
7 }


很自然,一個訓練有素的程序員會重構(gòu)出如下代碼:

 1   public   void  login(String username, String password)   {
 2      selenium.open( " http://localhost:8080/login " );
 3      selenium.type( " id=username " ,username);
 4      selenium.type( " id=password " , password);
 5      selenium.click( " id=login_button " );
 6 }
 7  
 8    public   void  testShouldShowAWeclomeMessageAfterUserLoggedIn()   {
 9      login( " someuser " ,  " password " );
10      assertTrue(selenium.isTextPresent( " Welcome to xxxx " ));
11 }

之后無論是pull up到公共基類還是extact到Utils class都是很容易的事情。由于Java在代碼重構(gòu)上便利,Java Selenium Remote Control成為使用Selenium的佳方式。在這一點上,縱使Ruby語法上比Java簡單靈活得多,它仍不是編寫Selenium測試的佳載體(當然一個經(jīng)過精心設計的ruby selenium dsl wrapper還是具有非凡的價值的,這個我們后面會涉及到)。
2. Using the name user, system, page instead of selenium

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd