Client端主要是通过一个ant build文件来启动JUnit的TestCase的,进而启动TestCase中的test方法,连接并激活server端进行自动化测试。Client端核心测试单元的代码如下:
- package com.tail.p2test;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- import junit.textui.TestRunner;
- import com.thoughtworks.selenium.DefaultSelenium;
- import com.thoughtworks.selenium.Selenium;
- public class DemoTest extends TestCase {
- private Selenium selenium;
- public void setUp() throws Exception {
- String url = "http://localhost:8080/";
- selenium = new DefaultSelenium("localhost", 4444, "*chrome", url);
- selenium.start();
- }
- protected void tearDown() throws Exception {
- selenium.stop();
- }
- public void testNew() throws Exception {
- selenium.setTimeout("100000");
- selenium.open("/login.action");
- selenium.type("username", "admin");
- selenium.type("password", "123");
- selenium.click("//input[@value='Log In']");
- selenium.waitForPageToLoad("100000");
- Thread.sleep(10000);
- for (int second = 0;; second++) {
- if (second >= 60) fail("timeout");
- try { if (selenium.isElementPresent("signLabel")) break; } catch (Exception e) {}
- Thread.sleep(1000);
- }
- // omit lines
- ...
- selenium.open("/main.action");
- }
- }
当然,应用可以直接在Eclipse中运行,但是为了能更加灵活,我们考虑用ant脚本来控制client的运行,这里使用ant脚本的一个好处就是可以很方便快捷的输出测试报告,在本例中输出报告的目的就是那个report目录咯。
ant的Build.xml的脚本详细如下:
- <?xml version="1.0"?>
- <project name="portal" default="junit" basedir=".">
- <property name="source.dir" value="src" />
- <property name="build.dir" value="build" />
- <property name="lib.dir" value="lib" />
- <property name="classes.dir" value="${build.dir}/classes" />
- <property name="report.dir" value="report" />
- <!-- ================================================================== -->
- <!-- C L E A N -->
- <!-- ================================================================== -->
- <target name="clean">
- <delete dir="${classes.dir}" />
- <mkdir dir="${classes.dir}" />
- <delete dir="${report.dir}" />
- <mkdir dir="${report.dir}" />
- </target>
- <!-- ================================================================== -->
- <!-- C O M P I L E -->
- <!-- ================================================================== -->
- <target name="compile" depends="clean">
- <!-- local project jars -->
- <patternset id="lib.includes.compile">
- <include name="*.jar" />
- </patternset>
- <fileset dir="${lib.dir}" id="lib.compile">
- <patternset refid="lib.includes.compile" />
- </fileset>
- <pathconvert targetos="windows" property="libs.compile" refid="lib.compile" />
- <!-- compile -->
- <javac srcdir="${source.dir}" destdir="${classes.dir}" classpath="${libs.compile}" includes="**/*.java" debug="true">
- </javac>
- </target>
- <!-- ================================================================== -->
- <!-- J U N I T -->
- <!-- ================================================================== -->
- <target name="junit" depends="compile">
- <junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
- <classpath>
- <pathelement path="${classes.dir}" />
- <fileset dir="${lib.dir}">
- <include name="**/*.jar" />
- </fileset>
- </classpath>
- <formatter type="xml" />
- <batchtest todir="${report.dir}">
- <fileset dir="${classes.dir}">
- <include name="**/*Test.*" />
- </fileset>
- </batchtest>
- </junit>
- <junitreport todir="${report.dir}">
- <fileset dir="${report.dir}">
- <include name="TEST-*.xml" />
- </fileset>
- <report format="frames" todir="${report.dir}" />
- </junitreport>
- <fail if="tests.failed">
- </fail>
- </target>
- </project>
以后,你只需要在work目录下执行一个简单的 ant 命令就能轻松运行整个测试了。