How To Run Your First Selenium TestNG Test? 


If you’ve found your way to this article, you’re probably eager to learn how to run Selenium tests using the TestNG framework. Teaming up Selenium with TestNG offers you streamlined control over Selenium sessions, effortless browser creation and shutdown, and the ability to execute tests in parallel with any configuration. Furthermore, if you need to log any Selenium events as your tests run, TestNG has got your back.

Additionally, if you’re interested in cloud-based testing, you might consider using LambdaTest. LambdaTest is a cloud-based digital experience testing platform that provides access to 3000+  of browsers and operating systems, making it easier to run Selenium tests in multiple environments without the need for local setup. With LambdaTest, you can easily integrate with TestNG and run your tests on a scalable, secure, and reliable cloud infrastructure. 

Now, let’s move forward and see how to create and run a Selenium test in TestNG. It’s time to roll up your sleeves and get started!

What Is TestNG?

TestNG is an open-source testing framework that stands for ‘Next Generation.’ It’s designed to make a wide variety of testing needs easier, ranging from unit to integrated system testing. Initially, both JUnit and TestNG were created specifically for unit testing. TestNG draws inspiration from the JUnit Java platform and the NUnit .NET platform, but it also introduces some new features that make it more powerful and user-friendly than JUnit. 

Why Did TestNG Come Into Existence?

You might be wondering, if you already have the Selenium framework, why would you need TestNG with Selenium WebDriver for automation testing? The reason is that Selenium does not have a built-in mechanism or framework for generating test reports. Therefore, an external framework like TestNG is needed to provide this functionality. In addition to generating test reports, TestNG also simplifies various testing needs, including functional testing, regression, end-to-end testing, and more.

Advantages Of Using TestNG 

Here are some of the biggest advantages of using TestNG in your application development process:

  1. Support for parameterization.
  2. Support for data-driven testing through Data Providers.
  3. Ability to generate HTML test reports showing the number of test cases run, failed, and skipped.
  4. Easy integration with other tools and plugins, such as Eclipse IDE, and build automation tools like Ant and Maven.
  5. Support for parallel execution.
  6. Capability to generate logs.
  7. Unlike JUnit, TestNG does not require the use of @AfterClass and @BeforeClass in a project.
  8. TestNG does not have a constraint on the name of test methods, unlike JUnit.
  9. TestNG provides three additional setup and teardown levels: @Before/AfterSuite, @Before/AfterTest, and @Before/AfterGroup. These methods can be run before or after test suites, groups, or methods. This is particularly useful for Selenium testing, as a Selenium server and browser instance can be created before running the test suite.
  10. TestNG doesn’t require extending any class, so there’s no need for inheritance.
  11. It allows the definition of dependent test cases.
  12. TestNG supports executing test cases based on groups. For example, if you have two groups of test cases named ‘Regression’ and ‘Sanity’, you can easily execute just the test cases under the ‘Sanity’ group using the TestNG framework.

TestNG Annotations

Annotations provide extra information about a program and are indicated by an ‘@’ symbol. They don’t change the behaviour of a compiled program and are used to attach metadata to various program elements such as instance variables, constructors, methods, and classes.

Important TestNG Annotations List:

  1. @BeforeSuite: Executes before all test methods are run.
  2. @AfterSuite: Executes after all test methods have run.
  3. @BeforeTest: Runs before all the test methods in the available classes.
  4. @AfterTest: Runs after all the test methods in the available classes.
  5. @BeforeClass: Runs before the first method of the current class is invoked.
  6. @AfterClass: Executes after all test methods in the current class have run.
  7. @BeforeMethod: Runs before each test method.
  8. @AfterMethod: Executes after each test method.
  9. @BeforeGroups: Runs once for a group before all test cases in that group.
  10. @AfterGroups: Runs once for a group after all test cases in that group.


Process To Run Your First Selenium TestNG Test

