Create TestNG Project In Eclipse & Run Selenium Test Script
Praveen Mishra
Posted On: July 31, 2020
16 Min
TABLE OF CONTENTS
Chapters
- What Is TestNG Framework
- Install TestNG in Eclipse
- Create TestNG Project in Eclipse
- Create TestNG.xml File in Eclipse
- Using TestNG In Selenium
- First TestNG Automation Script
- Exception Tests in TestNG
- Parallel Test Execution in TestNG
- Assertions in TestNG
- Set Test Case Priority in TestNG
- TestNG Reporter Log
- Generating TestNG Reports
- Running JUnit Selenium Tests using TestNG
- Grouping Test Cases in TestNG
- Use DataProviders In TestNG
- Parameterization In TestNG
- TestNG Listeners
- TestNG Annotations
- Parallel Testing In Selenium With TestNG
- Appium With TestNG
Creating a TestNG project in Eclipse and running Selenium test scripts helps you efficiently organize and execute automated web tests, making your testing process faster, structured, and easier to manage. TestNG’s features like annotations, parallel execution, and detailed reports ensure your automation framework is reliable and scalable.
Overview
What Is TestNG?
TestNG is a powerful testing framework for Java that simplifies test execution, grouping, and reporting. It supports annotations, parallel test execution, data-driven testing, and flexible configuration, making it ideal for both unit and integration testing in automation projects.
How to Setup a TestNG Project in Eclipse?
Setting up a TestNG project in Eclipse allows you to manage, execute, and report Java test cases efficiently:
- Setup & Configuration: Jest works out of the box with minimal setup, while Mocha and Jasmine often require additional configuration or libraries.
- Install Eclipse IDE: Ensure you have Eclipse IDE for Java Developers installed correctly to start creating and managing Java projects effectively.
- Add TestNG Plugin: Open Eclipse Marketplace, search for TestNG, install it, and restart Eclipse to enable TestNG functionality.
- Create Java Project: Navigate to File > New > Java Project, provide a project name, and configure Java settings accordingly.
- Add TestNG Library: Right-click your project, go to Build Path > Add Libraries > TestNG to include necessary dependencies.
- Create Test Class: Right-click the src > folder > New > Class, add methods, and annotate them with @Test for execution.
- Run Tests: Right-click the test class > Run As > TestNG Test to execute all tests and view results immediately.
In the last article, we installed TestNG in Eclipse and did some environment set up for the same. Subsequently, we saw how we could convert our existing Java projects into TestNG projects in Eclipse. In this article, we will create a TestNG project from scratch and write our very first test script using TestNG. We will begin by setting up a TestNG project first and then move on to writing the scripts.
Before getting started, we will quickly list down the prerequisites for getting started with a TestNG project, so that you can continue without any hiccups. If you’re looking to deepen your TestNG knowledge, don’t miss our comprehensive TestNG interview questions and answers guide to enhance your TestNG skills further.
Here’s what you need to have installed in your system-
- Eclipse IDE
- Download Selenium WebDriver and Client for Java. – Visit the official website and hit the download button for Java file.

Download and extract these Selenium bindings. We will be using these to add External JAR files later in this Selenium TestNG tutorial.
Watch this video to learn how to set up and use TestNG with Selenium to automate your testing process. We will also introduce the TestNG Priority method, which allows you to write easy-to-read and maintainable tests.
Create TestNG Project In Eclipse
First of all, you will need to launch Eclipse and then follow the steps below to create a new TestNG project.
Step 1: Navigate to File > New > Java Project.

Step 2: Give your project a name for example, ‘LambdaTestNG’ and click on Next.

Step 3: On the next screen, you will see the Java settings for your new project. To make it a TestNG project just navigate to the Libraries tab and click on the Add Library button.

Step 4: Choose TestNG from the list of libraries and click Next.

Step 5: Now, you will see TestNG added to your project libraries. Click on Finish and you are all set with your testNG project.

Your Java project has been created successfully and you will be able to see it by clicking on the Package Explorer button on the left panel.

