TestNG and ANT Configuration


Execute TestNG Tests from ANT build.xml
Note: No need to install/configure ANT in your computer if already exists(step 1- 10)
1) First please download the latest version of ANT from below url
http://ant.apache.org/
Note: .zip format is recommended format for Windows Operating systems
2) Unzip downloaded ANT and Store it in preferably drive
Ex: C:\ANT\
3) Now set the class path in Environment variable
4) From the desktop, right click the 'Computer' icon / Right click on my computer and Choose 'Properties' from the context menu. And Click the 'Advanced system settings' link.
5) Click on Environment variable
clip_image002
6) In the section 'System Variables', Add path for ANT. Click on New button
clip_image004
7) Enter the Variable Name as ANT_HOME and variable value as path of the ant(where ANT was installed)
clip_image005
8) Select path variable in 'System Variables' Section, Add path for ANT. Click on 'Edit' button to add
clip_image007
9) Click on OK button and close all remaining windows.
10) Go to the command prompt and check if the installation is done successfully or not
clip_image009
Note if you get ant installed version number means ANT installed successfully.

In Build.xml we will mainly see the below tags
1. project - Project is the staring point of the Ant configuration file and consists of the entire configuration with respect to building a project.Each project defines one or more targets.
2. target - A target is a set of tasks that that you want to execute during the build process.
3. task - Task is a piece of code that can be executed
4. property - Property tags are an important way to customize a build process or to provides way to define shortcuts to reuse repeatedly inside a build file
Now Create a Sample project in Eclipse
Step 1) Create a Project
Step 2) create a package com.ant.exp
Step 3) create class in package antTestNGExp
package com.ant.exp;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
importorg.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
publicclass antTestNGExp {
public WebDriver driver=null;
String appURL = "http://google.com";
@BeforeClass
publicvoid testSetUp() {
driver = new FirefoxDriver();
}
@Test
publicvoid verifyPageTittle() {
driver.navigate().to(appURL);
String getTitle = driver.getTitle();
Assert.assertEquals(getTitle, "Google");
}
@AfterClass
publicvoid tearDown() {
driver.quit();
}
}
To Create a build.xml file
Select Fileà Export

clip_image011
clip_image013
Select AntBuildfiles under General Section
Select the project from list of projects means where you want to create a build.xml file then click on finish
clip_image015
Build file get generated and you should change the build file (Project, target task etc.) according to your requirements
Below is the build.xml file copy the code
<project name="Sample Ant build" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<!-- set global properties for build -->
<property name="basedir" value="." />
<property name="lib" value="${basedir}/lib" />
<property name="src" value="${basedir}/src" />
<property name="bin" value="${basedir}/bin" />
<property name="report-dir" value="${basedir}/Test-Report" />
<property name="testng-report-dir" value="${report-dir}/TestNGreport" />
<!-- ====== Set the classpath ==== -->
<path id="classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
</path>
<!-- Delete directories -->
<target name="delete-dir">
<delete dir="${bin}" />
<delete dir="${report-dir}" />
</target>
<!-- Creating directories -->
<target name="create" depends="delete-dir">
<mkdir dir="${bin}" />
<mkdir dir="${report-dir}" />
</target>
<!-- Compile the java code from ${src} into ${bin} -->
<target name="compile" depends="create">
<javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
<echo> /* Compiled Directory Classes */ </echo>
</target>
<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
<target name="testng-execution" depends="compile">
<mkdir dir="${testng-report-dir}" />
<testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
<xmlfileset dir="${basedir}" includes="testng.xml" />
</testng>
</target>
</project>
clip_image017
Directory structure like this
clip_image018
Before running the build.xml file you should create one lib folder and load all required jar files (Testng, selenium etc...) to this libbecause ant tool take reference of the jars from this lib folder only
clip_image019
Go to Command prompt and navigate to respective folder where your build.xml file is
Then you should enter the below command
clip_image021
Once execution is completed ,Output is looks like this
clip_image023


















































































































Comments