`
dtrex
  • 浏览: 138139 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用selenium进行web测试项目框架

阅读更多
最近看了一下selenium如果要把这个用于自动化测试,需要进行整理,形成一个框架,我也对Google搜索这样简单的功能做了一些尝试,形成了一个简单的框架,简单的说应该有四层:
第一层应该是UIObject这个对象层,主要是用来封装对象的操作方法,例如:
public class TextFieldUIObject extends UIObject {

	/**
	 * 构造函数用于构造textfield对象
	 * @param locator 描述信息
	 */
	public TextFieldUIObject(String locator)
	{
		super(locator);
	}
	/**
	 * 向textfield输入值
	 * @param content 输入的内容
	 * @throws SeleniumHelperNotExistException
	 */
	public void type(String content) throws SeleniumHelperNotExistException
	{
		if(UIObjectHelper.SeleniumHelper==null) throw new SeleniumHelperNotExistException();
		UIObjectHelper.SeleniumHelper.type(this.locator,content);
	}
}

该代码,封装了textfield的控件,加入了方法type用于输入

第二层主要是构件层,主要用来描述页面上的控件,这里我用了最简单的静态变量的方法,还可以使用yml,xml,json甚至某种格式的文本文件进行描述,之后根据文件生成,这样可能会更加方面修改。
代码如下:
public class GoogleGuis {
	public static PageUIObject SearchPage = new PageUIObject("/");
	public static TextFieldUIObject SearchInput = new TextFieldUIObject("q");
	public static ButtonUIObject SearchButton =  new ButtonUIObject("btnG");
}


第三层应该叫组件层,可以页面切分成大组件,然后对组件进行相关的操作,这里把Google的搜索作为一个组件,代码如下:
/**
 * 组件类
 * @author renzq
 *
 */
public class GooglePageSearchComponent {
	
	/**
	 * 进行查询操作
	 * @param content 查询的内容
	 * @throws SeleniumHelperNotExistException
	 */
	public void search(String content) throws SeleniumHelperNotExistException{
		GoogleGuis.SearchPage.PageOpen();
		GoogleGuis.SearchInput.type(content);
		GoogleGuis.SearchButton.click();
		GoogleGuis.SearchPage.WaitForPageReady("3000");
		
	}
	/**
	 * 校验查询结果是否含有内容
	 * @param content 内容
	 * @return 根据是否含有,返回判断的值
	 * @throws SeleniumHelperNotExistException
	 */
	public boolean checkText(String content) throws SeleniumHelperNotExistException{
		return GoogleGuis.SearchPage.PageTextContain(content);
	}
	
}


第四层,应该就是测试断言层,这个部分用来执行testcase
public class GoogleSearch extends SeleneseTestCase{

	public void setUp() throws Exception {
		super.setUp("http://www.google.com/", "*iexplore");
		com.asiainfo.selenium.gui.UIObjectHelper.SeleniumHelper=selenium;
	}
	
	 public void testNew() throws Exception {
		 GooglePageSearchComponent gpsc=new GooglePageSearchComponent();
		 gpsc.search("asiainfo");
		 assertTrue(gpsc.checkText("asiainfo"));
        
   }
}


如果使用testsuite就应该有第五层,这层主要用来组织testcase


这样的划分,也是我的一点拙见,我觉得还是后提高的空间的。相关的源代码,我也上传上来,有兴趣的可以在附件下载。
分享到:
评论
2 楼 test803 2013-06-04  
selenium_java_code_.rar 无法下载啊
1 楼 jiangduxi 2010-02-24  
不错,学习了

相关推荐

Global site tag (gtag.js) - Google Analytics