How to Configure Webdriver-Selenium for Java in Eclipse on Windows

Posted by Jim Holmes

It’s easy to get up and running with WebDriver using Eclipse and the Java bindings on Windows.

In this article we’ll show you how to get the various parts and pieces, plus write and run one simple test.

The Components You'll Need

To run WebDriver tests in Java with Eclipse you’ll need:

  • Java
  • Eclipse
  • A test framework (We’ll use JUnit; there are many you can use)
  • WebDriver’s Java bindings
  • WebDriver’s Java Standalone Server library
  • Mozilla’s Gekodriver proxy for Firefox

Regardless of what platform you’re on, there are many, many different environment setups for Java in Eclipse. Different teams will use different build and dependency systems such as Maven, Gradle, etc. We’ll be using the most simple approach possible: A statically linked library.

Getting Java

Naturally WebDriver’s Java bindings require a version of Java to be on the system you’re running your tests from. WebDriver’s Java bindings require only the Java Runtime Environment (JRE), but you’ll be fine if you have either the Java Development Kit (JDK) or JRE installed. There are also different versions for Enterprise, SE, and other Java JRE/JDKs.

You can get Java from its download site.

We’ll skip walking through Java installation in this article since it will vary based on the version you select.

Getting Eclipse

Eclipse is one of several widely popular editors for Java. It’s an open source project maintained by the Eclipse Foundation, and has a huge marketplace of add-ons, tools, and customizations.

You can download the latest version of Eclipse for your operating system at http://www.eclipse.org/downloads/eclipse-packages/.

The installer is small utility that will configure, download, and install the particular Eclipse variant you’re looking for.

For brevity’s sake, and due to platform differences, we’ll skip the actual install steps in this guide.

Once Eclipse is installed you’re free to customize it as you like. There are a wealth of themes and editor tweaks available both from the larger community and the Eclipse Marketplace (https://marketplace.eclipse.org/).

Creating The Project

Eclipse works with the concepts of workspaces—folders and spaces where you work with your projects. In this example I’m using a small workspace I’ve just created for example projects. Teams use different approaches for workspaces and project structures. Use whatever you like with your own small sample projects; however, make sure to follow your team’s pattern for real work. (Workspace and project structure is almost as hotly debated as tabs versus spaces. Be kind.)

This article uses a simple Java Project created by using the menu File => New => Java Project.

Fill out the basic information on the New Java Project dialog, then click Finish to proceed.

Adding WebDriver to Eclipse

As mentioned earlier, there are many ways to include WebDriver in your Eclipse projects. Most mature teams use a build and dependency management system like Maven or Gradle. This post uses a simple static library inclusion due to the wide variances in how teams organize Maven and Gradle.

Load Libraries Into Eclipse

Download the Java WebDriver bindings and save the zip file to a handy spot. I generally include a “libs” folder in my projects for external libraries like this. In Eclipse right click on the project, then select Build Path => Configure Build Path.

Load libraries into eclipse

In this example we’ll also be using the FirefoxDriver for Mozilla’s browser. That means you’ll also need to download the Selenium Standalone Server, which includes the FirefoxDriver, plus remote drivers if you’ll be using them.

In the Project Properties dialog, select Java Build Path then the Libraries tab. Click Add External Jars, then browse to where you saved the Selenium Java language bindings—the “libs” folder in my example. Select that file and click Open to save your changes.

Position The Browser Driver Proxy

Finally we’ll need to set up the Firefox driver proxy. Again, there are numerous ways to handle where to place the executable, how to configure it, how to invoke it, etc. These ways also differ for other browser types. (Remember, you likely need similar proxies for other browsers like Chrome, Internet Explorer, or Edge.) In this case, I’ve saved the file “geckodriver” to my ~/Utils folder. Firefox’s Gecko driver requires you to specify its location in code, not just have it on the path. You’ll see that in the sample code below.

You’re done!

Writing Your First Test

WebDriver doesn’t know how to do anything other than talk to the browser driver. As a result, you’ll need some sort of test framework to execute your tests, make assertions, and report test status. We’ll use JUnit because 0) it’s very popular and 1) it’s included in Eclipse’s default installation.

Add a test case to your Eclipse project via right click => New => JUnit Test Case.

Give your test case a good name in the resulting dialog and click Finish to create the file. Note I’ve used the default namespace for this test case—and that Eclipse is rightfully complaining about it. Again, be kind. Follow your teams’ standard practices for project organization and naming.

Note that Eclipse will helpfully add JUnit 4 to your project if it’s not already on Eclipse’s build path.

You’ll find your first test case set up and ready to author.

A Simple Test

Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance. The example is extremely simple and doesn’t follow normal practices like using Page Object Patterns. This is example code, not production code!

import static org.junit.Assert.*;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.junit.Test;

public class CheckSauceLabsHomePage {

@Testpublic void site_header_is_on_home_page() {   

WebDriver browser;   

//Firefox's geckodriver requires you to specify its location.    System.setProperty("webdriver.gecko.driver","/Users/jimholmes/Utils/geckodriver");   

browser = new FirefoxDriver();    browser.get("http://saucelabs.com");   

WebElement header = browser.findElement(By.id("site-header"));   

assertTrue((header.isDisplayed()));   

browser.close();

}

}

Running The Test

Execute the test by right-clicking in the test body and select Run As => JUnit Test Case. Alternatively, use the shortcut combo Alt-Shift-X, T. The test will run and you’ll see the results in the JUnit Test Explorer pane.

Summing Up

In this post you learned a bit about the various components that make up Selenium WebDriver. We also showed you how to install Eclipse, gather up the various WebDriver pieces necessary for Java tests in Eclipse, and we showed you an extremely simple test.


Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.