Adding Selenium JAR files To Selenium TestNG Project
Step 1: Next, you need to add the Selenium API JAR files to your TestNG project “LambdaTestNG”. Right-click on the Project and then hit the Properties.

Step 2: Now, you need to perform JAR selection from the Selenium Java language bindings which you downloaded in your systems. Navigate or choose the path where you have downloaded the Selenium Java language bindings. Add all the JAR files (even the ones inside the libs folder).

Step 3: After adding all JAR files, hit the button to Apply and Close.

Awesome, now you will notice a folder for Referenced Libraries in the project LambdaTestNG.

With these steps, your java project is now all set to execute TestNG, the only thing you will have to do is import the Selenium libraries and browser driver(make sure to use the driver compatible with your system browser version) to run Selenium code.
Creating a TestNG class in Eclipse
Creating a TestNG class is as easy as creating a Java class. All you need to do is follow the steps mentioned below and have your first testNG class ready.
Step 1: Navigate to src from the project folder and right-click the same. You will see TestNG as an option in the dropdown towards the bottom. Click on it and you will now see two sub-options to either create a TestNG class or convert the class to TestNG. As we are creating a new TestNG class, you need to select the first option.

Step 2: Generally, the source folder name is auto-filled but if it is not, you can simply browse through the same. Next, you can give any name to your class, for example, ‘TestNGTestOne’ and its package. For now, we will keep the basic annotations selected @BeforeMethod and @AfterMethod. However, Annotations can be configured at a later stage as well depending upon your test scenario. If you wish to configure them now, you can refer to the TestNG annotations.

Step 3: You will now see a class(TestNGTestOne.java) in your project directory with default methods, viz f(), as well as beforeMethod() and afterMethod() that you can see were checked in the screenshot above.

You are now all set to write code in your first TestNG class, but before doing so let me quickly brief you about the TestNG annotations that we can see in the class we just created.
- @Test annotation implies that the method is a test method and any code written under it constitutes to be a test case.
- @BeforeMethod implies that the method beneath should be running before the test method.
- @AfterMethod, just as the name suggests implies that the method should run after the test method.
Watch this video to learn about the TestNG Annotations and how they help provide better structure and readability to the code.
Writing Our First Test Case Using Selenium and TestNG
Test Scenario: We will write a simple test script wherein we will just open up a browser, navigate to Google, verify the title, and then quit the browser session. You will see the utility of the @BeforeMethod and @AfterMethod annotations as well.
Here is the java test file “TestNGTestOne.java”.
Running Selenium TestNG Script
Right-click on the test script and navigate to Run As >> TestNG Test.

After running the test script, as shown above, you can verify the results of the test. This can be seen either on the TestNG reports or the console itself.
Now we will write our first script using Selenium and TestNG.
With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.
Here’s a short glimpse of the TestNG certification from LambdaTest:
TestNG Reports and Results
As we mentioned above, once you have executed the test shown above you will be able to see its corresponding results either in the Eclipse Console or under TestNG reports under the test-output folder that gets created automatically as soon as you run your first test in the project directory.
Results through Console
You have two options to view your results via the eclipse console which are displayed as two tabs viz, Console and Results of running class < your class name >
Console–

TestNG Results–

Results through TestNG Reports
TestNG offers a rich space for detailed reports of your test scripts that are visible under the test-output folder. We shall discuss briefly it as it is a huge topic altogether. These reports can be seen here:

