Integrating Selenium 2 with Ant

We recently integrated Selenium 2 into the Ant build of one of our projects. In this blog post I will show you how you can integrate your automated JUnit based Selenium 2 tests with Ant. If you are using Maven instead, you should probably read the german blog post Automatisierte Selenium-Tests mit Maven. The build script snippets in this blog post should also work with Selenium 1 or other web application test tools that can be used with JUnit.

Before the tests can be started, you need to deploy your application to a servlet container and start the server. After your tests have finished, you have to shut down the server container. I assume that you already have Ant targets to start and stop your server. In the following example these targets are called tomcat.start and tomcat.stop.

First define an Ant target called selenium.test. This target should depend on a compile target and on a target to deploy your application to the servlet container. These targets should already exist in your build.xml. Here is the actual Ant target:

<target name="selenium.test" depends="compile.test, webapp.deploy" description="Deploy application, start Tomcat, run Selenium tests, shutdown Tomcat">
	    	<antcall target="tomcat.start"/>
    			<waitfor maxwait="2" maxwaitunit="minute">
    				<http url="${selenium.browserURL}"/>
    			<mkdir dir="${results}/selenium-tests" />
				<junit fork="true" forkmode="once" haltonfailure="false" printsummary="yes" tempdir="${target}" failureproperty="selenium.test.successful">
					<classpath path="${classes}" />
					<classpath path="${classes.test}" />
					<classpath refid="classpath.compile" />
					<classpath refid="classpath.test" />
					<batchtest todir="${results}/selenium-tests">
						<fileset refid="files.test.selenium" />
					<formatter type="xml" />
				<mkdir dir="${reports}/selenium-tests" />
				<junitreport todir="${reports}/selenium-tests">
					<fileset dir="${results}/selenium-tests">
						<include name="TEST-*.xml" />
					<report todir="${reports}/selenium-tests" />
	    		<antcall target="tomcat.stop"/>
    			<fail if="selenium.test.successful" message="Selenium test(s) failed!"/>

Because the tomcat.start target will block until the server has been stopped, you cannot define a dependency to that target. You need to call the tomcat.start target and the actual test tasks in parallel. The test execution has to wait until the servlet container is ready, so it periodically checks a URL (defined via the property selenium.browserURL) until the application is availlable.

The properties needed are as follows:

  • target – points to your target folder, e.g. ${basedir}/target
  • results – points to a folder where the test results will be saved, e.g. ${target}/results
  • reports – points to a folder where the test reports will be saved, e.g. ${target}/reports
  • classes – points to the folder where your compiled classes are located, e.g. ${target}/classes
  • classes.test – points to the folder where your compiled classes are located, e.g. ${target}/classes-test
  • selenium.browserURL – URL of your web application deployed to your local servlet container, e.g. http://localhost:8080/myapp/

Additionally, you need to define the following paths:

  • classpath.compile – fileset with all required libraries for your application
  • classpath.test – fileset with all required libraries to run the tests

This is an example of these path difinitions:

<path id="classpath.compile">
	<fileset dir="${lib.compile}">
		<include name="**/*.jar" />
<path id="classpath.test">
	<fileset dir="${lib.test}">
		<include name="**/*.jar" />

Finally you need to define a fileset files.test.selenium that includes all Selenium test classes. E.g. you could define to include all files that end with Test.class in a subdirectory of the selenium folder:

<fileset id="files.test.selenium" dir="${classes.test}">
	<include name="**/selenium/**/*Test.class" />
Short URL for this post:
This entry was posted in Build, config and deploy, Java and Quality, Java Web Frameworks and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *