selenium在网站自动化测试中的实践
来源:互联网 发布:java字符流读取文件 编辑:程序博客网 时间:2024/06/05 00:33
最近用selenium在做网站自动化,按照google用selenium的经验做了一些尝试。
主要分成了4层,这样做主要是为了对于代码重用性的考虑。
第一层是UIobject,主要是对于页面上的UI做了一些封装
Java代码
1. public class SelectUIObject extends UIObjiect {
2.
3. private SeleniumHelper helper = new SeleniumHelper();
4. public String read(String locator){
5. return helper.getSelectOptions( locator);
6. };
7. public void write(String locator, String value) {
8. String selectValue ="label=" +value;
9. helper.select(locator, selectValue);
10. }
11. }
12.
public class SelectUIObject extends UIObjiect {
private SeleniumHelper helper = new SeleniumHelper();
public String read(String locator){
return helper.getSelectOptions( locator);
};
public void write(String locator, String value) {
String selectValue ="label="+value;
helper.select(locator, selectValue);
}
}
其中的SeleniumHelper是一些基本selenium的客户端的封装,如;
Java代码
1. public String getSelectOptions(String locator) {
2. String[] values = SeleniumTestCase.browser.getSelectOptions(locator);
3. String value = "" ;
4. for ( int i = 0 ; i < values.length; i++) {
5. value = value + values[i];
6. value = value + ',' ;
7. }
8. return value;
9. }
public String getSelectOptions(String locator) {
String[] values = SeleniumTestCase.browser.getSelectOptions(locator);
String value = "";
for (int i = 0; i < values.length; i++) {
value = value + values[i];
value = value + ',';
}
return value;
}
}
第二层页面层,主要是页面的ui的封装 。如
Java代码
1. /**
2. * 搜索价格
3. * @param price 单位下拉
4. * @param maxPrice 最高价格
5. * @param minPrice 最低价格
6. */
7. public void searchPrice(String price,String maxPrice,String minPrice) {
8. selectUI.write("o.121" , price);
9. textUI.write("o.price" , minPrice);
10. textUI.write("document.forms[1].elements[4]" , maxPrice);
11. LinkUI.write("document.forms[1].elements[5]" );
12. waitForPageToLoad("30000" );
13. }
/**
* 搜索价格
* @param price 单位下拉
* @param maxPrice 最高价格
* @param minPrice 最低价格
*/
public void searchPrice(String price,String maxPrice,String minPrice) {
selectUI.write("o.121", price);
textUI.write("o.price", minPrice);
textUI.write("document.forms[1].elements[4]", maxPrice);
LinkUI.write("document.forms[1].elements[5]");
waitForPageToLoad("30000");
}
这里有些问题,主要页面上的元素的定位,还是在代码中写死,对于页面的改版还是需要修改源代码
第三层测试层,测试的断言验证的
Java代码
1. @Test
2. public void testIndex() throws FileNotFoundException, InterruptedException {
3. int count = 15 ;
4. PostClient client = new PostClient();
5. init_date();
6. client.login("this92" , "123456" );
7. client.openUrl(TestUtil.Site,TestUtil.PostURL);
8. client.waitForPageToLoad("30000" );
9. postView(list,UIObjects,count);
10. }
@Test
public void testIndex() throws FileNotFoundException, InterruptedException {
int count = 15;
PostClient client = new PostClient();
init_date();
client.login("this92", "123456");
client.openUrl(TestUtil.Site,TestUtil.PostURL);
client.waitForPageToLoad("30000");
postView(list,UIObjects,count);
}
Java代码
1. public void init_date() throws FileNotFoundException{
2. UIObjects = Yaml.loadType(new FileInputStream( new File( + "post_view_UI.yml" )), HashMap. class );
3. list = Yaml.loadType(new FileInputStream( new File(+ "post_view_value.yml" )), HashMap. class );
4. }
public void init_date() throws FileNotFoundException{
UIObjects = Yaml.loadType(new FileInputStream(new File( +"post_view_UI.yml")), HashMap.class);
list = Yaml.loadType(new FileInputStream(new File(+"post_view_value.yml")), HashMap.class);
}
这些用yml存放一些数据。
还有最后一层,是对于测试数据的封装,比如email的生成,还有一些公用方法的封装。
目前从效果,比以前把所有的逻辑写在testcase中提高了一定的重用性,对于页面的改版只要需要修改部分页面层,测试逻辑那块基本上不需要修改。
google的视频资源 http://www.youtube.com/watch?v=hWQdCdH77NA
- selenium在网站自动化测试中的实践
- Selenium自动化测试在
- Selenium自动化测试相关网站
- jenkins在自动化测试中的应用(Selenium+Python+Jenkins)
- jenkins在自动化测试中的应用(Selenium+Python+Jenkins)
- 57.Python项目在Jenkins中的自动化测试实践
- 全新设计的《开源Selenium自动化测试实践》课程大纲
- Selenium自动化测试实践 公开班(广州)
- Selenium官网教程_selenium自动化测试实践
- 基于Selenium的UI自动化测试屏幕截图功能实践
- 使用Selenium通过浏览器对网站进行自动化测试
- 应用Selenium + NUNIT对动态WEB测试自动化(自动化测试开发实践系列)
- Selenium 自动化测试工具
- Selenium 自动化测试系统
- Selenium 自动化验收测试
- python + selenium 自动化测试
- junit+selenium自动化测试
- 自动化测试:selenium
- Mysql的时间函数
- 学知网
- ulimit
- 斯坦福大学开放课程——编程方法 作业6
- 北京税后工资计算方式(仅供参考)
- selenium在网站自动化测试中的实践
- Rational Purify 使用及分析实例
- Silverlight加载xap 使用WCF
- warning:incompatible implicit declaration of built-in function
- 推荐一款地图影像下载工具
- Database Replay
- C语言的(...)参数
- Oracle dbms_job package 用法小结
- 关于PopupWindow的代码