By Anonymous (not verified) , on 21 Apr 2014

This blog will take you on a journey to Selenium world. I will talk about choosing framework for QA test automation using Selenium WebDriver. Further I have given instance of few Selenium test cases written using TestNG and how to integrate them with one of the build tool – Ant.

Many, perhaps most, software applications today are written as web-based applications to be run in an Internet browser. Test automation is nothing but using a software tool to run repeatable tests for such web applications. There are many advantages of test automation. Most are related to the repeatability of the tests and the speed at which the tests can be executed. There are a number of commercial and open source tools available for assisting with the development of test automation. But ‘Selenium’ is possibly the most widely-used open source solution for this.

Know More About Selenium Framework using Java? Talk to our Expert

Selenium's tool suite includes the following:


  1. Selenium IDE
  2. Selenium 1 i.e. Selenium RC or Remote Control selnium testing
  3. Selenium 2 i.e. WebDriver
  4. Selenium Grid



Why choose ‘Selenium Webdriver’ for QA test automation?


 'Selenium RC' and 'Selenium WebDriver' allow you to write automation scripts in programming languages with great ease. WebDriver is the successor to Selenium RC. Selenium RC has been officially deprecated in favor of Selenium WebDriver. Selenium IDE is a Record and Playback tool. Though, it’s very easy to use, but is highly unreliable. It also does not provide programming facility. Although the scripts recorded

 by Selenium IDE can be saved for later use, it is not designed to run your test passes. The test cases will fail due to a small change in UI, or for things like change in name of a UI component. Selenium-Grid allows the Selenium RC solution to scale for large test suites and for test suites that must be run in multiple environments. Hence, Selenium Webdriver is the preferred tool to automate your testcases. 


What is an automation framework? 

A test automation framework is a set of concepts, and practices that provide support for automated software testing. It is a methodology built to successfully carry out test automation. If we do not have any frameworks, then it is difficult to get proper reports, handle checkpoints, or exception handling.


Which Automation framework to choose?

Now either you may choose from any of the frameworks available like JUnit & TestNG or you can design our own framework.

Junit is a unit testing framework or the Java programming language. TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc. 

TestNG is a preferred framework of QA analysts as it allows you to generate test reports in both HTML and XML formats. TestNG enable you to group test cases easily which is not possible in JUnit.  

Usually we want to integrate execution of automated test suites with build process. Ant is a software tool for automating software build processes. Ant can get source code from version control, compile source code, package compiled source code and resources, handle dependencies between its targets, run test cases, generate reports of test execution and much more. It is advisable to call your TestNG tests through Ant script, to integrate the testing with the build process. Using Ant you can also generate better reports than the default reports generated by TestNG.


Let’s get started with TestNG!                                                                                                                                                               

Here is a small tutorial to help QA professionals write Selenium test cases in TestNG, and calling the TestNG suite from selenium testing Ant. You can use any of the programming language supported by Selenium 2. I have used Java in below tutorial.

Before getting started, install these software's

