Ant Conditionals

I’m finally getting around to integrating Ant and my Selenium scripts…

Right now I have a suite of tests to run in Selenium – but before running each test I’d like to revert the database back to a know state. I have an Ant buildfile that does this but it also includes a target that require user input. So I dug around trying to figure out how I could reuse this buildfile without duplicating any code.

Turns out this is quite easy in Ant.

I have my build-sql.xml:


<!-- define oracle login - using info provided above -->

Upon execution this asks me which server to I want to deploy to and also prompts me for my SQL password. Then it uses that information to build a new property ‘script.login’ which we then use later to kick off our SQL scripts.

Turns out it is very easy to bypass this by using the UNLESS attribute within the target:


By simply adding unless=”script.login” – Ant will now check to see if a property called ‘script.login’ has already been defined. If it has, it will bypass running this target.

To reverse this logic and only run this task if the property has been defined use IF:


Now in my test buildfile I simply define ‘script.login’ (set in testing.properties) before running my build-sql.xml buildfile.


	<!-- include external properies - common values for build file -->

This example uses simple conditionals as attributes of the target. There is also a dedicated condition task for even more control.

Recent Related Posts

3 thoughts on “Ant Conditionals

  1. Boyan – Most of my Selenium scripts are generated with the Selenium IDE – an easy to use Firefox extension. Simply run Selenium IDE, fill out your form or whatever and Selenium will record your actions which you can then ‘replay’ back later.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>