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:

CODE:
  1. <target name="prepare" depends="init" description="Setup properties">
  2. <input message="Where do you want to deploy to?"
  3.         validargs="ESDEV,ESTST,ESPRD"
  4.         addproperty="script.schema"
  5.         defaultvalue="ESDEV"/>
  6. <query name="sql.password" message="Enter the schema password" password="true"/>
  7. <!-- define oracle login - using info provided above -->
  8. <property name="script.login" value="${sql.username}/${sql.password}@${script.schema}.WORLD" />
  9. </target>

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:

CODE:
  1. <target name="prepare" unless="script.login" depends="init" description="Setup properties">

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:

CODE:
  1. <target name="prepare" if="script.login" depends="init" description="Setup properties">

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

CODE:
  1. <target name="prepare" description="Prepare environment for build">
  2.     <!-- include external properies - common values for build file -->
  3.     <property file="common.properties"/>
  4.     <property file="auth.properties"/>   
  5.     <property file="testing.properties"/>
  6.     <property file="${user.name}.properties"/>
  7. </target>
  8.  
  9. <target name="buildSQL" depends="prepare" description="Clean db">
  10.       <ant antfile="build-sql.xml"/>
  11. </target>

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

3 Comments

  1. Posted April 10, 2008 at 8:56 am | Permalink

    I want to see some of those Selenium scripts. You have any posts about that?

  2. Posted April 10, 2008 at 9:04 am | Permalink

    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.

  3. Posted April 10, 2008 at 9:05 am | Permalink

    Doh! Thanks, I will give that a shot.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Comments for this post will be closed on 6 August 2008.

Creative Commons License

Creative Commons License This article: Ant Conditionals, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .

Copyright © 2007 thecrumb.com. All rights reserved.