Introduction of TestNG with Selenium

TestNG is an open source testing framework which can be used with selenium to organize selenium scripts.
TestNG is mentioned with Selenium because Selenium is a common testing tool that interacts with browser.
Selenium + TestNG work together well, but they are completely separate.
Selenium connects you to browser, TestNG organizes your tests.


For example :-
Below is the selenium script written in simple JAVA main method :-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestSample {

  public static void main(String s[]) {

    // Initialize Driver. We are taking an example of firefox
    WebDriver driver = new FirefoxDriver();
    String baseUrl = "http://www.gmail.com";
    driver.get("http://www.gmail.com");

    driver.findElement(By.id("username")).sendKeys("contact.automateapps@gmail.com");
    driver.findElement(By.id("passwrd")).sendKeys("password");
    driver.findElement(By.id("Login")).click();

    //Closes the browser
    driver.quit();
  }
}

When you execute above program, it will execute selenium code but it won’t generate any report because code is written in simple main() method.
Let say if you have multiple scripts, then creating scripts in main() method is not the right way. You have to configure TestNG/JUNIT in order to make your scripts organized.
Through TestNG,
a) one can execute multiple scripts at a same time(Parallel execution).
b) TestNG automatically generates HTML reports after scripts execution.
c) One can perform data-driven testing(script execution with multiple sets of test data).


Selenium WebDriver script with TestNG

The above program is divided into 3 functions :-
@BeforeTest – Method having “BeforeTest” annotation will execute before the method having @Test annotation.
All initialization step such as object initialization, setup database connection etc will come under this method.

@Test – It contains your selenium scripts.

@AfterTest – Method having “@AfterTest” annotation will execute after the method having @Test annotation.
Actions that you want to perform after test script execution like close browser,
close database connection etc will come under this method.


import org.openqa.selenium.By;
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 TestSample {

WebDriver driver;
String baseUrl;

  @BeforeTest
  public void setUp() {
    driver = new FirefoxDriver();
    baseUrl = "http://www.gmail.com";
  }

  @Test
  public void testApp() {
    System.out.println("Launch Application");
    driver.get(baseUrl);
    driver.findElement(By.id("username")).sendKeys("contact.automateapps@gmail.com");
    driver.findElement(By.id("passwrd")).sendKeys("password");
    driver.findElement(By.id("Login")).click();
  }

  @AfterTest
  public void close() {
    //closes browser
    driver.quit();
  }

The information provided above is just the overview of TestNg framework. For detailed information of TestNG framework, Click here

Leave a Reply

Your email address will not be published. Required fields are marked *