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

原创粉丝点击