使用selenium在網站自動化測試中的實踐

發表于:2009-04-18來源:作者:點擊數: 標簽:seleniumSelenium自動化實踐
使用 selenium 在網站 自動化測試 中的實踐 最近用selenium在做網站自動化,按照google用selenium的經驗做了一些嘗試。 主要分成了4層,這樣做主要是為了對于代碼重用性的考慮。 第一層是UIobject,主要是對于頁面上的UI做了一些封裝 MI LY: Consolas, 'Cour

使用selenium在網站自動化測試中的實踐

最近用selenium在做網站自動化,按照google用selenium的經驗做了一些嘗試。 
     主要分成了4層,這樣做主要是為了對于代碼重用性的考慮。 
     第一層是UIobject,主要是對于頁面上的UI做了一些封裝 
     

MILY: Consolas, 'Courier New', Courier, mono; BACKGROUND-COLOR: transparent; WORD-WRAP: break-word">
Java代碼 javascripts/syntaxhighlighter/clipboard_new.swf width=14 height=15 type=application/x-shockwave-flash flashvars="clipboard=public%20class%20SelectUIObject%20extends%20UIObjiect%20%7B%0A%0A%20%20%20%20private%20SeleniumHelper%20helper%20%3D%20new%20SeleniumHelper()%3B%0A%20%20%20%20public%20String%20read(String%20locator)%7B%0A%20%20%20%20%20%20%20%20return%20%20helper.getSelectOptions(%20locator)%3B%0A%20%20%20%20%7D%3B%0A%20%20%20%20public%20void%20write(String%20locator%2C%20String%20value)%20%7B%0A%20%20%20%20%20%20%20String%20selectValue%20%3D%22label%3D%22%2Bvalue%3B%0A%20%20%20%20%20%20%20%20helper.select(locator%2C%20selectValue)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%20%20%20%20%20%20" quality="high" allowscriptaclearcase/" target="_blank" >ccess="always">
  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.          

   其中的SeleniumHelper是一些基本selenium的客戶端的封裝,如;
Java代碼 TestCase.browser.getSelectOptions(locator)%3B%0A%20%20%20%20%20%20%20%20String%20value%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20values.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%20value%20%2B%20values%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%20value%20%2B%20'%2C'%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20value%3B%0A%20%20%20%20%7D%0A%7D" quality="high" allowscriptaccess="always">
  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. }  

   第二層頁面層,主要是頁面的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.     }  

這里有些問題,主要頁面上的元素的定位,還是在代碼中寫死,對于頁面的改版還是需要修改源代碼 

 

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97