使用Selenium/Ant做Web應用遠端自動化測試

zm_21發表於2014-06-03

 

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

相關文章