2. Using the name user, system, page instead of selenium
觀察上面提到的代碼,其中使用selenium來操縱web應(yīng)用的行為,這在Remote Control里是常見的做法,但是仍然不夠好,我們可以做一些小的變化以得到更好的測(cè)試:
	 1 protected   void  setup()  {
	 2     selenium  =    //  intialize selenium instance
	 3     user  =  selenium;
	 4     currentPage  =  selenium;
	 5 }
	 6
	 7 public   void  login(String username, String password)  {
	 8     user.open( " http://localhost:8080/login " );
	 9     user.type( " id=username " ,username);
	10     user.type( " id=password " , password);
	11     user.click( " id=login_button " );
	12 }
	13
	14 public   void  testShouldShowAWeclomeMessageAfterUserLoggedIn()  {
	15     login( " some guy " ,  " password " );
	16     assertTrue(currentPage.isTextPresent( " Welcome to xxxx " ));
	17 }
基本上這只不過是"另一種寫法"而已,但是它更好的表達(dá)了"用戶的行為",如login代碼所示。以及"系統(tǒng)的正確相應(yīng)",即currentPage.isTextPresent()。這種是典型的對(duì)編譯器無意義對(duì)人有意義的代碼,也是普遍意義上好的代碼。
3. Creating a DSL base on your test codes
懂得HTML的QA可以在沒有DEV的幫助下使用Selenium FIT mode,然而卻不能在沒有DEV的幫助下使用Driven Mode。于是自然也是fashion的做法,是在已有的test codes之上提供Testing DSL或者Scripting Language,讓FIT mode變得更加FIT。這方面內(nèi)容是一個(gè)更大的主題,以后再詳細(xì)展開吧。
4. Hacking Selenium Object to support FIT command
Selenium FIT mode和RC mode下的命令有些許差異,比如FIT中的assertTextPresent,在RC中變成了isTextPresent。同樣還有FIT中實(shí)用的命令clickAndWait,在RC中變成了click和waitForPageToLoad。在RC中使用FIT mode中的命令也非難事,找到com.thoughtworks.selenium.Selenium,添加方法:
public void doCommand(String commmand, String parameters);
	然后在com.thoughtworks.selenium.DefaultSelenium中添加實(shí)現(xiàn):
	1 public   void  doCommand(String commmand, String parameters)  {
	2    String[] paras  =   new  String[] { "" , "" , "" }
	3     for  ( int  i  =   0 ; i  <  parameters.length  &&  i  <   3 ; i ++ )
	4       paras[i]  =  parameters[i];
	5    commandProcessor.doCommand(command, paras);
	6 }
然后試驗(yàn)一下:
selenium.doCommand( " clickAndWait " );
在我們使用純RC mode之前曾經(jīng)用過一段中間方案,將rc code轉(zhuǎn)化為fit code來跑(因?yàn)閞c不支持https),由于不是真正的rc mode,像isTextPresent之類的方法都沒有辦法使用,只能使用FIT mode command。因此如果因?yàn)橐恍┨厥獾脑?https, chrome起不來,hta bug多等等),你沒有辦法使用RC mode,但是有希望得到RC可重構(gòu)的好處,那么這個(gè)tricky的技巧倒是不錯(cuò)的選擇。
	5. Using chrome and IE hta lanucher to support https
	6. Run test using different browser lanucher to test browser compatibility
這兩個(gè)都是和browser lanucher相關(guān)的,Selenium和JWebUnit大的不同在于它使用真實(shí)的瀏覽器來跑測(cè)試,從而可以更加真實(shí)地考察系統(tǒng)在不同瀏覽器中的表現(xiàn)。因此使用不同的瀏覽器lanucher來運(yùn)行測(cè)試,可以更好測(cè)試應(yīng)用的瀏覽器兼容性,這對(duì)于web 2.0應(yīng)用而言是很有幫助的。此外,使用rc提供的試驗(yàn)性lanucher,chrome和hta可以解決跨domain測(cè)試和https的問題。不過目前hta還是有很多bug的,推薦使用chrome。當(dāng)然,希望的還是澳洲的同事可以早日在selenium里提供https支持。