As you can see there are a number of different reports that can be seen and used for subsequent reporting. We shall discuss more about these reports in our upcoming posts. For now, you can go back to any website you like and practice creating a basic test script using the TestNG framework & cover up the basics as shown in this article. In the next post, we will jump on to understand more about the TestNG XML file.
For an all in one session on performing a complete TestNG tutorial with Selenium you can checkout this video.
Bonus Pointer: Use A Cloud Selenium Grid For Parallel Testing
Kudos on creating the TestNG project in Eclipse and running the Selenium TestNG script. Right now you have triggered the Selenium TestNG script using a Selenium WebDriver in your own machine but that isn’t a scalable approach. It’s okay if you’re a novice at Selenium testing. However, after some time your test suites are bound to get bigger as you may have to run automated browser testing over dozens or maybe hundreds of browsers + OS combinations.
Doing so with Selenium WebDriver can consume a lot of time and effort because the tests will be queued and executed one after another.
This is where you can leverage a Selenium Grid which can allow you to run these test cases in parallel. So read this Selenium Grid tutorial and fasttrack your Selenium testing experience like a pro!
If you’re already familiar with Selenium Grid and have it set up in your system, even then there are going to be scale issues. You will need to expand the Selenium Grid with new devices and browsers that would be launched in the market. And that would require a lot of investment in hardware and maintenance of your local Selenium Grid. Which is why it is recommended to use a cloud Selenium Grid like LambdaTest.

LambdaTest will help you trigger your Selenium tests with TestNG over 3000+ real browsers and operating systems. That way, you won’t even have to worry about which browsers are new in the market or maintaining your in-house Selenium infrastructure so you could focus on writing better Selenium testing scripts.
In order to run your same script on LambdaTest Selenium Grid, you will need to sign up with LambdaTest(Its FREE) and do a couple of changes to your project setup. Run your TestNG scripts on Selenium Grid Cloud. Happy Testing!
Frequently Asked Questions (FAQs)
Can TestNG tests be executed in a specific order?
Yes, TestNG allows controlling the execution sequence of tests using the priority attribute in the @Test annotation. For example, tests with priority=1 will execute before those with priority=2. This ensures a logical test flow and avoids false failures due to incorrect sequencing.
How can we group tests in TestNG for selective execution?
TestNG supports groups that let you categorize tests such as smoke, regression, or functional. You can assign groups using @Test(groups={“smoke”}) and execute them selectively via the TestNG XML file or command line. This helps run critical or specific test sets efficiently.
Can TestNG handle data-driven testing?
Yes, TestNG provides the @DataProvider annotation for data-driven testing. It allows supplying multiple input sets to a single test method, running it repeatedly for each data set. This improves efficiency and helps test various scenarios like login credentials or search terms.
Is it possible to run Selenium TestNG scripts across multiple browsers?
Absolutely. You can configure WebDriver instances for Chrome, Firefox, or Edge within your TestNG tests. Using Selenium Grid or cloud platforms like LambdaTest, you can run tests simultaneously across browsers and OSs, ensuring cross-browser compatibility.
How do TestNG annotations improve test maintainability?
Annotations like @BeforeMethod, @AfterMethod, @BeforeClass, and @AfterClass help separate setup, teardown, and test logic. This modular approach minimizes duplication and keeps tests clean and maintainable.
Can we integrate TestNG reports with CI/CD pipelines?
Yes, TestNG generates XML and HTML reports after execution. These can be integrated into CI/CD tools like Jenkins, GitLab, or Azure DevOps to automate test runs, track results, and identify failures faster, improving deployment reliability.
How can Selenium and TestNG handle dynamic web elements?
Selenium provides explicit waits like WebDriverWait and ExpectedConditions to manage dynamic elements. Combining these with TestNG assertions like assertEquals or assertTrue ensures reliable validation without flaky tests.
Is it possible to run multiple Selenium TestNG classes in parallel?
Yes, TestNG supports parallel execution at the method, class, or suite level through its XML configuration. This reduces execution time and enhances test efficiency, especially in large projects with multiple independent test classes.
How can you capture failures in Selenium TestNG tests effectively?
TestNG provides listener interfaces like ITestListener to detect failures. You can implement screenshots, logging, or recovery steps automatically upon failure, improving debugging and maintaining robust test suites.
Can TestNG be used with Page Object Model for better scalability?
Yes, integrating TestNG with the Page Object Model (POM) enhances scalability. POM structures page elements and actions in separate classes, allowing TestNG tests to reuse them efficiently, leading to cleaner, modular, and maintainable automation frameworks.
Author