Create TestNG Test Case Using Selenium Webdriver

In our previous blog posts on the TestNG framework, we’d covered the installation of TestNG in Eclipse and discussed the various TestNG annotations available for the Selenium Webdriver. If you are planning to learn Selenium Webdriver using TestNG, then it would be worth reading these posts. In case you already know TestNG then you would be aware that it works in a proper sequence with entities like the test cases, test suites and test groups, etc. In today’s post, we’ll show how to create TestNG test case using Selenium Webdriver.

A TestNG test case is an executable item which you can prioritize or run in groups. So Let’s proceed to understand the necessary steps required to write a TestNG test case.

In this Selenium Webdriver tutorial, we’ll create a demo TestNG project in Eclipse and add the Selenium Webdriver standalone jar to demonstrate the TestNG test case execution. You must note that every TestNG project can have an associated <TestNG.XML> file which outlines the structure of the test cases. It allows you to group multiple test cases into a single test suite. For your reference, a simple version of the <TestNG.XML> is as follows.

We mentioned about the <TestNG.XML> just because you might need to modify this file to enable/disable test cases. Also, it is one of the typical questions asked during test automation interviews.

Next important thing you might be wondering is the scenario we are going to automate while writing the TestNG case. So here are the details of the use case we’ll cover in this demo project.

  • Create a TestNG test case class and instantiate the FireFox Webdriver Object.
  • Start the Firefox web browser and navigate to the URL (
  • Fill the user name field with “test”.
  • Fill the password field with “test”.
  • Click on the sign-in link for login.

Note: We’ve tested the code using Firefox version 45.0.1.

Create TestNG Test Case Using Selenium Webdriver.

Let’s follow each of the steps one by one. We assume that you have a working installation of the Eclipse IDE and the TestNG plugin. If any of these is missing, then you might consider reading these posts.

1- Create a new Java project in Eclipse to write your TestNG test case.

You need to start the Eclipse IDE and choose to create a new Java project to write the first TestNG test case. Press next and take clues from the attached screen shot.

Create Eclipse Java Project to Write a TestNG Test Case

Create Eclipse Java Project to Write a TestNG Test Case


Name the new TestNG project as the <TestNGSeleniumDemo> and press the Finish button for a successful project creation.

New Java Project to Write a TestNG Test Case

New Java Project to Write a TestNG Test Case


2- Add the <TestNG> library to your Java project.

Here are the steps to introduce the <TestNG> library.

Right-click on the project icon to open the context menu and click the “Build Path >> Add Library” option.

It’ll open up a dialog listing all the installed libraries. Choose the <TestNG> option to add it to your project.

Add TestNG Library to Write a TestNG Test Case

Add TestNG Library to Write a TestNG Test Case


3- Create a new package in your project.

Right-click on the project icon and select the “New” option to create a package as <com.techbeamers.testng>.

4- Adding a TestNG class.

Right-click on the newly created package and select New >> Other.

Adding TestNG Test Case Class

Adding TestNG Test Case Class

You’ll now see a TestNG class dialog like the one shown below.

  • Here you must give a name to the new TestNG class as <SeleniumWebdriverTest>.
  • Then, select the <BeforeMethod> and <AfterMethod> checkboxes available on the dialog box.
  • Finally, press the Finish button to create the class.
Set TestNG Annotations for the TestNG Test Class

Set TestNG Annotations for the TestNG Test Class

After this, the Eclipse will auto generate the TestNG class code. But there’ll be errors in the code because of missing package imports. We are giving you a solution to this issue. You must remember it as it is one of the crucial steps which as you’ll need later as well.

Q: How will you fix the import errors for the missing packages?

It’s very easy to resolve such compile time issues.

  • Make sure you’ve added the related library into your projects like the TestNG library or the Selenium Webdriver standalone Jar.
  • You don’t need to remember the names of the required packages. Just press the CTRL+Shift+O keyboard shortcut. And it will automatically add the required imports to your code.

5- Adding Selenium Webdriver’s Standalone Jar to your project.

You should be careful while adding the Selenium Webdriver jar file because the file must be compatible with the version of the browser (Firefox) you’ll use for testing. For this demo project, we’ve downloaded the latest version of the jar (2.53.0) file from the Selenium website. And we’d the most recent version of the browser (i.e. Firefox 45) which works well with the downloaded jar file.

Here, you may ask why are we using the standalone API of the Selenium Webdriver?

The reason is straightforward and clearly explained below.

  • Using it eliminates the need of adding any other dependent library.
  • It directly works with the Firefox browser.
  • To run your tests with browsers like Chrome, you need to download/add their respective driver’s jar files.

6- Write TestNG test case script using Selenium Webdriver API.

We’ve given the full source code of the TestNg test case script. You can directly copy it to your project’s <> file. You can change the name of this file, but then you would also need to make the same modifications in the below source code. Here we are automating the test case scenario mentioned earlier, click the <test case description> link to check it.


7- Run the TestNG Test Case and View Test Execution Report.

You can easily run any of the TestNG test cases. Just right-click on the TestNG project icon, click the “Run as >> TestNG Test” option to execute the script. Check out the below snapshot for more info.

How to Run the TestNG Test Case

How to Run the TestNG Test Case


When you run the test case, it creates a <test-output> folder which contains a folder named as the <Default suite>. Here the TestNG project report is available in the <Default test.html> file. You can load the report in Eclipse by opening it in the web browser mode. For more clarity on viewing the test execution report, refer the attached picture.

TestNG Test Case Using Selenium Webdriver

TestNG Test Case Using Selenium Webdriver

Final Word.

We tried to reduce the complexity of the steps required to create a TestNG test case using Selenium Webdriver API. Additionally, we mentioned little tricks that can help you write effective test cases and resolve errors.

You can surely build on the concept share in this post and use it to create much larger Selenium Webdriver projects. If you like the content, then please share it further.

All the Best,



  1. Krishna Reply
    • Meenakshi Agarwal Reply
  2. Vivek Reply
    • Meenakshi Agarwal Reply