Getting Started with Webdriver - Selenium in Java Eclipse

September 4, 2019
6 mins
Share this post

Getting started with webdriver's java binding is very easy- all you need to know how to put different pieces together, just like a puzzle.

Component’s you will need:

  1. Eclipse.
  2. A Test Framework- We will be using JUnit 5.
  3. Wedriver’s Java Code Library.
  4. A Browser Driver- We will use Chrome Driver. You can use Safari Driver, Firefox Driver.
  5. A Dependency Manager- We will use Maven. Common Dependency Managers are Maven and Graddle. You can use either any of these two or you can use any other dependency manager.

Frustrated with Frequent App Performance Issues?

Upgrade to seamless speed & reliability with our testing.

Getting Eclipse

Eclipse is an open-source project maintained by Eclipse foundation and one of the most popular IDE for Java. It has a large marketplace for add-ons, tools and support.
You can download the latest version of Eclipse here.

Eclipse IDE Download

The downloader will save the executable file (.exe) to the browser's “download” directory or to the local drive on your computer. All you need to do is to Download and run it. You will see a list of options.

Eclipse Installer

For this example I have selected Eclipse IDE for Java.

Creating The Project

Eclipse IDE works on the principle of folders and spaces. Different companies and Different teams follow different approaches for workspaces and project structures. Use whatever you are more comfortable with.

In this example we will create a new Java Project by simply clicking on “Create New Java Project” from the opening screen.

Eclipse IDE for Java

Complete the basic information in the form and then click finish to proceed.
NOTE: Please make sure that Java version is 1.8 or higher.

Is Your App Crashing More Than It’s Running?

Boost stability and user satisfaction with targeted testing.

Add Dependency Manager- MAVEN

Maven is a dependency registration and management service that frees you of the task to download and install all dependencies by hand.

Right Click on the Project→Configure→Convert to Maven Project.

Maven Eclipse IDE

Add Dependencies

In the bottom left of the package explorer you will see the pom.xml file.
We will add reference to the JUnit Jupiter and Webdriver into the POM, by adding dependencies xml fragment.
Dependencies are added below build and before </project>.

Here is the snippet that you need to add to the pom.xml file.

 

JUnit Jupiter

Adding Webdriver to Eclipse

Before delving into the details on how to add a webdriver in an eclipse project. We first need to see the list of web drivers that are available.

The Browser Driver

It is the webdriver’s lowest component responsible for manipulating browser directly through it’s automation interface. This is the actual program that libraries will call. There a variety of browser driver’s that are available- ChromeDriver for Google Chrome, Geckodriver for Firefox, Internet Explorer Driver for Internet Explorer and Safari Driver for Safari.
Place the browser driver directly in the directory that is part of your System Environment Path variable or you can place it anywhere and mention the exact path to the browser driver in the test case inside the System.setProperty() method.

We will pick webdriver automatically by adding it to the pom.xml file.

Language Bindings

Language bindings are also known as client drivers. These bindings enable you to write test scripts in any language that you are comfortable with. Official bindings are available for Java, Python, Ruby, C#, JavaScript(Node) and many more.

If you are running your tests on one local machine then you need language binding but if you are running tests on many machines then you need a server component.

Selenium Server is a server component when running tests on many machines. This will act as a central hub in grid configuration.

Writing First test case

We will be using JUnit to write test case because of the 2 reasons:

  1. It comes installed with Eclipse
  2. It is very Popular

Right Click on the Eclipse Project-New-JUnit Test Case

JUNit Jupiter
JUnit Jupiter

Give your test case a good name following the naming practice that is followed by your team in your organization.
Now it’s time to do the actual coding and write the test case.

 

Here we are opening the homepage of FrugalTesting and closing the browser instance. NOTE: You can either set the exact path of the webdriver-chromedriver in this case inside the System.setProperty() method parameter list or you can set it as Environment variable so that system will automatically pick this webdriver.

Run the test by Right Clicking in the body of the test case and then selecting Run Configurations options.You will see the JUnit5 Option in the left hand side of the dialog box that appears.

FrugalTesting
JUnit Jupiter

Constantly Facing Software Glitches and Unexpected Downtime?

Discover seamless functionality with our specialized testing services.

Bharti Garg
Author
Our blog

Latest blog posts

Discover the latest in software testing: expert analysis, innovative strategies, and industry forecasts
Testing Tools
Software Testing

16 Best Load Testing Tools Reviewed For 2024

Rupesh Garg
Rupesh Garg
October 8, 2024
5 min read
AI and Testing

Enhancing test efficiency using AI for Performance testing

Rupesh Garg
Rupesh Garg
October 7, 2024
5 min read
Automation Testing
Software Testing

The Role of AI in Improving Automation Testing and QA

Rupesh Garg
Rupesh Garg
October 4, 2024
5 min read