Getting Started with WebDriver-Selenium for Java in Eclipse

Getting started with WebDriver’s Java bindings in Eclipse is a snap. Here's how to get started.

Getting started with WebDriver’s Java bindings is easy -- if you know how to connect the pieces together. Once the pieces are in place, development is a snap.

In this article we’ll show you how, plus write and run one simple test.

The Components You'll Need

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

  • Eclipse
  • A test framework - We’ll use JUnit5; there are many you can use.
  • WebDriver’s Java Code Library
  • A browser-driver - Our examples will use chrome
  • A dependency manager - our example will be Maven

There are different ways to add dependencies in Eclipse with Java. Common dependency managers include Maven and Gradle. An older version of this article added the dependencies by hand.

Getting Eclipse

Eclipse is one of the most popular development environments for Java. It is an open source project maintained by the Eclipse Foundation, and has a large marketplace of add-ons, tools, and support. You can download the latest version of Eclipse for your operating system at http://www.eclipse.org/downloads/eclipse-packages/. In our example we’ll select “Eclipse IDE for Java Developers.”

The installer saves to your local drive or your browser’s “downloads” directory. Download and run it.

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 an existing workspace where I have several different small 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 clicking on “Create a new java project” from the opening screen.

Eclipse Java new project

Fill out the basic information on the New Java Project dialog - make sure the java version is 1.8 -  then click finish to proceed.

Add Maven - A dependency Manager

That various drivers depend on other libraries. In some cases, those libraries depend on other libraries. Instead of downloading and installing them by hand, you’ll use Maven, a dependency registration and management service. You’ll add one entry for ChromeDriver, another for Junit5 (“Jupiter”), and be off to the races. So Right-click on the project, select configure, and convert to become a maven project.

Add Dependencies

In the bottom-left of the package explorer you will now see a pom.xml. We’ll add a reference to Junit Jupiter and Webdriver into the POM, by adding a dependencies xml fragment, below build and before </project>. Here’s the snippet:

   <dependencies>
   <dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-api</artifactId>
   <version>5.1.0</version>
   <scope>test</scope>
   </dependency>
   <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>3.141.59</version>
   </dependency>
 </dependencies>

Time for the WebDriver Components

Before we proceed, a quick review of WebDriver’s makeup is helpful. WebDriver consists of a number of different components, and it’s important you’re clear on what pieces you’ll need. The Selenium HQ page introducing WebDriver has great detail on how WebDriver works. You should read that; however, here’s an abbreviated version:

The Browser Driver

WebDriver’s lowest-level component is a browser driver which manipulates a browser directly through that browser’s automation interface. This is a program that the libraries will call. Browser vendors (Microsoft, Mozilla, Google, etc.) handle the details inside their own browser, which means the folks that make the browser are also handling the mechanics of automation for that specific browser. The names of the browser-drivers are Chromedriver (for Chrome), Geckodriver (for FireFox), SafariDriver(Safari) and InternetExplorer Driver (IE and Edge). Place them in a directory that is part of your environment PATH, so that no matter what working directory you are in, a command-line calll to the programs will execute them.

Language Bindings

Language bindings allow you to write your WebDriver scripts in whatever language works best for your team. These bindings are also called client drivers, as opposed to browser drivers. These language bindings are maintained by the Selenium WebDriver open source team. “Official” bindings are available for Java, C#, Python, Ruby, and JavaScript(Node). Other language bindings my exist, but are not officially supported by the Selenium Project.

If you’re only running your tests on one local system then a language binding is all you need; however, if you’re running your browser tests on multiple machines then you’ll need a server component.

Selenium Server

Selenium Server is a component that manages running WebDriver instances on remote machines. Selenium Server is also the central hub when working with Grid configurations. When working with Sauce Labs, Sauce can provide the entire grid; just have your tests tunnel out to connect. For this example, you’ll run the tests locally, so you do not need to run a server.

Adding WebDriver to Eclipse

As mentioned earlier, there are many ways to include WebDriver in your Eclipse projects. We pick up WebDriver automatically by adding it to the pom.xml file.

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. If you team has never done this before or you are new to jUnit, think of things are experiments. You may put files in the wrong directory, or get spelling wrong. Think of re-doing this effort a few times not as waste but as practice.

Add Junit5 And Your First Test

Right-click on the project, Build Project, Add Libraries, add Junit5. From there you can right click on the project, select New, then new Junit test. Give it a good name and select Jupiter Junit (Junit5) test. 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!

You’ll notice the test case needs to set the property to where the chromedriver file is located; you may need to change this. In windows this will look something like “c:\somedirectory\chromedriver.exe”.

package org.seleniumhq.selenium.selenium_java;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

class SauceLabsHomePageTesst {
          @Test
          public void site_header_is_on_home_page() {

                   System.setProperty("webdriver.chrome.driver", "/opt/local/bin/chromedriver");
                   WebDriver browser = new ChromeDriver();
                   browser.get("https://www.saucelabs.com");
               WebElement href = browser.findElement(By.xpath("//a[@href='/beta/login']"));
                   assertTrue((href.isDisplayed()));

                  browser.close();       

}

}

Run the test by right-clicking in the test body and selecting Run As => Junit test.

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.

It may take a few attempts for you to follow the instructions. Don’t get frustrated. Become an expert in creating a junit5 test in eclipse. If you need a reference, compare it with our project in github. Just don’t forget to change the System.setProperty for webdriver in SauceLabsHomePageTest.java.

Written by

Matthew Heusser

Topics

Programming languagesGet Started/GuideSeleniumFrameworksOpen sourceCross browser