How to Set Up Selenium WebDriver in Eclipse with External JAR Files or Maven

Andrei Dobra
5 min readSep 4, 2017

--

Start your journey with up to date procedures

Automating testing procedures and quality assurance processes is already essential in today’s IT landscape. Unfortunately, delving into one of the most popular frameworks — Selenium — can be confusing and convoluted.

That is why I wrote this article — to show how, in 2017, you can delve into Selenium and get it up and running on your machine through the Eclipse IDE.

I’ve spent quite a few hours, gone through hundreds of browser tabs and many experiments with Eclipse to finally get everything set up and functional. As such, with this article, you can avoid this altogether and hit the ground running.

Two methods are described below. The easy one, involving external .jar files, and the more complex and relatively confusing one involving Maven.

Prerequisites

First up, before going into the actual details, there are a few prerequisites that you have to fulfill.

Install the latest version of the Java Development Kit. Link.

Add %JAVA_HOME% to your PATH. Google link.

Install the Eclipse IDE for Java EE Developers. Link.

Download the GeckoDriver that is used to run your tests in Mozilla Firefox. Link.

Unzip the archive in a folder called geckodriver and then add the folder path to the system PATH.

More downloads and operations are needed for each of the two ways, so read on for more work to do.

The easy .jar way

This method is somewhat easy because you just get some .jar files, point Eclipse to them, and you are good to go. As opposed to the Maven workflow, this is a slightly old-fashioned method, because you need to manually update these dependencies.

Follow these steps to get up and running:

Download Selenium Client & WebDriver Language Bindings for Java from the official page. Link.

I made a folder with selenium dependencies and unzipped the archive in that folder.

Launch Eclipse.

It will prompt you to create a Workspace that will hold relevant project information.

Once it’s loaded up, close the Welcome screen.

Create a new project by going to File > New > Project. In the New Project window, select Java Project.

Enter a Project Name and click Finish.

You may get a message asking if you want to open the associated perspective. This doesn’t really matter, so click Yes.

Once you do these steps, the new project should appear in the Eclipse Project Explorer.

Right click on the project name and select Properties. In the Properties window, select Java Build Path and navigate to the Libraries tab.

Click Add External JARs and browse to the location of the unzipped Selenium Java folder that you downloaded from the official website. Select the main .jar file. Mine had the client-combined-3.5.3-nodeps.jar name.

Once again, click Add External JARs and browse to the same location, only this time open the lib folder. Here, select all the files and add them as well.

Back in the Properties window, click OK. You should now see in the Package Explorer an entry for Referenced Libraries. Expand that and confirm that the external JARs are present.

You can now go to the Test your work section of the article to see if everything works as intended and that you can run tests using Selenium. Alternatively, you can also try making a Maven Selenium project. You can have both in a single workspace as there is no overlap, or not one that I am aware of.

The slightly more complex Maven way

Maven, made by Apache, is a software project management and comprehension tool. Basically, it can include or exclude dependencies and help the build process become more efficient.

In theory, this sounds great, but the instructions from the Selenium documentation are confusing and ultimately result in a project that doesn’t actually function. After a bit of experimentation, I’ve found a working procedure, which you can find below.

First up, download Maven. Link.

Unzip the folder and then add the path of the bin folder to the system path.

It will prompt you to create a Workspace that will hold relevant project information.

Once it’s loaded up, close the Welcome screen.

Create a new project by going to File > New > Project. Select Maven > Maven Project.

In the New Maven Project window, click Next. The Quickstart archetype should be selected by default. Click Next.

Give it a Group ID, Artifact ID, and leave everything as they are. Click Finish.

You should now see the new project in the Eclipse Package Explorer pane.

Expand the project and you should see a pom.xml file. Double-click it to open the file in the main Eclipse window.

By default, it opens the pom.xml overview. Below the file, select the pom.xml tab. This will show you the actual text content found in the file. Again, by default, it should include some details you already set in the project, like Group ID and so on. It should also include a dependencies section that includes just one entry, in the form of JUnit.

Above the dependency entry for JUnit, add the dependency for Selenium. Save the file. It should look like the following example:

Right click on pom.xml and select Run As > Maven test. If everything went according to plan, should see a Build Success message in the Console.

Note: Initially, this step failed because Eclipse had its Installed JRE pointed to the JRE (Java Runtime Environment) folder, instead of the JDK (Java Development Kit) folder. You can fix this by going to Window > Preferences > Java > Installed JREs. Select the listing for JRE, click Edit, and then Directory. Navigate to the JDK folder, the same one set in the system path and %JAVA_HOME%, and then hit OK. Apply this change and then reattempt the Run as Maven test operation.

If the test is successful, read on to see how you can actually try out Selenium.

Test your work

You’ve set up your development environment in one of the two aforementioned ways. Now, it’s time to actually test it out and see if you can use Selenium.

Right click on the project name and select New > Package. Give it a name and click Finish.

Right click the package name and select New > Class. Give it a name. Select the public static void main(String[] args) checkbox. Click Finish.

You should now see the class opened with a few predetermined lines of code.

To test out everything, we can use the standard example from the Selenium Documentation, which opens a browser, navigates to Google, searches for Cheese! and then returns the title of search results page.

The Java code for it is available below. Copy over to your class the relevant lines.

Once the code looks correct, go to Run > Run.

If you did everything right, a new Firefox window should appear, it should go to Google, search for Cheese! and the close all on its own.

Congratulations, you’ve just automated a test case in which a user searches for Cheese!

Conclusion

Selenium, like many frameworks, can be quite powerful. Unfortunately, as is often the case, the documentation and first examples aren’t up to date or have confusing steps, especially if you are a beginner.

I hope this article shed some light. Look forward to more as I continue working with it. If you encounter any other issues, leave a comment and we can put our minds together to find solutions.

--

--