Automating Selenium testing with TestNG, Ant and CruiseControl

If you are using Selenium for web testing, most likely you’ll want to make your tests as automated as possible so that they can be run automatically on a regular basis. You can do this by using a few additional technologies:

1. Integrate with a Java test framework

For any serious testing, your tests will need to be written in Java, rather than the basic Selenium html format. You can save the tests in Java from the Selenium IDE by going to Options -> Format. You have a choice of either TestNG or JUnit format. Personally I prefer TestNG as it is a more powerful framework. I’m not going to give a tutorial on how to using TestNG as there is plenty of information on the web for this, but the key features are:

  • Easy to define what tests you want to run and their parameters by using a testng.xml file.
  • Parameters can be automatically passed to tests using the @Parameters annotation.
  • For data driven tests, data provider methods can be used to invoke the tests multiple times with different sets of input and expected output.
  • Assertion mechanism for the checks in your tests.
  • HTML reporting.

2. Write an Ant script to compile and run your tests

The main things your Ant script needs to do:

  • Compile your tests.
  • Start the Selenium RC server (assuming you are using Selenium 1).
  • Run the tests.
  • Stop the Selenium server.

Here is a sample of what your script should look like:


3. Check your tests into a source control system

Although you can get away without using source control, for any serious testing it's a must. It will make it easier to keep track of changes to the tests and maintain multiple versions of them if you need to. The most commonly used free source control systems are Subversion and Git, although my example below uses Perforce.

4. Set up CruiseControl to run the tests

Although I'd recommend using the CruiseControl Java GUI (CC-Config) for configuring CruiseControl, under the covers it is just making changes to the config.xml file, and to explain the settings it is easiest just to give an example of that file:

I'll explain the various parts of the configuration:

  • project - requiremodification is set to false. If set to true, CruiseControl will only run the build if changes have been made to the code. For building a product, this makes sense, but not for running tests. The product you are testing could have changed, so you still want to rerun the tests even if they haven't changed themselves.
  • modificationset - tells CruiseControl how to check if any of your tests have been updated in your source control system. This example shows the syntax for connecting to Perforce.
  • schedule - tells CruiseControl how to run your build, and how often. The time interval is measured in seconds, so an interval of 86400 seconds means the tests will be run once a day.
  • listeners - defines how to see the current progress of the build. I've just set this to the default log location.
  • bootstrappers - bootstrapping is what occurs before every build. In this configuration, I've set the bootstrapper to get all of the test files from source control. For large projects, this wouldn't make sense, as the bootstrapper will get all of the files regardless of whether any of them have actually been updated. In that situation, you would usually write your Ant script so that it contains targets that can extract your files from source control. The bootstrapper would just get the Ant script, and only if a modification was detected in the files would a build run and the modified files be extracted from source control. However, for a small or moderately sized set of test files, the current configuration is fine.

Useful links

Apache Ant manual:
CruiseControl config.xml:
My blog post on Selenium and XPath: Tutorial: Writing XPath selectors for Selenium tests

This entry was posted in Ant, Selenium, Testing and tagged , . Bookmark the permalink.

2 Responses to Automating Selenium testing with TestNG, Ant and CruiseControl

Leave a Reply

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

HTML tags are not allowed.

516,722 Spambots Blocked by Simple Comments