近看了一下selenium如果要把这个用于自动化测试,需要进行整理,形成一个框架,我也对Google搜索这样简单的功能做了一些尝试,形成了一个简单的框架,简单的说应该有四层:
第一层应该是UIObject这个对象层,主要是用来封装对象的操作方法,例如:
Java代码:
1. public class TextFieldUIObject extends UIObject { 2. 3. /** 4. * 构造函数用于构造textfield对象 5. * @param locator 描述信息 6. */ 7. public TextFieldUIObject(String locator) 8. { 9. super(locator); 10. } 11. /** 12. * 向textfield输入值 13. * @param content 输入的内容 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用于输入。
第二层主要是构件层,主要用来描述页面上的控件,这里我用了简单的静态变量的方法,还可以使用yml,xml,json甚至某种格式的文本文件进行描述,之后根据文件生成,这样可能会更加方面修改。
代码如下:
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. }
第三层应该叫组件层,可以页面切分成大组件,然后对组件进行相关的操作,这里把Google的搜索作为一个组件,代码如下:
1. /** 2. * 组件类 3. * @author renzq 4. * 5. */ 6. public class GooglePageSearchComponent { 7. 8. /** 9. * 进行查询操作 10. * @param content 查询的内容 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. * 校验查询结果是否含有内容 22. * @param content 内容 23. * @return 根据是否含有,返回判断的值 24. * @throws SeleniumHelperNotExistException 25. */ 26. public boolean checkText(String content) throws SeleniumHelperNotExistException{ 27. return GoogleGuis.SearchPage.PageTextContain(content); 28. } 29. 30. }
第四层,应该是测试断言层,这个部分用来执行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应该有第五层,这层主要用来组织testcase。
这样的划分,也是我的一点拙见,我觉得还是后提高的空间的。相关的源代码,我也上传上来,有兴趣的可以在附件下载。