Help! My Selenium tests take more than 8 hours to run.
We have a problem with too many tests that take too long to run. The Ant build script documented in a previous post only runs tests in sequence.
We could try running more than one at the same time.
Image recorded at the Warner Bros. Studio Tour London The Making of Harry Potter
Selenium has support to run Firefox tests in parallel. Selenium server is able to open multiple Firefox browsers and keep track of each of them in a separate session. Unfortunately there are issues with Internet Explorer that cause it not to be able to run in parallel.
Thanks to Solutions Daily blog http://solutionsdaily.blogspot.com/2010/06/making-junit-tests-run-parallely.html we now have a way to launch more than one Firefox test at a time.
The key is adding another ant jar called ant-contrib into your library. I downloaded it from here.
Then it has to be referenced in your build.xml file for Ant.
<classpath>
<pathelement location="C:/ant/apache-ant-1.8.4/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
This gives you the command called foreach which allows for a loop. This is used to start each test individually in rapid succession. The old way involved making a batch of all of the tests and running them one by one, waiting for each to finish.
Now the target for running Firefox tests needs to be set in a way to implement the foreach.
Mine looks like this now. This is the part that runs the foreach and passes each test name to the execute.test target. The inheritrefs part of foreach is what passes the data to the next target. Notice that I set maxthreads to 5. That is the most I have run at one time so far and is the default number set in Selenium Grid.
<target name="run-ff-tests" depends="compile" description="run your test suite in firefox" >
<foreach
target="execute.test"
maxthreads="5"
inheritall="true"
inheritrefs="true" parallel="true" param="test.source.absolute">
<path>
<fileset dir="${src}">
<include name="**/*Test*.java"/>
</fileset>
</path>
</foreach>
</target>
This is the target that runs one test at a time.
<target name="execute.test">
<!-- we need to have relative path -->
<pathconvert property="test.source.relative">
<fileset file="${test.source.absolute}" />
<map from="${src}/" to="" />
</pathconvert>
<!-- run one particular test -->
<junit fork="false" printsummary="true" haltonfailure="false" showoutput="true">
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
<sysproperty key="browser.property" value="firefox" />
<formatter type="xml" />
<batchtest todir="${reports}/ff-raw/">
<fileset dir="${src}">
<filename name="${test.source.relative}" />
</fileset>
</batchtest>
</junit>
</target>
Now we have been able to run more tests at one time for Firefox at least. To save time in running Internet Explorer tests we are turning to Selenium Grid. But that will wait until the next post.
By the way, this is one way to run unit tests in parallel using Ant also. Under certain conditions JUnit tests can be run in parallel and saves time in waiting for tests to finish after each build.
Take a look at our training at http://lmnsolutions.com/training.html
selenium@lmnsolutions.com

