My Thoughts: WebDriver Tutorial Part 4 : Working with TestNG framework in Eclipse

Saturday, August 18

WebDriver Tutorial Part 4 : Working with TestNG framework in Eclipse

Writing a script in plain Java is simple like what we have done in the previous post. But there is more we can do using WebDriver.

Say If you want to run multiple scripts at a time ,better reporting and you want to go for datadriven testing (Running the same script with multiple data) then plain Java script is not enough .

So it is recommended to use any of the existing frameworks like TestNG or JUnit. Select one of the framework and start using it.

In this post , I start with TestNG. If you are planning to use TestNG then you need to
  • 1.Install TestNG Eclipse plug-in 
  • 2.Customize the output directory path
  • 3.Start writing scripts
1.Install TestNG Eclipse plug-in 

Detailed instruction on how to install TestNG plug-in can be found here

2.Customize the output directory path

This is not a mandatory step. But it is good to have it.Using this you can tell TestNG where to store all the output results files. 

In Eclipse Goto Window>>Preferences>>TestNG>>
Set the Output Direcory location as per your wish and click on "Ok".

Vamshi Kurra -TestNG

3.Start writing scripts
Writing scripts using TestNG is so simple. We will start with the editing of script we have written in the previous post .
Here is the edited script using TestNG.
package learning;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GoogleSearchTestNG {

 WebDriver driver;
 public void start(){
  driver = new FirefoxDriver();
 public void Test(){ 
  System.out.println("Loading Google search page");
  System.out.println("Google search page loaded fine");
 public void close(){

If you look at the above code the main chnages we have done is 
1.Imported new TestNG files

2.Now there is no main function.We removed it.

3.We have split the single main function into 3 functions. i.e start(), test(), close()
start()  -- Initialize the WebDriver
Test()  -- Perform our exact requirement.
close() -- Close the Browser once

4.There are 3 different annotations named "BeforeTest" , "Test" , "AfterTest".
@BeforeTest -- Keep this annotaion before a method which has to be called initially when you run the script. In our script ,we put it before start() method because we want to initialize the WebDriver first.
@Test    -- Keep this annotation before a method which will do the exact operation of your script.
@AfterTest -- Keep this annotation before mehod which has to run at the end. In our script , closing browser has to be done at the end. So we put this annotation before close() method.

TestNG is so powerful . But now we stop here :) and experiment more on WebDriver functions. Once that is done we will come back to TestNG.

Can't wait and learn more ?? , you can always refer documents here

Going forward all the example scripts in this site will refer to TestNG.


  1. nice Article.........

  2. This post helped me a lot. Thanks!

  3. Anuradha raghunathTuesday, April 30, 2013

    this is very helpful for beginners to understand

  4. great man ...helped a lot :)