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

猜你喜欢

转载自yangzx.iteye.com/blog/352784
今日推荐