您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium--WEB層面的自動(dòng)化測試工具
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/15 14:24:41 ] 推薦標(biāo)簽:

  Selenium是由THOUGHWORKS公司開發(fā)的基于WEB層面的自動(dòng)化測試工具。它是由JAVASCRIPT開發(fā)的,支持PYTHON,RUBY,C#,JAVA,PHP,PERL 等多種語言。它有三部分組成,Selenium-IDE:應(yīng)用層錄制工具;Selenium-RC(Remote Controller):支持不同語言的客戶端驅(qū)動(dòng),包含Selenium-SERVER;Selenium-core,主要為JAVASCRIPT核心代碼,對(duì)于selenium的功能擴(kuò)展有幫助。

  一般使用selenium選擇TestNG or Junit框架,因?yàn)檫@兩個(gè)框架都能和selenium,ANT很好的結(jié)合,并且都提供ResultReport的功能。準(zhǔn)備好Selenium-RC,Junit(TestNG),我們的自動(dòng)化之旅開始了。

  這里有個(gè)之后會(huì)影響測試的BUG先解決掉,在Selenium1.0中對(duì)網(wǎng)頁彈出窗口無法捕捉,我們?cè)趺醋瞿兀?/p>

  在selenium-server.jar中core文件夾下scripts文件下的selenium-browerbot.js中修改如下的JS代碼:

var newOpen = function(url, windowName, windowFeatures, replaceFlag) {
       var myOriginalOpen = originalOpen;
       if (isHTA) {
           myOriginalOpen = this[originalOpenReference];
       }
       if (windowName == "" || windowName == "_blank") {
           windowName = "selenium_blank" + Math.round(100000 * Math.random());
           LOG.warn("Opening window '_blank', which is not a real window name. Randomizingtarget to be: " + windowName);
       }
       var penedWindow = myOriginalOpen(url, windowName, windowFeatures, replaceFlag);
       LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is "" + windowName + """);
       if (windowName!=null) {
           openedWindow["seleniumWindowName"] = windowName;
       }
       selenium.browserbot.openedWindows[windowName] = openedWindow;
       return openedWindow;
   };

  轉(zhuǎn)為:

var newOpen = function(url, windowName, windowFeatures, replaceFlag) {
       // var myOriginalOpen = originalOpen;
       //var myOriginalOpen = window.open;
         if (isHTA) {
           // myOriginalOpen = this[originalOpenReference];
         }
         if( !windowFeatures )
         {
         windowFeatures = null;
         }
         if( !replaceFlag )
         {
         replaceFlag = null;
         }

         var penedWindow = null;
         if( !windowFeatures && !replaceFlag )
         {
           openedWindow = this.window.open(url, windowName);
         }
         else
         {
         openedWindow = this.window.open(url, windowName, windowFeatures, replaceFlag);
         }
         LOG.debug("window.open call intercepted; window ID (which you can use withselectWindow()) is "" + windowName + """);
         if (windowName!=null) {
             openedWindow["seleniumWindowName"] = windowName;
         }

         if(openedWindow != null)
         {
             selenium.browserbot.openedWindows[windowName] = openedWindow;
             return openedWindow;
         }
         return null;
      
     };

  這樣您能開始WEB自動(dòng)化了。

ANT的腳本主要以JAVA編譯和JunitReport的Task為主:

<!--
target: compile Compile all test cases.
-->
   <target name="compile" depends="init-test">
       <javac srcdir="${src-dir}" destdir="${out-dir}">
           <classpath refid="classpath.all" />
       </javac>
   </target>
   <!--
       target: test run the unit test
   -->
   <target name="test" depends="init-test">
       <junit fork="yes" printsummary="on" maxmemory="100m">
           <classpath refid="classpath.all" />
           <formatter type="xml" />
           <batchtest todir="${junit-xml-dir}">
               <fileset dir="${out-dir}">
                   <include name="**/AllTest*.class" />
                   <exclude name="**/*$*.class" />
               </fileset>
           </batchtest>
       </junit>
       <junitreport todir="${junit-xml-dir}">
           <fileset dir="${junit-xml-dir}">
               <include name="TEST-*.xml" />
           </fileset>
           <report format="frames" todir="${junit-html-dir}" />
       </junitreport>
   </target>
   <!--
   Public Targets
   -->
   <target name="clean">
       <delete quiet="true" includeEmptyDirs="true">
           <fileset dir="${report-dir}">
               <exclude name=".cvsignore" />
               <exclude name="CVS" />
           </fileset>
           <fileset dir="${out-dir}">
           </fileset>
       </delete>
   </target>
   <!--
   Global Targets
   -->
   <target name="junit" depends="clean, compile, test" />
</project>


一般的步驟這里不多說了,重要的是幾點(diǎn)對(duì)您之后的回歸測試將十分有用:

  1、在每個(gè)Test Case中加入錯(cuò)誤檢查機(jī)制try……catch,由于selenium打開的是網(wǎng)頁,在集成測試的時(shí)候使用selenium.stop來關(guān)閉 selenium,所以如果在回歸測試期間遇到不可預(yù)知的錯(cuò)誤時(shí)需要將selenium關(guān)閉,防止后臺(tái)進(jìn)程累加。

  2、對(duì)waitforpageload的使用和ThreadSleep的使用要間隔,保證場景的真實(shí)重現(xiàn)。之前的經(jīng)歷中由于waitforpageload的默認(rèn)超時(shí)時(shí)間是30S,但有些特殊認(rèn)證的時(shí)間只有15S,所以要保證場景的重現(xiàn)需要不斷的調(diào)試。

  3、Selenium的使用都是基于網(wǎng)頁HTML元素的,所以和開發(fā)規(guī)范是緊密聯(lián)系到一起的,正常開發(fā)設(shè)計(jì)過程域中的設(shè)計(jì)庫中的編碼規(guī)范中的命名規(guī)范提示我們?cè)谠孛忠欢ǖ那闆r下,之后的version保證性,不變性,即使此元素被隱藏,其命名仍然存在。

  4、對(duì)于SETUP和TEARDOWN類使用要做到即時(shí)建立,即時(shí)清除,保證數(shù)據(jù)庫的清潔。避免MOCK數(shù)據(jù)對(duì)您的測試帶來不必要的麻煩。

  5、在遇到ALERT時(shí)有幾種方法,新的SELENIUM貌似已經(jīng)有方法驗(yàn)證ALERT了,如果還是不行您可以使用JAVA的AWT庫中的ROBOT類去模擬實(shí)現(xiàn)電擊ALERT。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd