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

Running tests in TestNG with Selenium Java involves several steps, including setting up the environment, creating test scripts, and organizing them using TestNG. TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but introduces some new functionalities that make it more powerful and easier to use, especially for integration testing.

Step 1: Set Up the Environment

  1. Install Java Development Kit (JDK)

  2. Set Up Eclipse or IntelliJ IDEA

  3. Add Selenium and TestNG Dependencies

If you’re using Maven, add the following dependencies to your ‘pom.xml‘ file:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.x.x</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.x.x</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

Step 2: Create a Simple Selenium Test with TestNG

  1. Create a New Java Project

    • Open your IDE and create a new Java project.
  2. Create a Test Class

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GoogleSearchTest {
    WebDriver driver;

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

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

        // Find the search box
        WebElement searchBox = driver.findElement(By.name("q"));

        // Enter a search query
        searchBox.sendKeys("Selenium WebDriver");

        // Submit the search
        searchBox.submit();

        // Wait for the page to load and verify the title
        try {
            Thread.sleep(2000); // This is just for demo purposes, use WebDriverWait in real tests
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Assert the title contains the search query
        Assert.assertTrue(driver.getTitle().contains("Selenium WebDriver"));
    }

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

				
			

Step 3: Create a TestNG XML File

  1. Create a TestNG XML file to define test suites and test cases:

				
					<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="GoogleSearchTest">
        <classes>
            <class name="GoogleSearchTest"/>
        </classes>
    </test>
</suite>

				
			

Step 4: Run the Test

1. Run the TestNG Suite

  • Right-click on the ‘testng.xml‘ file and select “Run As” > “TestNG Suite”.

Step 5: Advanced Example with Page Object Model (POM)

Using the Page Object Model, we create separate classes for each page in our application to encapsulate the page elements and actions.

1. Create Page Object Class

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

public class GoogleHomePage {
    WebDriver driver;

    By searchBox = By.name("q");

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

    public void enterSearchQuery(String query) {
        WebElement searchInputElement = driver.findElement(searchBox);
        searchInputElement.sendKeys(query);
    }

    public void submitSearch() {
        WebElement searchInputElement = driver.findElement(searchBox);
        searchInputElement.submit();
    }
}

				
			

2. Create the Test Class Using Page Object

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GoogleSearchWithPOMTest {
    WebDriver driver;
    GoogleHomePage googleHomePage;

    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        googleHomePage = new GoogleHomePage(driver);
    }

    @Test
    public void testGoogleSearch() {
        driver.get("https://www.google.com");
        googleHomePage.enterSearchQuery("Selenium WebDriver");
        googleHomePage.submitSearch();

        try {
            Thread.sleep(2000); // This is just for demo purposes, use WebDriverWait in real tests
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Assert.assertTrue(driver.getTitle().contains("Selenium WebDriver"));
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}

				
			

3. Update the TestNG XML File

				
					<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="GoogleSearchWithPOMTest">
        <classes>
            <class name="GoogleSearchWithPOMTest"/>
        </classes>
    </test>
</suite>

				
			

Step 6: Running the POM-based Test

  • Follow the same procedure to run the ‘testng.xml‘ file as a TestNG Suite.

This approach of using TestNG with Selenium Java and organizing tests using Page Object Model ensures better code maintenance, readability, and reusability.

Scroll to Top