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

Creating and Running TestNG Tests in Selenium with Java

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, such as annotations, test configurations, and parallel execution.

1. Setup and Installation

To get started, you’ll need the following:

  • Java Development Kit (JDK)
  • Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse
  • Maven (for dependency management)
  • Selenium WebDriver
  • TestNG

Maven Dependencies

Include the following dependencies in your ‘pom.xml‘ file:

				
					<dependencies>
    <!-- Selenium Java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Creating a Basic Selenium Test with TestNG

Project Structure

				
					src
├── main
│   └── java
│       └── your.package
│           └── BaseTest.java
└── test
    └── java
        └── your.package
            └── GoogleSearchTest.java

				
			

BaseTest.java

				
					package your.package;

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

public class BaseTest {
    protected WebDriver driver;

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

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

GoogleSearchTest.java

				
					package your.package;

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

public class GoogleSearchTest extends BaseTest {

    @Test
    public void testGoogleSearch() {
        driver.get("https://www.google.com");
        driver.findElement(By.name("q")).sendKeys("TestNG");
        driver.findElement(By.name("btnK")).submit();

        String title = driver.getTitle();
        Assert.assertTrue(title.contains("TestNG"), "Title does not contain 'TestNG'");
    }
}

				
			

3. Running the Test

You can run the test from your IDE or use Maven from the command line.

Running from IDE

  • Right-click on the test file (GoogleSearchTest.java) and select “Run”.

Running from Command Line

  • Create a ‘testng.xml‘ file in the root of your project:
				
					<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite">
    <test name="GoogleSearchTest">
        <classes>
            <class name="your.package.GoogleSearchTest"/>
        </classes>
    </test>
</suite>

				
			
  • Run the test using Maven:
				
					mvn test

				
			

Using the Page Object Model (POM)

The Page Object Model (POM) is a design pattern that creates an object repository for web UI elements. It helps to make tests more maintainable and reusable.

Page Class

				
					package your.package.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class GoogleHomePage {
    private WebDriver driver;

    private By searchBox = By.name("q");
    private By searchButton = By.name("btnK");

    public GoogleHomePage(WebDriver driver) {
        this.driver = driver;
    }

    public void search(String keyword) {
        driver.findElement(searchBox).sendKeys(keyword);
        driver.findElement(searchButton).submit();
    }
}

				
			

Test Class using POM

				
					package your.package.tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import your.package.BaseTest;
import your.package.pages.GoogleHomePage;

public class GoogleSearchWithPOMTest extends BaseTest {

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

        GoogleHomePage googleHomePage = new GoogleHomePage(driver);
        googleHomePage.search("TestNG");

        Assert.assertTrue(driver.getTitle().contains("TestNG"), "Title does not contain 'TestNG'");
    }
}

				
			

This guide covers the basics of setting up and writing TestNG tests in Selenium with Java. It also introduces advanced TestNG features and the Page Object Model (POM) design pattern for better test maintainability. By following these examples, you can create robust and scalable test automation suites

Scroll to Top