近看了一下selenium如果要把這個(gè)用于自動(dòng)化測(cè)試,需要進(jìn)行整理,形成一個(gè)框架,我也對(duì)Google搜索這樣簡(jiǎn)單的功能做了一些嘗試,形成了一個(gè)簡(jiǎn)單的框架,簡(jiǎn)單的說(shuō)應(yīng)該有四層:
第一層應(yīng)該是UIObject這個(gè)對(duì)象層,主要是用來(lái)封裝對(duì)象的操作方法,例如:
Java代碼:
1. public class TextFieldUIObject extends UIObject {
2.
3. /**
4. * 構(gòu)造函數(shù)用于構(gòu)造textfield對(duì)象
5. * @param locator 描述信息
6. */
7. public TextFieldUIObject(String locator)
8. {
9. super(locator);
10. }
11. /**
12. * 向textfield輸入值
13. * @param content 輸入的內(nèi)容
14. * @throws SeleniumHelperNotExistException
15. */
16. public void type(String content) throws SeleniumHelperNotExistException
17. {
18. if(UIObjectHelper.SeleniumHelper==null) throw new SeleniumHelperNotExistException();
19. UIObjectHelper.SeleniumHelper.type(this.locator,content);
20. }
21. }
該代碼,封裝了textfield的控件,加入了方法type用于輸入。
第二層主要是構(gòu)件層,主要用來(lái)描述頁(yè)面上的控件,這里我用了簡(jiǎn)單的靜態(tài)變量的方法,還可以使用yml,xml,json甚至某種格式的文本文件進(jìn)行描述,之后根據(jù)文件生成,這樣可能會(huì)更加方面修改。
代碼如下:
Java代碼:
1. public class GoogleGuis {
2. public static PageUIObject SearchPage = new PageUIObject("/");
3. public static TextFieldUIObject SearchInput = new TextFieldUIObject("q");
4. public static ButtonUIObject SearchButton = new ButtonUIObject("btnG");
5. }
第三層應(yīng)該叫組件層,可以頁(yè)面切分成大組件,然后對(duì)組件進(jìn)行相關(guān)的操作,這里把Google的搜索作為一個(gè)組件,代碼如下:
1. /**
2. * 組件類(lèi)
3. * @author renzq
4. *
5. */
6. public class GooglePageSearchComponent {
7.
8. /**
9. * 進(jìn)行查詢(xún)操作
10. * @param content 查詢(xún)的內(nèi)容
11. * @throws SeleniumHelperNotExistException
12. */
13. public void search(String content) throws SeleniumHelperNotExistException{
14. GoogleGuis.SearchPage.PageOpen();
15. GoogleGuis.SearchInput.type(content);
16. GoogleGuis.SearchButton.click();
17. GoogleGuis.SearchPage.WaitForPageReady("3000");
18.
19. }
20. /**
21. * 校驗(yàn)查詢(xún)結(jié)果是否含有內(nèi)容
22. * @param content 內(nèi)容
23. * @return 根據(jù)是否含有,返回判斷的值
24. * @throws SeleniumHelperNotExistException
25. */
26. public boolean checkText(String content) throws SeleniumHelperNotExistException{
27. return GoogleGuis.SearchPage.PageTextContain(content);
28. }
29.
30. }
第四層,應(yīng)該是測(cè)試斷言層,這個(gè)部分用來(lái)執(zhí)行testcase。
Java代碼:
1. public class GoogleSearch extends SeleneseTestCase{
2.
3. public void setUp() throws Exception {
4. super.setUp("http://www.google.com/", "*iexplore");
5. com.asiainfo.selenium.gui.UIObjectHelper.SeleniumHelper=selenium;
6. }
7.
8. public void testNew() throws Exception {
9. GooglePageSearchComponent gpsc=new GooglePageSearchComponent();
10. gpsc.search("asiainfo");
11. assertTrue(gpsc.checkText("asiainfo"));
12.
13. }
14. }
如果使用testsuite應(yīng)該有第五層,這層主要用來(lái)組織testcase。
這樣的劃分,也是我的一點(diǎn)拙見(jiàn),我覺(jué)得還是后提高的空間的。相關(guān)的源代碼,我也上傳上來(lái),有興趣的可以在附件下載。