Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. Here’s a detailed guide on how to install and set up TestNG in Selenium with Java, including examples.

1. Installing TestNG in Eclipse

Step 1: Install TestNG Plugin

  • Open Eclipse: Launch your Eclipse IDE.
  • Navigate to Eclipse Marketplace: Go to ‘Help -> Eclipse Marketplace‘.
  • Search for TestNG: In the “Find” textbox, type ‘TestNG‘ and click on the ‘Go‘ button.
  • Install TestNG: From the search results, click ‘Install‘ next to ‘TestNG for Eclipse‘.
  • Follow Installation Steps: Follow the on-screen instructions to complete the installation. Eclipse will prompt you to restart the IDE once the installation is complete.

Step 2: Verify Installation

  • Check TestNG: After restarting, go to ‘Window‘ -> ‘Show View‘ -> ‘Other‘ and look for ‘TestNG‘. If it’s there, it means the plugin is installed successfully.

2. Creating a TestNG Project in Eclipse

Step 1: Create a New Java Project

  • Create New Project: Go to ‘File‘ -> ‘New‘ -> ‘Java Project‘.
  • Project Name: Give your project a name, e.g., ‘TestNGSeleniumProject‘.
  • Finish: Click ‘Finish

Step 2: Add TestNG Library to the Project

  • Right-click on Project: Right-click on your project and go to ‘Build Path‘ -> ‘Add Libraries‘.
  • Select TestNG: Select ‘TestNG‘ and click 'Next‘ -> ‘Finish‘.

3. Adding Selenium WebDriver to the Project

Step 1: Download Selenium WebDriver

  1. Download Selenium: Go to the Selenium official website and download the Selenium WebDriver JAR files.
  2. Extract the ZIP: Extract the ZIP file to a convenient location.

Step 2: Add Selenium JAR Files to the Project

  1. Right-click on Project: Right-click on your project and go to ‘Build Path‘ -> ‘Configure Build Path‘.
  2. Add External JARs: Click on ‘Add External JARs‘ and select the Selenium JAR files you extracted.
  3. Apply and Close: Click ‘Apply and Close‘.

4. Writing Test Scripts Using TestNG and Selenium

Step 1: Create a Package

  • Create New Package: Right-click on src -> New -> Package.
  • Package Name: Name your package, e.g., com.test.

Step 2: Create a Test Class

  • Create New Class: Right-click on your package -> ‘New‘ -> ‘Class‘.
  • Class Name: Name your class, e.g., ‘GoogleSearchTest‘.
  • Finish: Click ‘Finish‘.

Step 3: Write a TestNG Test Case

Here’s an example of a TestNG test case using Selenium WebDriver:

				
					package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GoogleSearchTest {
    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set the path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void searchTest() {
        // Open Google
        driver.get("https://www.google.com");

        // Find the search box and enter text
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("TestNG in Selenium");

        // Submit the search
        searchBox.submit();

        // Verify the title
        String expectedTitle = "TestNG in Selenium - Google Search";
        String actualTitle = driver.getTitle();
        assert actualTitle.equals(expectedTitle) : "Title does not match";
    }

    @AfterMethod
    public void tearDown() {
        // Close the browser
        driver.quit();
    }
}

				
			

5. Running the Test

Step 1: Create TestNG XML File

  1. Create New File: Right-click on your project -> ‘New‘ -> ‘File‘.
  2. File Name: Name your file, e.g., ‘testng.xml‘.
  3. Content: Add the following content to the ‘testng.xml‘ file:
				
					<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="com.test.GoogleSearchTest"/>
        </classes>
    </test>
</suite>

				
			

Step 2: Run the TestNG Suite

  1. Right-click on testng.xml: Right-click on ‘testng.xml‘ -> ‘Run As‘ -> ‘TestNG Suite‘.

You’ve now installed and set up TestNG in Selenium with Java, and created and executed a simple test case. This setup is the foundation for more complex test automation projects using Selenium and TestNG.

Scroll to Top