使用Selenium/Ant做Web应用远程自动化测试

来源:互联网 发布:rar在mac怎么打开 编辑:程序博客网 时间:2024/05/29 16:27

 

Client端主要是通过一个ant build文件来启动JUnit的TestCase的,进而启动TestCase中的test方法,连接并激活server端进行自动化测试。Client端核心测试单元的代码如下:

Java代码 复制代码 收藏代码
  1. package com.tail.p2test;   
  2.   
  3. import junit.framework.Test;   
  4. import junit.framework.TestCase;   
  5. import junit.framework.TestSuite;   
  6. import junit.textui.TestRunner;   
  7.   
  8. import com.thoughtworks.selenium.DefaultSelenium;   
  9. import com.thoughtworks.selenium.Selenium;   
  10.   
  11. public class DemoTest extends TestCase {   
  12.     private Selenium selenium;   
  13.        
  14.     public void setUp() throws Exception {   
  15.         String url = "http://localhost:8080/";   
  16.         selenium = new DefaultSelenium("localhost"4444"*chrome", url);   
  17.         selenium.start();   
  18.     }   
  19.   
  20.     protected void tearDown() throws Exception {   
  21.         selenium.stop();           
  22.     }   
  23.        
  24.     public void testNew() throws Exception {   
  25.         selenium.setTimeout("100000");   
  26.         selenium.open("/login.action");   
  27.         selenium.type("username""admin");   
  28.         selenium.type("password""123");   
  29.         selenium.click("//input[@value='Log In']");   
  30.         selenium.waitForPageToLoad("100000");   
  31.         Thread.sleep(10000);   
  32.         for (int second = 0;; second++) {   
  33.             if (second >= 60) fail("timeout");   
  34.             try { if (selenium.isElementPresent("signLabel")) break; } catch (Exception e) {}   
  35.             Thread.sleep(1000);   
  36.         }   
  37.         // omit lines   
  38.         ...   
  39.         selenium.open("/main.action");   
  40.     }   
  41. }  


当然,应用可以直接在Eclipse中运行,但是为了能更加灵活,我们考虑用ant脚本来控制client的运行,这里使用ant脚本的一个好处就是可以很方便快捷的输出测试报告,在本例中输出报告的目的就是那个report目录咯。

ant的Build.xml的脚本详细如下:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0"?>  
  2.   
  3. <project name="portal" default="junit" basedir=".">  
  4.  <property name="source.dir" value="src" />  
  5.  <property name="build.dir" value="build" />    
  6.  <property name="lib.dir" value="lib" />  
  7.  <property name="classes.dir" value="${build.dir}/classes" />  
  8.  <property name="report.dir" value="report" />  
  9.   
  10.  <!-- ================================================================== -->  
  11.  <!-- C L E A N                                                          -->  
  12.  <!-- ================================================================== -->  
  13.  <target name="clean">  
  14.   <delete dir="${classes.dir}" />  
  15.   <mkdir dir="${classes.dir}" />  
  16.   <delete dir="${report.dir}" />  
  17.   <mkdir dir="${report.dir}" />     
  18.  </target>  
  19.   
  20.  <!-- ================================================================== -->  
  21.  <!-- C O M P I L E                                                      -->  
  22.  <!-- ================================================================== -->  
  23.  <target name="compile" depends="clean">  
  24.   <!-- local project jars -->  
  25.   <patternset id="lib.includes.compile">  
  26.    <include name="*.jar" />  
  27.   </patternset>  
  28.   <fileset dir="${lib.dir}" id="lib.compile">  
  29.    <patternset refid="lib.includes.compile" />  
  30.   </fileset>  
  31.   <pathconvert targetos="windows" property="libs.compile" refid="lib.compile" />  
  32.   <!-- compile -->  
  33.   <javac srcdir="${source.dir}" destdir="${classes.dir}" classpath="${libs.compile}" includes="**/*.java" debug="true">  
  34.   </javac>  
  35.  </target>  
  36.   
  37.  <!-- ================================================================== -->  
  38.  <!-- J U N I T                                                          -->  
  39.  <!-- ================================================================== -->  
  40.  <target name="junit" depends="compile">  
  41.   <junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">  
  42.    <classpath>  
  43.     <pathelement path="${classes.dir}" />  
  44.     <fileset dir="${lib.dir}">  
  45.      <include name="**/*.jar" />  
  46.     </fileset>  
  47.    </classpath>  
  48.    <formatter type="xml" />  
  49.    <batchtest todir="${report.dir}">  
  50.     <fileset dir="${classes.dir}">  
  51.      <include name="**/*Test.*" />  
  52.     </fileset>  
  53.    </batchtest>  
  54.   </junit>  
  55.   <junitreport todir="${report.dir}">  
  56.    <fileset dir="${report.dir}">  
  57.     <include name="TEST-*.xml" />  
  58.    </fileset>  
  59.    <report format="frames" todir="${report.dir}" />  
  60.   </junitreport>  
  61.   <fail if="tests.failed">  
  62.   </fail>  
  63.  </target>  
  64. </project>  
以后,你只需要在work目录下执行一个简单的 ant 命令就能轻松运行整个测试了。 

origin: http://tailsherry.iteye.com/blog/191539

0 0