Setting Up Your Java Selenium Environment
Let’s build a professional Selenium WebDriver automation framework with Java and Maven.
Prerequisites
Before we start, ensure you have:
- Java JDK 11+ installed
- IDE (IntelliJ IDEA or Eclipse)
- Basic Java knowledge
- Understanding of web technologies
Java Development Kit Setup
First, verify Java installation:
java -version
javac -version
If not installed, download from Oracle or use:
# macOS with Homebrew
brew install openjdk@17
# Ubuntu/Debian
sudo apt install openjdk-17-jdk
# Windows with Chocolatey
choco install openjdk17
Maven Project Creation
Create a new Maven project structure:
mvn archetype:generate \
-DgroupId=com.automation \
-DartifactId=selenium-framework \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
cd selenium-framework
Maven Dependencies
Update pom.xml with Selenium and testing dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.automation</groupId>
<artifactId>selenium-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>4.15.0</selenium.version>
<testng.version>7.8.0</testng.version>
</properties>
<dependencies>
<!-- Selenium WebDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<!-- WebDriverManager for automatic driver management -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.6.2</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Project Structure
Organize your framework:
selenium-framework/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/automation/
│ │ ├── pages/ # Page Objects
│ │ ├── utils/ # Utilities
│ │ └── config/ # Configuration
│ └── test/
│ ├── java/
│ │ └── com/automation/
│ │ └── tests/ # Test classes
│ └── resources/
│ ├── testng.xml
│ └── config.properties
├── pom.xml
└── README.md
First Selenium Test
Create a simple test to verify setup:
// src/test/java/com/automation/tests/FirstTest.java
package com.automation.tests;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class FirstTest {
private WebDriver driver;
@BeforeMethod
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void testGoogleTitle() {
driver.get("https://www.google.com");
String title = driver.getTitle();
Assert.assertTrue(title.contains("Google"),
"Page title should contain 'Google'");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
TestNG Configuration
Create testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Selenium Test Suite" parallel="tests" thread-count="3">
<test name="Smoke Tests">
<classes>
<class name="com.automation.tests.FirstTest"/>
</classes>
</test>
</suite>
Running Tests
Execute tests with Maven:
# Run all tests
mvn clean test
# Run specific test
mvn test -Dtest=FirstTest
# Run with specific browser
mvn test -Dbrowser=chrome
IDE Configuration (IntelliJ IDEA)
- Import as Maven project
- Enable TestNG plugin
- Configure JDK 17+
- Set up Run Configurations for TestNG
WebDriverManager Benefits
WebDriverManager automatically:
- Downloads the correct driver version
- Manages driver binaries
- Resolves compatibility issues
- Updates drivers when needed
// Automatic driver management
WebDriverManager.chromedriver().setup(); // Chrome
WebDriverManager.firefoxdriver().setup(); // Firefox
WebDriverManager.edgedriver().setup(); // Edge
Browser Options
Configure browser behavior:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
WebDriver driver = new ChromeDriver(options);
Next Steps
Now that your environment is ready, we’ll explore WebDriver fundamentals in the next lesson.
Key Takeaways
✅ Maven manages dependencies and build lifecycle
✅ WebDriverManager eliminates manual driver setup
✅ TestNG provides powerful test execution features
✅ Proper project structure enables scalability