Step-by-Step Guide to Creating a Maven Project with Selenium and TestNG

Step-by-Step Guide to Creating a Maven Project with Selenium and TestNG person Vishnu Hari | Published: Sun Sep 2023

Selenium and TestNG together form a powerful duo, enabling seamless automated testing and robust test management for your projects

Before we start the tutorial, just to clarify, I'm using a Windows 10 computer with OpenJDK, Eclipse IDE and testNG plugin already installed. Once you've got everything ready, you're all prepared to begin crafting your first Maven Project with Selenium and TestNG.

Create a new Maven Project

Maven simplifies project management by automating build processes, dependency management, and project structure, streamlining your development workflow.

1. Create a Maven project by navigating to Eclipse IDE File -> New -> Project -> Maven Project. 

2. Choose 'Create a simple project' and then click the 'Next' button.

3. Name your Group Id and Artifact Id, then click Finish to create your Maven project hassle-free.

Because we're planning to use Selenium and TestNG in this project, I'll be adding the necessary dependencies for Selenium and TestNG to the project's 'pom' file.

<dependencies>
 
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
 
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
 
</dependencies>

To get the dependency, go to the Maven Central Repository, search for 'selenium java,' choose the version you need, and then copy the Maven value provided.

Once you save the 'pom' file, the necessary JAR files will be downloaded automatically and you'll find them in the 'Maven Dependencies' folder within your project's structure.

Create Selenium Test With TestNG

1. Simply right-click within the 'src/main/java' directory, choose 'New,' then 'Package,' and provide a name to create a new package

2. Right-click on the newly created package, select 'New,' then choose 'Class' to create a new class file, and give it a name.

Here's a simple example of a Selenium test using TestNG, where we'll open the Chrome browser and navigate to google.com.

package SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

 

public class browserTest {

    WebDriver driver;

 

    @BeforeMethod

    public void setUp() {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");

        driver = new ChromeDriver();

    }

 

    @Test

    public void testGoogleSearch() {

        driver.get("https://www.google.com");

        // Your test actions here

    }

 

    @AfterMethod

    public void tearDown() {

        if (driver != null) {

            driver.quit();

        }

    }

}

To execute the test, simply right-click on the method where we've added the '@Test' annotation, then choose 'Run As' followed by 'TestNG Test'.