Friday, March 16, 2012

Ant task for compiling classes

The following build.xml file contains 3 targets for deleting unnecessary directories, creating required directories and compiling classes in a specified directory.

//build.xml
  1. <project name="SampleBuildFile" default="compile-classes" basedir=".">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="build.loc" value="E:/temp/build" />
  5.     <property name="classes.dir" value="${build.loc}/classes" />
  6.     <!-- Setting ClassPath -->
  7.     <path id="classpath">
  8.         <pathelement location="${classes.dir}"/>
  9.         <fileset dir="${lib.dir}" includes="*.jar"/>
  10.     </path>
  11.     <!-- Delete directories that are not needed -->
  12.     <target name="delete-dir" >
  13.             <delete dir="${build.loc}"/>
  14.             <echo> /* Deleted existing Directories */ </echo>
  15.     </target>
  16.     <!-- Create Directories -->
  17.     <target name="create-dir" depends="delete-dir">
  18.         <mkdir dir="${classes.dir}"/>
  19.         <echo> /* Created Directories */ </echo>
  20.     </target>
  21.     <target name="compile-classes" depends="create-dir">
  22.         <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  23.             <classpath refid="classpath"/>
  24.         </javac>
  25.         <echo> /* Compiled Classes */ </echo>
  26.     </target>
  27. </project>
In the above build file,

              Property task is used to set a property by name and value which are case sensitive.The attribute 'name' represents name of property and 'value' represents value to the property. By using this we can have an advantage when we want any modification further on property values because value represented by property name is used through out the build file so no need to change the property value for all.

Path task is used to building class path required to execute different ant targets in the build file.

             Here by default the 'compile-classes' target 'll call first when the build file 'll run, but this target depends on 'create-dir' target so it 'll call 'create-dir' target before it run.Again the 'create-dir' depends upon 'delete-dir' so it 'll call the 'delete-dir' target before 'create-dir' run.Clearly, the order of execution 'll be 
                       delete-dir   =>  create-dir   => compile-classes

             In the 3rd target the 'javac' task 'll compile all the java classes that are in the directory specified by 'srcdir' attribute i.e 'src' directory and all the compiles classes 'll be stored in the directory specified by 'destdir' attribute i.e 'E:/temp/build/classes'

The <classpath> is used to specify the PATH references using path tasks 'id' attribute value.

Ant script for create or delete directory

Ant Task for Deleting directories:
Below is an ant task that deletes the directory specified to 'dir' attribute of delete tag.
  1. <target name="delete-dir" >
  2.     <delete dir="build}"/>
  3.     <echo> /* Deleted existing directory */ </echo>
  4. </target>

The delete task deletes 'build' directory including all the files and subdirectories of 'build'.
The echo task just prints the message in console.

  1. <target name="delete-dir" >
  2.     <delete>
  3.         <fileset dir="." includes="**/*.java"/>
  4.     </delete>
  5. </target>

Here the delete task deletes all files with the extension '.java' from the current directory(represented by '.') and any subdirectories of it.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build"/>
  4.     </delete>
  5. </target>
Here the deletetask deletes all files and subdirectories of 'build', including build itself.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build" includes="**/*"/>
  4.     </delete>
  5. </target>
Here the delete task deletes all files and subdirectories of build, without build itself.

Ant Task for Making Directory:
Below is an ant task for making required directories specified to 'dir' attribute of 'mkdir' task.
  1. <target name="create-dir" >
  2.     <mkdir dir="build"/>
  3.     <echo> /* Created Directory */ </echo>
  4. </target>

The mkdir task creates a directory 'build' in to the root directory.
Note: mkdir creates the directory if not exists only, if exists it does nothing.

Thursday, March 15, 2012

Getting Started with Apache Ant

Prerequisite: 
  1. Java environment installed (Make sure that environmental variables JAVA_HOME is set to the directory where your JDK is installed and JAVA_HOME  is added to PATH)
Installation:
  1. Download the latest stable version of Ant from the Ant web page http://ant.apache.org/
  2. Unzip the downloaded file into a directory in your local drive.
  3. Set environmental variables JAVA_HOME to your Java environment i.e to the directory where your JDK is installed.
  4. Set the ANT_HOME environment variable to the directory you unzipped Ant to or where you installed Ant.
  5. Add %ANT_HOME%/bin (for Windows) to your PATH
Creating build file:
It is an xml file generally referred by build.xml 
  1. Create a project(create package and test classes to be execute) or open existing project.
  2. Create a file namely 'build.xml' parallel to 'src' directory.
Apache Ant's build file structure:
  1. Build file contains one project and at least one (default) target.
  2. The main components of ant are property, path, target, task etc.
A sample build file is shown below:

//build.xml
  1. <project name="GoogleSearch" default="compile" basedir=".">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="build.loc" value="${build}" />
  5.     <property name="classes.dir" value="${build.loc}/classes" />
  6.    
  7.     <path id="classpath">
  8.         <pathelement location="${classes.dir}"/>
  9.         <fileset dir="${lib.dir}" includes="*.jar"/>
  10.     </path>
  11.        
  12.     <target name="compile">
  13.         <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  14.             <classpath refid="classpath"/>
  15.         </javac>
  16.         <echo> /* Compiled Directory Classes */ </echo>
  17.     </target>
  18. </project>

Run Build file:
We can run an ant file from
  1. IDE(in my case eclipse) by right click on build.xml -> Run as -> Ant Build
  2. Command line by ant <target>  
In first case the default target assigned to ant's project will run and in second case if you 'll run simply ant it 'll run the default target assigned or ant <target> 'll run the specific target you have given in command line.
        In the above sample build.xml, when it 'll run, it 'll execute the 'compile' target of build file which 'll compile all the classes under 'src' directory of the project.

Selenium RC with TestNG

Here is an example explaining how to use TestNG with Selenium RC i.e using TestNG  to run selenium rc tests.

//GoogleTestTestNG.java
  1. package test.example;
  2. import org.testng.annotations.AfterClass;
  3. import org.testng.annotations.BeforeClass;
  4. import org.testng.annotations.Test;
  5. import com.thoughtworks.selenium.DefaultSelenium;
  6. import com.thoughtworks.selenium.Selenium;
  7. /**
  8.  * Search Google example.
  9.  *
  10.  * @author aksahu
  11.  */
  12. public class GoogleTestTestNg {
  13.    
  14.     Selenium selenium;
  15.    
  16.     @BeforeClass
  17.     public void startSelenium(){
  18.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  19.         selenium.start();
  20.         selenium.windowMaximize();
  21.     }
  22.    
  23.     @Test
  24.     public void testGoogleSearch(){    
  25.         selenium.open("/");
  26.         selenium.type("id=lst-ib""selenium");
  27.         selenium.click("name=btnK");
  28.         selenium.click("link=Selenium - Web Browser Automation");
  29.         selenium.waitForPageToLoad("30000");
  30.     }
  31.    
  32.     @AfterClass
  33.     public void stopSelenium(){    
  34.         selenium.stop();
  35.     }
  36. }

          This test launches firefox browser and executes the google search test.The selenium session is started in startSelenium() method which is called before any test run in the test class and ends with stopSelenium() method which 'll call after the tests run.