Setting up Selenium WebDriver Project using TestNG

TestNG is one of the commonly used unit-testing frameworks and is, therefore, one of the most preferred frameworks for Selenium WebDriver projects.

In this article, we see how TestNG features can be used for Selenium WebDriver projects. We cover parameterization, data provider, test dependency, multiple class features of TestNG. You can get the complete code from my GitHub account.

WebDriver With TestNG


We will be creating test scripts for the Search Page and Product Detail page of Amazon website. Each page will be covered in separate TestNG classes, each covering the various features of TestNG.

First of all, as part of project setup, I have created a simple Maven project and added selenium-java and testng as the dependencies. Check the pom.xml file here. See this post to know more about WebDriver Maven project setup.

SEE ALSO:   Solutions to 5 Common WebDriver Doubts

Parameterize Browser Initialization

Since we may have to run our tests in various browsers, we can pass the browser as a parameter while running the TestNG tests using testng.xml file.

Create WebDriverHelper Class

First of all, we need to create a WebDriverHelper class which handles the driver initialization and other common processes. This class will be used by our testng classes.

As you can see above, we have one method which will initialize and return the WebDriver object based on the value browser given. This browser value will be passed on from the testng.xml file.

TestNG Class and testng.xml file

Now let’s create our TestNG class and the testng.xml file.

Using the @Parameters annotation, we can pass the browser to the WebDriverHelper class for initialization. The value for browser parameter is given in the testng. Please note that the method with @BeforeClass annotation will be executed at the start of every class.

The value for browser parameter is given in the testng.xml file (which is placed in the project root).

As you can see, we have one test, with the browser parameter and one class for execution. You can add as many parameters and as many classes as required.

SEE ALSO:   5 Factors that Build your Confidence in WebDriver

Now let’s see how to write a simple @Test method.

Using Simple TestNG @Test Method

We will write a simple test method to perform a search on the homepage and verify the results page.

Test Dependency

Even though I would never make two tests dependent on each other (see 3 Things to Avoid in Test Automation), there is a feature in TestNG to assign dependency. Let’s see how it works.

We will create a test to verify navigation and make the testSearchOnPage method dependent on it. This is done by adding (dependsOnMethods = { “verifyNavigation” })  to the @Test annotation as shown below.

The first method will be executed (which gets the parameter value from testng.xml file btw) and then the second dependent test case will be run. If the first test fails, then the second is skipped unless (alwaysRun=true) is set.

SEE ALSO:   Selenium WebDriver Cheat Sheet

Passing Parameters Using TestNG DataProvider

Since we may want to run the above search with various search terms, we will use the DataProvider concept in TestNG.

First of all, we define the data provider using the @DataProvider annotation.

Any method which returns an array of Objects can be used as a DataProvider. Therefore, here we can return a set of two values – a search term and a boolean which shows if the search is valid or not.

In the above data provider, all except the last search term displays a proper results page. The last search term shows an error message. We use the boolean value to identify the invalid case and give our logic based on it.

Running Multiple Classes in TestNG

Finally, let’s create another testng class to perform validations on the product detail page.

In addition to that, modify the testng.xml file to include the new class.

Therefore, on executing the testng.xml file, you get a result similar to this.

TestNG Result

Other TestNG Features

In a similar way, other TestNG features like Suite, Ignore, Group can be implemented for Selenium WebDriver projects.


I hope this post was useful to you. You can get the complete code from my GitHub account.

If you enjoyed this article and hope to see more such posts, subscribe to our newsletter or follow us on Facebook or Twitter.

Automation Testing Tutorials, Tips, Solutions & more once a week

You may also like...

Leave a Reply

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