WEB UI自动化之旅(一)

上一篇 / 下一篇  2009-12-14 19:59:51 / 个人分类:自动化测试

随着敏捷实践的引入,越来越多的公司注重产品的回归自动化测试

这里简单的介绍一下关于SELENIUM的用法,Selenium是由THOUGHWORKS公司开发的

基于WEB层面的自动化测试工具。它是由JAVASCRIPT开发的,支持PYTHON,RUBY,C#,JAVA,PHP,PERL等多种语言。它有三部分组成,Selenium-IDE:应用层录制工具;Selenium-RC(Remote Controller):支持不同语言的客户端驱动,包含Selenium-SERVER;Selenium-core,主要为JAVASCRIPT核心代码,对于selenium的功能扩展有帮助。

 

一般使用selenium选择TestNG or Junit框架,因为这两个框架都能和selenium,ANT很好的结合,并且都提供ResultReport的功能。准备好Selenium-RC,Junit(TestNG),我们的自动化之旅开始了。

 

这里有个之后会影响测试的BUG先解决掉,在Selenium1.0中对网页弹出窗口无法捕捉,我们怎么做呢?

在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;
   };

 

转为:

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自动化了。

 

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>

 

一般的步骤这里就不多说了,重要的是几点对您之后的回归测试将十分有用:

 

1,在每个Test Case中加入错误检查机制try……catch,由于selenium打开的是网页,在集成测试的时候使用selenium.stop来关闭selenium,所以如果在回归测试期间遇到不可预知的错误时需要将selenium关闭,防止后台进程累加。

 

2,对waitforpageload的使用和ThreadSleep的使用要间隔,保证场景的真实重现。之前的经历中由于waitforpageload的默认超时时间是30S,但有些特殊认证的时间只有15S,所以要保证场景的重现需要不断的调试。

 

3,Selenium的使用都是基于网页HTML元素的,所以和开发规范是紧密联系到一起的,正常开发设计过程域中的设计库中的编码规范中的命名规范提示我们在元素名字一定的情况下,之后的version保证唯一性,不变性,即使此元素被隐藏,其命名仍然存在。

 

4,对于SETUP和TEARDOWN类使用要做到即时建立,即时清除,保证数据库的清洁。避免MOCK数据对您的测试带来不必要的麻烦。

 

5,在遇到ALERT时有几种方法,新的SELENIUM貌似已经有方法验证ALERT了,如果还是不行您可以使用JAVA的AWT库中的ROBOT类去模拟实现电击ALERT。


TAG: ant Ant JUnit Selenium web 自动化 selenium junit selenium加强

 

评分:0

我来说两句

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 147621
  • 日志数: 22
  • 建立时间: 2009-10-14
  • 更新时间: 2010-07-27

RSS订阅

Open Toolbar