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:
-
<target name="prepare" depends="init" description="Setup properties">
-
<input message="Where do you want to deploy to?"
-
validargs="ESDEV,ESTST,ESPRD"
-
addproperty="script.schema"
-
defaultvalue="ESDEV"/>
-
<query name="sql.password" message="Enter the schema password" password="true"/>
-
<!-- define oracle login - using info provided above -->
-
<property name="script.login" value="${sql.username}/${sql.password}@${script.schema}.WORLD" />
-
</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:
-
<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:
-
<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.
-
<target name="prepare" description="Prepare environment for build">
-
<!-- include external properies - common values for build file -->
-
<property file="common.properties"/>
-
<property file="auth.properties"/>
-
<property file="testing.properties"/>
-
<property file="${user.name}.properties"/>
-
</target>
-
-
<target name="buildSQL" depends="prepare" description="Clean db">
-
<ant antfile="build-sql.xml"/>
-
</target>
This example uses simple conditionals as attributes of the target. There is also a dedicated condition task for even more control.
You May Also Enjoy Reading:
3 Comments
I want to see some of those Selenium scripts. You have any posts about that?
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.
Doh! Thanks, I will give that a shot.
Creative Commons License