Selenium Test Cases in Java with TestNG

TestNG is next generation testing framework like JUnit & NUnit but with extra features(Read keywords, Generate Test Reports, Generate logs) and easy to use. TestNG framework can cover different type of the tests like Integration, Unit, end-to-end, etc.

  Installing TestNG in Eclispe:

  • Open Eclipse , Select Help / Software updates / Find and Install.
  • Search for new features to install.
  • New remote site.
  • For Eclipse 3.4 and above, enter
  • For Eclipse 3.3 and below, enter
  • Make sure the check box next to URL is checked and click Next.
  • Eclipse will then guide you through the process.

We are going to Test a Demo website “” and will write a Test case
to Do a Registration in it.

Project Structure:

Create a project in the eclispe. Add the Selenium StandAlone jar and TestNG jar into a new libs  folder.
Make sure you add them to Build Path by Right Click – Build Path –> Add to Build Path.

Create a Base Class with Test Fixture methods e.g. Setup and Tear Down. Note: I have added function name
as JUnit you can name them as you want), Add annotations as Before Method and After Method.

package com.scrolltest.testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseClass {

WebDriver driver;

public void setUp() {
driver = new FirefoxDriver();


public void tearDown() {
// driver.close();


Step3 :
Create a Separate Class with Name Registration .java to write a Test case for Registration of the
Demo: Website

package com.scrolltest.testng;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Reporter;

public class RegisterationPageObect {

private WebDriver driver;

public RegisterationPageObect(WebDriver driver) {
this.driver = driver;

public void RegisterUser() {
WebElement t = driver.findElement("userName"));

// #Drop Down
new Select(driver.findElement("country")))
// register


Step4: Now Lets create a Final class and TestNG xml file.

package com.scrolltest.testng;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class MainClass extends BaseClass {

public void testRegisterationInDemoAUT() throws InterruptedException {

RegistrationPageObect pageOject = new RegistrationPageObect(driver);

assert driver
"Thank you for registering. You may now sign-in using the user name and password you’ve just entered.");

assert driver.findElement(By.tagName("body")).getText()
.contains("Dear Pramod Dutta,");



Now Run the Test by Creating new TestNG configuration by Clicking on the Project->Run Configuration, Select
the TestSuite and browse the XML file(testng.xml). and Now Run the Project.

After run successfully, If you refresh you project you will see the test-output folder , Open “index.html” and see that You Test Suite Results. Click on the Report output link you can see max length also.(Used as Logging)

Source code Github


Leave a Reply

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