Welcome back! In this guide, we'll cover everything you need to set up your environment to write Java-based test automation scripts using Selenium. By the end, you'll be ready to create your first project and start coding. Let's dive in.
1. Install Java
First, ensure that Java is installed on your system. Java is essential because we'll write our test scripts in Java. Follow these steps:
Download the latest LTS version of Java (e.g., Java 17 or Java 21) from the Oracle or AdoptOpenJDK website.
Install the JDK and set the
JAVA_HOME
environment variable:Windows: Add the JDK's
bin
directory to thePATH
variable.Mac/Linux: Use the
export JAVA_HOME=/path/to/jdk
command in your terminal.
Verify installation by running
java -version
in your terminal or command prompt.
2. Download and Install Eclipse
Eclipse is the editor we'll use to write and manage our code. Here's how to get started:
Visit the Eclipse Downloads page and select Download Packages.
Select the Eclipse IDE for Enterprise Java and Web Developers package. This package includes tools for Java development and Maven integration. Check the image below for reference.
Choose the version that matches your operating system:
Windows Users: Download the Windows x86_64 version.
Mac Users: Choose the Apple Silicon version for M1/M2 chips or the x86 version for Intel-based Macs.
Once downloaded, unzip the file and open the Eclipse folder.
Launch Eclipse by clicking the application launcher.
3. Set Up Your Workspace
When Eclipse starts, it will ask you to choose a workspace. This is the folder where your projects and files will be saved. You can use the default location or choose a custom path. Once done, click Launch.
4. Creating Your First Java Project
Eclipse makes it easy to create a new project:
Step 1: Go to "File" > "New" > "Java Project."
Step 2: Name your project (e.g., "Introduction").
Step 3: Keep the default settings for the JRE Environment and click "Next."
Step 4: Click "Finish." If prompted to create a module, select "Do not create."
You now have a Java project with an empty source folder. This is where your scripts will go.
5. Introducing Selenium and Maven
Selenium is a library for browser automation. However, your project doesn't know about Selenium yet. We’ll use Maven, a tool for managing dependencies, to add Selenium to our project.
What is Maven?
Maven is a central repository for Java libraries. Developers upload libraries (like Selenium) to Maven so others can easily access them. We'll use Maven to pull the Selenium library into our project.
Adding Maven to Your Project
Step 1: Right-click on your project in Eclipse.
Step 2: Select "Configure" > "Convert to Maven Project."
Step 3: Provide the following details when prompted:
Group ID: This is your organization or project identifier (e.g., "com.example").
Artifact ID: This is the project name (e.g., "selenium-test").
Packaging: Select "jar."
Step 4: Click "Finish."
Your project will now have a pom.xml
file. This file is the heart of your Maven project and will manage all your dependencies.
6. Adding Selenium to Your Project
To add Selenium:
Step 1: Visit mvnrepository.com.
Step 2: Search for "Selenium Java."
Step 3: Choose the latest version of Selenium from the search results.
Step 4: Copy the dependency code (four lines of XML) provided on the page.
Step 5: Paste this dependency into the
pom.xml
file under the<dependencies>
section:<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.27.0</version> </dependency>
Step 6: Save the
pom.xml
file. Maven will automatically download the necessary Selenium JAR files and add them to your project as shown in the image below.
7. Verifying Your Setup
Check the "Maven Dependencies" folder in your project to ensure the Selenium JAR files are present.
If the files don’t appear, right-click your project and select "Build Automatically" to force Maven to download them.
8. Common Errors and Fixes
Maven Dependencies Not Downloading:
Right-click the project > Maven > Update Project.
Browser Driver Errors:
- Ensure you’re using WebDriverManager to avoid manual driver setup issues.
Unsupported Java Version:
- Use a compatible Java version for Selenium (Java 17 or later is recommended).
9. Next Steps
Congratulations! You've successfully:
Installed Eclipse.
Created a Maven project.
Added Selenium dependencies.
In the next blog, we’ll learn about creating a WebDriver object and writing your first Selenium test script. Stay tuned!
Check out the complete series on Selenium for more such articles: The Selenium Guidebook