Introduction
Selenium is an open-source tool that allows developers to write automated tests for web applications.
What is TestNG?
TestNG (Test Next Generation) is a testing framework inspired by JUnit, introducing new functionalities to make it more powerful and easier to use.
What Is Visual Studio Code?
Visual Studio Code (VS Code) is a modern, lightweight, and powerful source code editor developed by Microsoft. It supports various programming languages and is highly extensible.
Why Use Visual Studio Code for Selenium and TestNG?
Unlike heavier IDEs like Eclipse, VS Code is lightweight and efficient. It opens quickly and consumes less RAM, making it ideal for maintaining system performance during test execution.
Advantages of Visual Studio Code:
- Lightweight: Faster startup and execution than many Java IDEs.
- Extensibility: Supports numerous extensions for customization.
- Cross-platform: Available on Windows, Linux, and macOS.
- Integrated Git Control: Simplifies version control and team collaboration.
- User-Friendly Interface: Modern UI and easy-to-navigate layout.
Installation Process
- Download and install Java. Set the
JAVA_HOMEenvironment variable. - Download and install Maven. Set the
MAVEN_HOMEenvironment variable. - Download and install Visual Studio Code.
Setting up Visual Studio Code for Java
Open VS Code, go to Extensions (Ctrl+Shift+X), and search for Java Extension Pack. Install the extension pack which includes:
- Language Support for Java
- Java Test Runner
- Maven for Java
How to Create a Maven Project
- Press
Ctrl+Shift+Pto open the Command Palette. - Type and select: Create Maven Project.
- Choose the
maven-archetype-quickstartarchetype.
Adding Selenium and TestNG Dependencies

Edit the pom.xml file in your Maven project and add the required dependencies for Selenium and TestNG:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.19.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
TestNG Annotations Overview
- @BeforeTest: Runs before any test method in the test suite. Used for setup.
- @AfterTest: Runs after all test methods in the suite. Used for cleanup.
- @Test: Marks a method as a test case. Supports attributes like priority and groups.
- @BeforeSuite / @AfterSuite: Run before/after the entire test suite.
- @BeforeClass / @AfterClass: Run before/after the first/last test method in the class.
- @BeforeMethod / @AfterMethod: Run before/after each test method.
Writing a Sample Selenium Test with TestNG

Create a new Java class in src/test/java named SampleTest.java and add your test methods using TestNG annotations. Here’s a simple example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SampleTest {
WebDriver driver;
@BeforeTest
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com");
}
@Test
public void pageTitleTest() {
String title = driver.getTitle();
System.out.println("Page Title: " + title);
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
Running Your Test
Open the integrated terminal in VS Code, navigate to your project’s root directory, and run:
mvn test

Maven will compile the code and execute your tests using TestNG.
Conclusion
Setting up Java, Selenium, and TestNG in Visual Studio Code is straightforward, especially with Maven’s dependency management. This streamlined environment enhances productivity and enables efficient, robust web automation testing. Happy testing!