Selenium and Ant

Here is an ant file which will check a bit of environment stuff, launch the selenium server, run my tests (a custom metaframework), and then stop the server. Previously I was just running this whenever someone asked me to, but it was decided that it should be run as part of the nightly build which is controlled through ant.

<project name="Run Test" default="run_test" basedir=".">
 
    <!-- sensible defaults; override as necessary -->
    <property name="config_file" value="c:\path\to\framework\config.xml" />
    <available file="${config_file}" property="config.ok" />
 
    <!-- see http://adam.goucher.ca/?p=21 for an explanation -->
    <property name="test_filter" value="ro_cas" />
 
    <target name="run_test" depends="check_jvm, check_config" description="Start Proxy ; Run Tests ; stop Proxy">
        <antcall target="start-server"></antcall>
 
        <exec executable="jams.bat" >
            <arg value="-c ${config_file}"/>
            <arg value="${test_filter}" />
        </exec>
 
        <antcall target="stop-server"></antcall>
    </target>
 
    <target name="check_jvm" depends="get-jvm" unless="jvm.ok">
        <fail message="You need to use at least Java 1.5 for the selenium tests" />
    </target>
 
    <target name="get-jvm">
        <condition property="jvm.ok">
            <not>
                <or>
                     <equals arg1="${ant.java.version}" arg2="1.4"/>
                </or>
            </not>
        </condition>
    </target>
 
    <target name="check_config" unless="config.ok">
        <fail message="The config file you specified does not exist" />
    </target>
 
    <target name="start-server">
        <!-- this is a 'known location' for this -->
        <java jar="../server/selenium-server-1.0-SNAPSHOT-standalone.jar"
              fork="true"
              spawn="true">
 
            <arg line="-proxyInjectionMode"/>
        </java>
 
        <waitfor maxwait="30" maxwaitunit="second">
            <and>
                 <socket server="localhost" port="4444"/>
                 <!-- this url will 403, so we say that it should start counting errors at 404 to skip -->
                 <http url="http://localhost:4444/selenium-server/core/index.html" errorsBeginAt="404"/>
            </and>
        </waitfor>
    </target>
 
    <target name="stop-server">
        <get taskname="selenium-shutdown" 
            src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"	
            dest="result.txt" ignoreerrors="true" />
        <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
    </target>
</project>

Like most things these days, I can’t claim the original work for everything involved; I just the following together:

Post a Comment

Your email is never published nor shared. Required fields are marked *