Table of Contents
Have you ever wanted to automate browser testing but did not know where to begin? If so, you have come to the right place! The goal of this tutorial is to teach you how to setup Eclipse and Selenium WebDriver from scratch to start writing automated browser tests in Java.
We will install all the components, configure the environment, and you will be writing your first WebDriver script in no time!
Why Selenium and WebDriver?
Test automation is becoming critical for software teams to efficiently test web apps. The open source Selenium tool is the most popular browser automation framework – over 60% of software testers use it!
Selenium supports multiple languages through client driver libraries:
| Language | Client Library |
|---|---|
| Java | Selenium WebDriver for Java |
| C# | Selenium WebDriver for .NET |
| Python | Selenium WebDriver for Python |
| JS | WebDriverIO |
The Java bindings have the most mature capabilities and features. Plus Java is one of the most used languages for enterprise applications. That is why we will use Java in this Selenium tutorial series.
WebDriver is the modern automation library provided by Selenium that overcomes limitations in the old RC library. Some benefits of WebDriver:
- Supports latest browser versions
- Native browser interactions
- Asynchronous executions
- Mobile and desktop app testing
Millions of software engineers and testers have moved to WebDriver for test automation. Through this comprehensive hands-on guide, you‘ll join them!
Install Java
Since we will use Java to write Selenium tests, the first step is to install Java.
-
Download the latest Java JDK 8 or Java JDK 17 based on your preference. Accept the license terms to proceed with installation.
-
Follow the installation wizard to complete the setup. When prompted, make sure to install JDK instead of just JRE. Also set the JDK path in system variables.
-
To verify, open a terminal and run
java -versionwhich should print the installed Java version proving it is correctly setup.
That‘s it! Java is ready to go. Next let‘s setup Eclipse IDE.
Install and Configure Eclipse IDE
Eclipse IDE will be the workspace where we create Selenium test projects and write test scripts.
-
Download Eclipse IDE 2022-12 (latest version). Get the Eclipse IDE for Java Developers which comes bundled with all essential plugins.
-
Extract the downloaded zip file into a folder like C:\Users\Name\eclipse. Do not install into spaces containing parenthesis as it can cause issues.
-
Launch Eclipse by running
eclipse.exe. Then select your workspace folder when prompted. I recommend creating a dedicated folder called SeleniumWorkspace to save your test projects. -
Once launched, you will see the Eclipse IDE perspectives with panels and menus. It is ready to use!
Some tips for working with Eclipse:
- Install Eclipse Marketplace plugins like TestNG, Maven Integration for added productivity
- Create shortcut links to frequently used views like package explorer
- Learn keyboard shortcuts like Ctrl+3 for quick command access
Overall the Eclipse installation is quick and simple. Next we will setup the WebDriver Java libraries.
Configure Selenium WebDriver in Eclipse
To use Selenium WebDriver APIs in test scripts, we need to import the Selenium client libraries into Eclipse:
-
Download the Selenium Java client driver zip file.
-
Extract the zip file which contains all the
.jarfiles required for WebDriver Java bindings. -
In your Eclipse workspace, create a new Java project. Give it a name like SeleniumTests.
-
Right click the project, select Build Path -> Configure Build Path -> Add external JARs.
-
Select all JARs from the extracted client driver folder and click Apply.
The libraries are now imported and ready to use for scripting browser tests!
As an example, here is a simple WebDriver test in Java that opens google.com:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyFirstWebDriverTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
}
}
Try creating a similar script in your project to validate everything is configured correctly.
Install Browser Drivers
The Selenium client library allows automating various browsers like Chrome, Firefox, Safari etc.
However some browsers need an additional executable driver binary for WebDriver to interface with the browser.
Here are the driver requirements for common browsers:
| Browser | Driver Needed |
|---|---|
| Google Chrome | ✘ |
| MS Edge | ✘ |
| Mozilla Firefox | ✔ (geckodriver) |
| Apple Safari | ✔ (safaridriver) |
Download the required drivers from the Selenium site.
For example to test on Firefox, grab the geckodriver that matches your Firefox version. Add the driver exe to your system PATH so WebDriver can detect and use it.
Now you‘re all set to start writing test automation scripts for WebDriver in Java!
Conclusion
In this comprehensive guide, we covered how to setup Eclipse, Java, and configure WebDriver from the ground up:
✔ Installed latest Java JDK
✔ Setup Eclipse IDE
✔ Imported Selenium WebDriver Java libraries
✔ Browser driver installation and PATH configuration
You now have the foundation to begin test automation using Selenium WebDriver!
In upcoming tutorials, we will:
- Write test scripts for web element interactions
- Run cross-browser tests on Chrome and Firefox
- Integrate with CI/CD pipelines like Jenkins and Docker
- and more!
So stay tuned, and happy test automating!
Recommended Tutorials
- Cucumber BDD Framework with Selenium WebDriver in Java
- Efficient Selenium Scripting and Troubleshoot Scenarios
- Debugging Selenium Scripts with Logs