🚀 Run Java in VS Code and Integrate Selenium WebDriver with Maven

Опубликовано: 04 Май 2026
на канале: QA_AI_WIZARDS
146
1

🚀 Selenium + Java + Maven Setup in Visual Studio Code

This will help you build a Selenium automation setup using Java and Maven inside Visual Studio Code, without using code snippets. Perfect for QA testers and automation engineers who want to learn the full process in simple terms.

✅ Step 1: Prerequisites
Make sure the following tools are installed and properly configured:

Java JDK (version 11 or higher)
➤ Confirm with: java -version

Apache Maven
➤ Confirm with: mvn -v

Visual Studio Code

Essential VS Code Extensions:

Java Extension Pack

Language Support for Java

Maven for Java

➤ You can install extensions from the VS Code Extensions sidebar using Ctrl + Shift + X.

🛠 Step 2: Create a Maven Project in VS Code
Open the Command Palette using Ctrl + Shift + P

Type and select Maven: Create Maven Project

Choose the maven-archetype-quickstart

Select the latest version

Enter the following project details:

Group ID: com.demo.selenium

Artifact ID: seleniumtest

Choose a folder to create the project

Click Open Project when prompted

📁 Expected Project Structure
Once created, the folder layout should look like this:

Project root: seleniumtest

Contains pom.xml

Source files located under:

src/main/java/com/demo/selenium/App.java

src/test/java/com/demo/selenium/AppTest.java

🧪 Step 3: Add Selenium WebDriver to Your Maven Project
Inside the pom.xml, add a dependency for Selenium by writing the following under the dependencies section:

Group ID is org.seleniumhq.selenium

Artifact ID is selenium-java

Version is 4.21.0 (or the latest stable version)

➤ Save the file to trigger Maven to automatically download the required Selenium JARs.

✏️ Step 4: Create Your First Selenium Test File
Inside the directory src/main/java/com/demo/selenium/, create a new file called LoginTest.java.

Here’s what your Selenium test should conceptually do:

Set up the ChromeDriver using a tool like WebDriverManager (to avoid manual driver downloads)

Launch the Chrome browser

Maximize the browser window

Navigate to the OrangeHRM login page (or any site of your choice)

Print the page title and URL to the console

Close the browser

➤ Make sure your class is saved in the correct package path: com.demo.selenium

▶️ Step 5: Run the Java Program in Visual Studio Code
There are three common ways to run your program:

🔹 Option A: Using the VS Code Run Button
Open your test file (e.g., LoginTest.java)

Click the green Run triangle icon above the main() method

🔹 Option B: Using Maven in Terminal
Open the terminal in VS Code (Ctrl + ~)

Run these Maven commands:

First command: mvn clean compile

Then run: mvn exec:java -Dexec.mainClass=com.demo.selenium.LoginTest

➤ Your pom.xml must have the Maven exec plugin properly configured for this to work.

🔹 Option C: Using Code Runner Extension
Install Code Runner from the Extensions tab

Right-click on your Java file

Click Run Code

🧩 Step 6: Automatically Manage ChromeDriver with WebDriverManager
To avoid manually downloading and maintaining ChromeDriver, add WebDriverManager to your Maven dependencies.

In the pom.xml, add:

Group ID is io.github.bonigarcia

Artifact ID is webdrivermanager

Version is 5.8.0 (or latest)

After adding it:

Use WebDriverManager in your test to automatically handle ChromeDriver setup

Your test file should start by calling a method to set up the ChromeDriver dynamically

Proceed to create an instance of the ChromeDriver and continue your test as planned

➤ This saves you from errors like "driver not found" or mismatched versions.

🧹 Final Notes & Tips
Always ensure your file and folder structure matches the Java package names

Avoid spaces in project folder names (e.g., avoid using “New Folder”)

If you get a ClassNotFoundException, re-check:

Package names

Folder structure

Proper compilation

Use Maven for clean builds and dependency handling

Use mvn clean to wipe previous compiled data when debugging

🔖 Hashtags
#SeleniumJava, #Maven, #VSCode, #JavaAutomation, #WebDriver, #TestAutomation, #AutomationFramework, #QAEngineer, #pomxml, #BuildTools, #JavaProject, #RunJavaVSCode, #JavaBeginner, #MavenSetup, #SeleniumTesting, #DevSetup, #WebDriverManager, #TestSetup, #CIReady, #AutomationTools