Selenium 3 Project for Firefox using Geckodriver in Java

Presenting today is the step by step tutorial for creating a Selenium 3 project to run UI automation tests in Firefox using Geckodriver. Let’s begin the interactive tutorial with a brief overview of Selenium 3 and the Geckodriver.

Selenium is the most favored UI automation tool for QA engineers. And it is continuously evolving since its inception in 2004. So far, Selenium 2 which introduced WebDriver interface was the most famous version as it added native browser automation support. Now, we have Selenium 3 recently launched with a list of new features. And in this post, we’ll cover how to create a Selenium 3 project for Firefox using Geckodriver in Java.

In Selenium 3, one of the major change is that you can no longer access Firefox directly from the code. Instead, like the Chrome driver, now there is the new Gecko driver which you need to use for Firefox. So, it’s important to learn about the Geckodriver.

Please note if you are preparing for a Selenium testing interview, then you must go through the following 35 Selenium interview questions at least once.

Selenium 3 Project for Firefox using Geckodriver in Java.

Selenium 3 Project for Firefox using Geckodriver

What is Geckodriver and how does it work?

It’s a composite term which combines Gecko and Driver. Gecko is the proprietary web browser engine design and developed by Mozilla.

And Geckodriver is the component that facilitates Selenium 3 tests to run in the Firefox browser. So basically, it replaced the default FF driver implementation in Selenium 3. It aimed to avoid compatibility issues that you might have seen with Selenium 2 and new versions of Firefox. Now Mozilla will also have to update the Geckodriver with every new release of its browser.

Geckodriver in its physical form is an executable program which starts a server to accept commands from Selenium 3 tests. It uses the Marionette automation protocol to communicate with the browser by acting as a proxy. Hence, we may call it as Marionette driver as well.

Note – There is a no. of changes made to the Selenium 3 which you can refer from here.


How to migrate Selenium 2.0 project to Selenium 3.0 or How to use Geckodriver in Selenium project?

First of all, you would need to download the latest version of the Selenium 3.0 package. And then, you will have to replace the Selenium 2.0 references with 3.0 libs under your project in Eclipse.

But there are some more steps you need to do. Because if you try to run the application, then it will give java.lang.IllegalStateException. The description of the error is as follows.

“The path to the driver executable must be set by webdriver.gecko.driver system property”.

So, for successful migration, first of all, you should download the latest version of the Geckodriver.

Click here to download the latest Geckodriver.

Next, you need to provide the Geckodriver path to the application. For this, you can adapt any of the following three approaches.

1. Use system property to specify the Geckodriver path
2. Add a persistent environment variable to set the Geckodriver path
3. Add Geckodriver path via the browser’s desired capabilities


1. Use system property to specify the Geckodriver path.

The new Geckodriver for Selenium 3 accepts the following system property to set from the Java code.

Below here is the full code to launch the Firefox using Geckodriver.

You can execute the above program in Eclipse and check if it is working alright. It’ll start Firefox and open the Google website.


2. Add a persistent environment variable to set the Geckodriver path.

Another simple method that one can use is by appending the path of Geckodriver in the PATH environment variable.

You can quickly do it from the command line using the SETX command in Windows 7.

The above command will update the PATH in the global context. The significance of this method is that you won’t need to set the system property in each Selenium test.

Your code will work without setting up the "webdriver.gecko.driver" Property.


3. Add Geckodriver path via the browser’s desired capabilities.

Alternatively, we can make use of Marionette capability parameter to run our Selenium3 tests. It is very easy to set it from the Java code.

Please check out from the below Java code. With this method, it is also possible to run your tests remotely.


TestNG Example – Launch Firefox using Geckodriver.

If anyone of you likes to use Geckodriver with a TestNG project, then refer the below code.

Also, if you are using Maven tool to build your project, then you’ll need to update POM file in your project.

Please see the sample POM file given below.


Summary – Selenium 3 Project for Firefox using Geckodriver.

Hope you’ve enjoyed the tutorial. If yes, then feel free to share it with your friends.

Since Selenium 3 is a new release, so it’s likely that you may face issues while compiling or running your automation tests. In such cases, please first check for a newer version of both Selenium 3 and Geckodriver.

However, if the problem doesn’t go away, then tell us about the issue and mention the versions (Selenium/Firefox/Geckodriver) used. We’ll do best to help you out.

Keep Learning,