Running Selenium Webdriver tests in Jenkins using Ant

In a previous post I introduced a very simple and straightforward way to run data-driven tests in Selenium Webdriver using test data stored in an Excel sheet. In this post, I want to show how to run these tests using a continuous integration (CI-) solution.

My preferred CI-tool is Jenkins, as it is open source, very flexible and easy to use.
First, make sure that Jenkins is set up properly and is running as a service. Installation is very easy, so I won’t go into details here.

I also recommend using Ant as a software build tool to further ease the process of compiling and running our tests. While it is not strictly necessary to use Ant, it will make life a lot easier for us. Again, install Ant and make sure it is running smoothly by typing ant on the command prompt. If it starts asking for a build.xml file, it’s running properly.


Next, open the Selenium Webdriver project in your IDE. Again, I prefer using Eclipse, so the images shown here will be based on Eclipse. Other IDEs such as IntelliJ usually provide the same functionality, it’s just hidden behind different menu options.

Before we start configuring our project for using Ant and running in Jenkins, we are going to add some flexibility to it. Jenkins uses its own workspace and might be running on another server altogether. However, our Selenium project contains a reference to a locally stored Excel data source. Therefore, we are going to add the possibility to provide the path to the data source to be used as an argument to our main method in the Selenium test. In this way, we can simply specify the location of the data source when we run the test. Not only is this necessary to have our test run smoothly in Jenkins, it also adds the possibility to execute several test runs with separate test data sets.