Running your first Selenium TestNG test involves a series of steps to set up your test environment and write your test case. Here’s a step-by-step guide:

  1. Install Java Development Kit (JDK): The JDK is essential for running Java applications, including Selenium tests. Download the JDK from the Oracle website (, and choose the appropriate version for your operating system. Install the JDK by following the installation instructions. Verify the installation by opening a terminal or command prompt and typing `java -version`. This should display the installed version of Java.
  2. Install An Integrated Development Environment (IDE):  An IDE makes it easier to write, debug, and manage your code. Eclipse and IntelliJ IDEA are popular choices for Java development. Download and install the IDE from the respective website. Open the IDE and familiarize yourself with the interface.
  3. Setup A Maven Project: Maven is a tool that manages Java project dependencies. In your IDE, create a new Maven project. This usually involves selecting ‘New’ -> ‘Maven Project’ and following the prompts. A Maven project includes a file called `pom.xml`, which defines the project’s dependencies.
  4. Add Dependencies: In the `pom.xml` file of your Maven project, add the dependencies for Selenium and TestNG. This tells Maven to download these libraries and include them in your project. Save the `pom.xml` file, and Maven will automatically download and include the specified dependencies in your project.
  5. Write Your Test: In your Maven project, create a new Java class. You can right-click on the ‘src/test/java’ folder and select ‘New’ -> ‘Class’. In this class, you’ll write your Selenium test using TestNG annotations. Here’s an example test case using Selenium with TestNG:


import org.openqa.selenium.WebDriver;


import org.testng.Assert;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;


public class MyFirstTest {

    private WebDriver driver;


    public void setup() {

        System.setProperty(“”, “path/to/chromedriver”);

        driver = new ChromeDriver();




    public void testGoogleTitle() {


        String title = driver.getTitle();

        Assert.assertEquals(title, “Google”);




    public void tearDown() {

        if (driver != null) {






Make sure to replace path/to/chromedriver with the actual path to your ChromeDriver executable.

  1. Run Your Test: Begin by opening your Integrated Development Environment (IDE) and locating your test class. This class should contain the test script you’ve created using Selenium. 

Once you’ve identified the test class within the IDE, right-click on it to open a context menu. Within this menu, find the option labeled ‘Run As’. Hovering over or expanding the ‘Run As’ menu should reveal several execution options. Among these options, select ‘TestNG Test’. Upon selecting this option, TestNG will start running your test. During the execution, you should observe the web browser automatically opening, navigating to the Google website, and then closing as per your test script. If your test encounters any problems during execution, the IDE will highlight these issues by displaying error messages, helping you diagnose and fix any issues that may arise.

  1. View Results: After running your test, view the results in the TestNG Results tab of your IDE. This will show whether your test passed or failed and provide additional information, such as any error messages. If your test failed, examine the error messages, fix the issues in your test code, and run the test again.

That’s it! You have now successfully written and run your first Selenium TestNG test. From here, you can expand your test suite, testing different scenarios and functionalities of your web application.

Best Practices To Run Your Selenium TestNG Test

Here are some best practices to consider when running your Selenium TestNG tests:

  1. Design Modular And Reusable Test Scripts: Break down your tests into smaller, modular scripts that can be reused across different test cases. Utilize TestNG’s `@BeforeMethod` and `@AfterMethod` annotations for setup and cleanup tasks that are common to all test methods.
  2. Use Descriptive Test Method Names: Give your test methods descriptive names that clearly indicate what the test is verifying. This helps in understanding the test purpose and debugging when tests fail.
  3. Utilize TestNG’s Advanced Features: Make use of TestNG features like parameterization, grouping, and dependency management. Use parameterization to run the same test with different sets of data. Group related test methods and execute them together.
  4. Implement Asserts And Verifications: Use assertions to validate expected outcomes. Use soft asserts (provided by TestNG) when you want the test to continue executing even if an assertion fails.
  5. Run Tests On Multiple Browsers And Devices: Use Selenium Grid or cloud-based platforms like LambdaTest to run your tests on different browsers, operating systems, and devices. This helps ensure the compatibility of your application across different environments.
  6. Implement Explicit Waits: Avoid using implicit waits or `Thread.sleep()`. Use explicit waits with expected conditions to ensure that elements are ready for interaction.
  7. Handle Exceptions Properly: Implement proper exception handling in your tests. Log relevant information when exceptions occur to aid in debugging.
  8. Organize Test Suite With testng.xml: Utilize the `testng.xml` file to organize and manage your test suite. Specify the test methods, groups, and parameters in the XML file for better control over test execution.
  9. Implement Continuous Integration (CI): Integrate your Selenium TestNG tests with CI/CD tools like Jenkins or Travis CI. Run your tests automatically on every code push or periodically to catch regressions early.
  10. Review And Analyze Test Reports: Review the test reports generated by TestNG, including HTML reports and logs. Analyze failed tests and take appropriate action to fix issues.


By combining the power of Selenium for web application automation and TestNG for test management, you can create robust and maintainable test suites. Remember to set up your environment with the appropriate tools and libraries, follow best practices for writing test cases, and maintain a structured approach to organizing your tests. As you become more familiar with Selenium and TestNG, you can explore advanced features and further optimize your testing process. Keep in mind that automation is just one part of an effective testing strategy, and it should complement manual testing to achieve comprehensive coverage and high-quality software.

Mike Adams
Mike Adams
I am a writer who is writing content since 8 years and it has become my hobby.


Please enter your comment!
Please enter your name here

Share post:




More like this

TEMU Affiliate Program 2024: Earn Up to SAR 400,000 a month!

Hey there, my friend! I've got to tell you about...

Your guide to winning big in live casino games

Do you want to take your online gaming to...

Best Practices for Developing an Effective Knowledge Management Process

Developing an effective knowledge management process is crucial for...

L Krishna Home Cleaning Service in Hyderabad Review – Affordable and Reliable?

Living in a high-rise building in Hyderabad was a...