(if you don't have):


  1. JDK
  2. Eclipse
  3. TestNG: Download TestNG from Eclipse's Help menu.
  4. Download Selenium server jar from


Steps to follow:


Step 1: Use Eclipse. Create a new Java project.


Step 2: Add Selenium server jar as external jar file to your project. To do this, right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add external jar'. Select the Selenium server jar file you have previously downloaded.


Step 3: Use TestNG library in your project: right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add Library'. Select TestNG. Click 'Ok'.


Step 4: A TestNG testsuite comprises of number of tests. A TestNG 'test' comprises of one or more classes, and a TestNG class consists of multiple 'Test Methods'.


Add a new Java class 'TestClass1' to the project. Following is a set of Selenium test cases written as TestNG methods. In the code below, I have written a set of test cases to test search functionality of the website 4 test cases are added here for demonstration purposeAdd following code to the Java class.

 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.By;

 import org.openqa.selenium.WebDriver;

 import org.openqa.selenium.firefox.FirefoxDriver;

 import org.testng.Assert;

 import org.testng.annotations.BeforeClass;

 import org.testng.annotations.Test;

 import org.testng.annotations.AfterClass;


 public class TestClass1 {

            protected static WebDriver wd;

            protected static String result;


 public static void setup()  {

            wd = new FirefoxDriver();

            wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);



 void Testcase1() {




            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();


 Assert.assertEquals(result, "115");




 void Testcase2() {




            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "13");



 void Testcase3() {




            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "27");



 void Testcase4() {




            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "10");



           public static void teardown()  {





Step 5: Add following TestNG testsuite to your project as 'TestNG.xml'

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE suite SYSTEM "" >

 <suite name="My sample Suite">

  <test name="Search test">


      <class name="TestClass1" />




Step 6: Execute above TestNG testsuite by right clicking on the TestNG.xml and selecting Run as -> TestNG suite. The testcases are executed and the summary of test execution is shown in Eclipse's console. TestNG automatically generates report for your test execution. To see the report, go to your project directory, and select test-output folder. Open idex.html. As it can be seen in the below report, Testcase1,2 and 3 are passed. Testcase4 is failed.

selenium testing

                                    Default Test execution Report generated by TestNG


Step 7: So TestNG suite was executed successfully! Now we want to use Ant to run the TestNG testsuite, instead of eclipse. We need to specify location of Selenium server jar and TestNG jar to Ant. Let's create a new folder in the project and put the jar files there.

i. Create a folder 'lib' in your Java project.

ii. Copy Selenium server jar to this 'lib' folder.

iii. Copy testng.jar too, in the same folder. In you Eclipse directory, go to plugins directory. There will be a directory like org.testng.eclipse_6.8.6.20130914_0724. Under this directory, in lib folder you will have testng.jar. 


Step 8: Add Ant script with name build.xml to your project as follows. 

 <?xml version="1.0" encoding="UTF-8"?>

 <project name="automation" default="main" basedir=".">

 <!--Define Path-->

 <path id="project-classpath">

    <fileset dir="./lib" includes="*.jar"/>

    <pathelement location="bin"/>


            <target name="main" depends = "init, compile, run">


 <!-- Initialization -->

            <target name="init">

                        <echo>starting init task</echo>

                        <delete dir="bin"/>

                        <mkdir dir="bin"/>



            <target name="compile">

                        <javac srcdir="src" destdir="bin">


                                        <fileset dir="./lib" includes="*.jar"/>

                                        <pathelement location="bin"/>                             




 <!--Define TestNG task-->

 <taskdef name="testng" classname="org.testng.TestNGAntTask">


                        <pathelement location="./lib/testng.jar"/>



 <!--Execute TestNG testsuite -->

            <target name ="run">

               <testng outputdir="./test-output" classpathref="project-classpath"  useDefaultListeners="true">

       <xmlfileset dir="." includes="src/TestNG.xml"/>

               <!-- generate report title -->

                <sysproperty key="org.uncommons.reportng.title" value="Test Automation"/>

                <sysproperty key="org.uncommons.reportng.escape-output" value="false"/>





Step 9: Finally, execute the Ant file. Go to command prompt, go to you project's directory. Type ant. It will start executing the ant script build.xml in your project. When the ant script is executed successfully, view the report generated by TestNG at same location i.e. index.html file in your project's test-output directory.

Happy QA with Seleniumsmiley


This blog is written by Archana Mulay, Senior QA at Faichi Solutions. Faichi holds expertise in QA & provide end to end QE services to several clients. Automation testing is an integral part of Faichi QA to ensure quality in a speedy and reliable way. We have designed our own test automation framework using SELENIUM and TestNG. This framework enables us to consistently deliver quality solutions across all clients. Know more about our QA Services.


With inputs from Selenium official documentation



Get in Touch