public static void main (String args[]) {
		String strPath;
		if (args.length == 1) {
			strPath = args[0];
		} else {
			strPath = "Z:\\Documents\\Bas\\blog\\datasources\\testdata.xls";
		try {
			// Open the Excel file
			FileInputStream fis = new FileInputStream(new File(strPath).getAbsolutePath());

If an argument is specified when running the main method of our test, we assume this is a relative path to our Excel data source. In order to be able to use it, all we have to do is to get the absolute path for it and off we go. If no argument is specified, we use the default Excel file.

Next, we create a build.xml file that provides Ant with the necessary instructions and details on how to build and run our test. In Eclipse, this can be done easily by right-clicking our project and selecting ‘Export > General > Ant Buildfiles’. After selecting the appropriate project, a build.xml file is generated and added to the root of our project. The example below is a part of the resulting file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. --><project basedir="." default="build" name="seleniumTest">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="C:/Tools/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="seleniumTest.classpath">
    <pathelement location="bin"/>
    <pathelement location="../../../../../vmware-host/Shared Folders/Documents/Bas/blog/libs/selenium-server-standalone-2.37.0.jar"/>
    <pathelement location="../../../../../vmware-host/Shared Folders/Documents/Bas/blog/libs/poi-3.9-20121203.jar"/>
<target name="init">
    <mkdir dir="bin"/>
    <copy includeemptydirs="false" todir="bin">
        <fileset dir="src">
            <exclude name="**/*.java"/>
<target name="clean">
    <delete dir="bin"/>

Before we can run our tests using Ant, we need to make two modifications.

First, we need to clean up the classpath as we want to use clear and relative paths. Make sure that the necessary libraries can be found on the designated locations. These are specified relative to the location of the build.xml file.

<path id="seleniumTest.classpath">
    <pathelement location="bin"/>
    <pathelement location="libs/selenium-server-standalone-2.37.0.jar"/>
    <pathelement location="libs/poi-3.9-20121203.jar"/>

Next, we need to make sure that our Excel data source is specified as an argument in the designated Ant target:

<target name="ExcelDataDriven">
    <java classname="com.ontestautomation.selenium.ExcelDataDriven" failonerror="true" fork="yes">
        <arg line="datasources/testdata.xls"/>
        <classpath refid="seleniumTest.classpath"/>

Now, we can execute our test using Ant either in Eclipse or at the command line. When you choose the latter, go to the subdirectory for the Selenium project in your workspace (build.xml should be located there) and execute ant <> (ant ExcelDataDriven in this case). You’ll see that the test is run successfully using Ant.


The final step is to have this step performed by Jenkins. This should be very straightforward now. Create a new job in Jenkins and add ‘Invoke Ant’ as a build step. Specify the correct target (again, ExcelDataDriven in our case).


Make sure that all referenced libraries and data sources can be found at the correct locations in the workspace for the Jenkins job (this is where using relative paths comes in handy!). Normally, you would do this using some sort of version control sytem such as Subversion. Next, schedule a build for the job, which should run smoothly now:


That’s it, we’ve now successfully run our Selenium Webdriver tests in Jenkins using Ant. One step closer to a successful continuous integration approach!

22 thoughts on “Running Selenium Webdriver tests in Jenkins using Ant

  1. Pingback: Testing Peru – Integración Continua con Jenkins y usando Selenium WebDriver

  2. Hi,

    I would just like to know if the above set up will work in Jenkins when I’m using TestNg as well?

    Pls let me know how to do it.


    • Hi Karla,

      I haven’t used the Selenium + TestNg + Jenkins combination myself before, but I think that if you set up a test like the example here:

      It would be very easy to run it through Jenkins.

      I’ll make a note and will try to figure it out for myself to see whether it works. If it does, I’ll write about it, so please keep posted.


  3. Hi Bas,

    I gone through your post, it is really very impressive. Could you please let me know how selenium webdriver, jenkins, ant can be used for regression testing.

    • Hi Badri,

      thank you for your kind words. I am afraid I don’t really know what you’re asking for, though. You could use the setup I wrote about for about any kind of functional test, including regression tests..

  4. Hey,

    While following your steps, I was able to get almost all of it but my selenium tests are not running at the end. But the build is successfull. Please suggest what mistake am I doing

    Below is the output

    Buildfile: C:\Workspace\Scripts\TestNGOne\build.xml
    [echo] TestNGOne: C:\Workspace\Scripts\TestNGOne\build.xml
    [delete] Deleting directory C:\Workspace\Scripts\TestNGOne\bin
    [mkdir] Created dir: C:\Workspace\Scripts\TestNGOne\bin
    [copy] Copying 1 file to C:\Workspace\Scripts\TestNGOne\bin
    [echo] TestNGOne: C:\Workspace\Scripts\TestNGOne\build.xml
    [javac] Compiling 2 source files to C:\Workspace\Scripts\TestNGOne\bin
    [junit] Running DoSearchTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit] Test DoSearchTestCase FAILED
    Total time: 1 second

    • Hi Prashant,

      a few quick questions that pop into my head:

      1) Does your test run successfully when you run it ‘by hand’, i.e., from your IDE?
      2) What browser do you use and does it open correctly when you run your tests through Jenkins?

      • Hi Bas,

        Even I’m facing the same issue.

        1.I’m running the selenium IDE test cases through Ant Script.My tests are running successfully through cmd.

        While running through jenkins I’m getting the following error in jenkins:

        Building in workspace C:\Program Files\Jenkins\jobs\Test Automation\workspace
        [Test Scripts] $ cmd.exe /C ‘”C:\ANT\apache-ant-1.9.4\bin\ant.bat -file build.xml run && exit %%ERRORLEVEL%%”‘
        Buildfile: D:\Test Scripts\build.xml

        [unzip] Expanding: D:\ into D:\Test Scripts
        [javac] Compiling 4 source files to D:\Test Scripts
        [javac] warning: Supported source version ‘RELEASE_6’ from annotation processor ‘’ less than -source ‘1.8’
        [javac] 1 warning

        [junit] Running com.example.tests.Abc
        [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 37.781 sec
        [junit] Test com.example.tests.Abc FAILED

        Total time: 39 seconds
        Finished: SUCCESS

        This is the result.xml :
        org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"username"}
        Command duration or timeout: 30.12 seconds
        For documentation on this error, please visit:
        Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
        System info: host: 'HYDHTC341897D', ip: '', 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_31'
        Driver info: org.openqa.selenium.firefox.FirefoxDriver
        Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=35.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
        Session ID: 26aa2166-d372-4555-ba16-e8edba719c02
        at java.lang.reflect.Constructor.newInstance(
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(
        at org.openqa.selenium.remote.RemoteWebDriver.execute(
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(
        at org.openqa.selenium.remote.RemoteWebDriver.findElementById(
        at org.openqa.selenium.By$ById.findElement(
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(
        at com.example.tests.Abc.testAbc(Unknown Source)
        Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"username"}
        For documentation on this error, please visit:
        Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
        System info: host: 'HYDHTC341897D', ip: '', 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_31'
        Driver info: driver.version: unknown
        at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/windows/TEMP/anonymous3947357284673730329webdriver-profile/extensions/
        at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/windows/TEMP/anonymous3947357284673730329webdriver-profile/extensions/

        Do I need to install any selenium Plugin for it???? Please help me ASAP.

        • I think that error might be caused by Jenkins not being able to open a browser, I think I have struggled with a similar issue before. Do you run Jenkins as a service on your machine? It has something to do with that, I only got it working by starting Jenkins through the .bat file when I tried to do so. There should be another solution though..

    • Hi Srinu,

      unfortunately I’m not sure what you mean by your question. Do you want to run your Selenium test cases using an Ant build.xml file? And do you want to run all of them or just a selection of your test cases?

  5. Hi Bas,

    I went through your blog, its really helpful. I had some queries:

    I m using ANT with TestNG for selenium webdriver test cases. When i run the Test suite by command prompt the full suite executes without any failures.

    But, the same suite run through Jenkins i get many failures. I do pull code from SVN and run.

    Do you have any answer for this.

    Thanks in advance!!

    • Hi Yesh,

      is there any way for you to verify that the pulling of source code and the building / compiling steps are completed successfully?

    • Not off the top of my head.. But you can specify a relative path and use Path.getAbsolutePath() (or something very similar) to convert relative paths to their absolute equivalent. Is that helpful enough or would you like a code sample? Not sure when I’ll have the time to work on that..

Leave a Reply

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