selenium在网站自动化测试中的实践

最近用selenium在做网站自动化,按照google用selenium的经验做了一些尝试。
主要分成了4层,这样做主要是为了对于代码重用性的考虑。
第一层是UIobject,主要是对于页面上的UI做了一些封装

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的客户端的封装,如;
    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的封装 。如

/**
* 搜索价格
* @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");
}
这里有些问题,主要页面上的元素的定位,还是在代码中写死,对于页面的改版还是需要修改源代码
第三层测试层,测试的断言验证的
    @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);
}


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值