开发者

Ant task for selenium script

I have written a test script. I am using Java, JUnit, and Selenium RC. I want run this script using an ant command.

How do I w开发者_JAVA技巧rite an ant task and setup my build.xml file to run this new task?


All a selenium test case is is just an extension of a JUnit test case. So all you need to do is just run a junit test case in ant. The ant manual shows how to do this very clearly.


I use the OnDemand service from http://saucelabs.com which absolutely rocks. Below is my Ant script for running my JUnit test against SauceLabs Selenium servers.

You can skip the sysproperty attributes in the junit task if you don't want to pass in any parameters to you test. Retrieve the parameters in your Java test methods with System.getProperty("..."). I do find it handy to be able to specify platform, browser and version from the Ant script and not hardcode them in the test cases.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="TestingStudieplads" basedir="." default="main">

    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="main-class" value="TestingStudieplads"/>
    <property name="lib.dir" value="selenium-2.24.1"/>
    <property name="test.name" value="TestingStudieplads"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <pathelement location="${classes.dir}" />
    </path>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath"/>
    </target>

    <target name="main" depends="clean,run"/>

    <target name="run" depends="compile">

        <junit fork="yes" haltonfailure="yes">

            <test name="${test.name}"/>

            <sysproperty key="browser.name" value="Chrome"/>
            <sysproperty key="browser.platform" value="Linux"/>
            <sysproperty key="browser.version" value=""/>

            <formatter type="plain" usefile="false"/>
            <classpath refid="classpath"/>
        </junit>

    </target>

</project>

My directory layout is

Test
|- build.xml
|- src
|  | TestingStudieplads.java
|- selenium-2.24.1
   | CHANGELOG
   | selenium-java-2.24.1.jar
   | libs
      | dep1.jar
      | dep2.jar
      | dep3.jar
      | ...


First you have to download ANT from Apache ANT. Then you extract that zip file and place to your drive. You have to set the class path and give the ANT path on classpath. After this, open a build.xml under the current project, write the jar file location in 76 line and write Suite name. Go to command prompt and type ant compile and ant run.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