Skip to content

Selenium - Environment Setup

To start developing Selenium WebDriver scripts, you need to set up your development environment. This typically involves installing Java, an IDE, a build tool, and configuring Selenium libraries and browser drivers.

This guide focuses on a typical Java-based setup using Maven.

Step 1: Install Java Development Kit (JDK)

Section titled “Step 1: Install Java Development Kit (JDK)”

Selenium requires Java to run (specifically the JDK, not just the JRE). Ensure you have JDK 8 or a later version installed (check Selenium documentation for specific version compatibility if needed).

  • Download: Get the JDK from Oracle (https://www.oracle.com/java/technologies/downloads/) or choose an OpenJDK distribution like Adoptium Temurin (https://adoptium.net/).
  • Install: Follow the installation instructions for your operating system.
  • Configure Environment Variables (Crucial):
    • Set the JAVA_HOME environment variable to point to your JDK installation directory.
    • Add the JDK’s bin directory to your system’s PATH environment variable.
  • Verify Installation: Open a new terminal or command prompt and type java -version and javac -version. Both commands should execute and display the installed JDK version.

Step 2: Install an Integrated Development Environment (IDE)

Section titled “Step 2: Install an Integrated Development Environment (IDE)”

An IDE provides tools for writing, running, and debugging code.

Step 3: Install a Build Automation Tool (Maven)

Section titled “Step 3: Install a Build Automation Tool (Maven)”

Build tools like Maven or Gradle simplify project setup, dependency management, and building/running tests.

  • Maven: Often bundled with IntelliJ IDEA and Eclipse. If not, download from https://maven.apache.org/download.cgi.
  • Installation: Follow Maven’s installation guide, which usually involves setting M2_HOME and adding its bin directory to the PATH.
  • Verify Installation: Open a new terminal and type mvn -version. It should display the installed Maven version.

Use your IDE to create a new Maven project. This sets up the standard project structure (src/main/java, src/test/java, pom.xml).

  • IntelliJ: File -> New -> Project... -> Select Maven -> Choose JDK -> Provide GroupId (e.g., com.mycompany) and ArtifactId (e.g., selenium-tests).
  • Eclipse: File -> New -> Maven Project -> Check ‘Create a simple project’ -> Provide GroupId and ArtifactId.

Step 5: Add Selenium Dependencies (pom.xml)

Section titled “Step 5: Add Selenium Dependencies (pom.xml)”

Open the pom.xml file located in your project’s root directory. Add the necessary dependencies within the <dependencies> section. This tells Maven to automatically download the required JAR files.

<project ...>
<properties>
<maven.compiler.source>11</maven.compiler.source> <!-- Set your Java version -->
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Selenium Java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version> <!-- Use the latest stable version -->
</dependency>
<!-- TestNG (or JUnit if you prefer) -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version> <!-- Use the latest stable version -->
<scope>test</scope> <!-- Only needed for testing -->
</dependency>
<!-- WebDriverManager (Highly Recommended) -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.3</version> <!-- Use the latest stable version -->
<scope>test</scope>
</dependency>
<!-- Optional: For logging (SLF4j API + Logback Implementation) -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Surefire Plugin to run tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<!-- Configuration for TestNG (if using TestNG XML) -->
<!--
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
-->
</configuration>
</plugin>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</build>
</project>

After adding dependencies, refresh your Maven project in the IDE (Maven -> Reload Project in IntelliJ or Right-click project -> Maven -> Update Project in Eclipse) to download the JARs.

Step 6: Browser Drivers (Handled by WebDriverManager)

Section titled “Step 6: Browser Drivers (Handled by WebDriverManager)”

Previously, you had to manually download browser driver executables (like chromedriver.exe, geckodriver.exe) and manage their paths. This was tedious and error-prone.

WebDriverManager, included as a dependency above, automates this process. Simply calling WebDriverManager.chromedriver().setup(); or WebDriverManager.firefoxdriver().setup(); in your test setup code will automatically download the correct driver version for the browser installed on your machine and configure the necessary system property.

Step 7: Using Browser Developer Tools (Replacing Firebug/FirePath)

Section titled “Step 7: Using Browser Developer Tools (Replacing Firebug/FirePath)”

Tools like Firebug and FirePath are deprecated. To inspect web elements and find locators (IDs, CSS Selectors, XPath):

  • Open DevTools: Right-click on a web page element and select ‘Inspect’ or ‘Inspect Element’, or simply press F12.
  • Inspect Elements: Use the element selection tool (often an arrow icon) to click on elements on the page. The HTML structure will be highlighted in the ‘Elements’ (Chrome/Edge) or ‘Inspector’ (Firefox) panel.
  • Find Locators: Examine the element’s attributes (id, name, class). Right-click on the element in the HTML view to copy its CSS Selector or XPath (though manually crafting selectors/XPath is often better for stability).

Your environment is now ready! You can start writing Selenium WebDriver tests within the src/test/java directory of your Maven project.