Set up Gecko Driver on Ubuntu

Selenium WebDriver can be used to automate routine tasks and to perform regression tests. Currently I build automations using Selenium WebDriver and Sikuli to automate routine daily tasks. Since I maintain multiple websites, I figured that I could use this same technology to perform the code updates on the websites that I manage. Selenium WebDriver has some first time setup steps that have to be done in order for you to be able to build the automations. My browser of choice is Firefox. Some people reading this will not be thrilled about that, but everyone has their preferences. If you like to use Chrome, or worse Internet Explorer, then the steps mentioned below are similar to those for Firefox. However this post focuses on setting up with Firefox. First thing that you will need to do is to have Eclipse or similar IDE installed on your computer. That will not be covered in this post.

Next, download the Gecko Driver. That can be downloaded from https://github.com/mozilla/geckodriver. The Gecko Driver serves as a middle man between WebDriver and the web browser. The web browser cannot directly communicate or interact with the web browser. What happens is WebDriver interacts with the Gecko Driver. Then the Gecko Driver translates those interactions into actions that the browser can perform. Once the action is performed, then response is sent back from the browser to the Gecko Driver to WebDriver. Next, create a Maven project. In the pom.xml file, you will need to add the following dependency to the file:

<dependency>
 <groupId>org.seleniumhq.selenium</groupId>
 <artifactId>selenium-java</artifactId>
 <version>3.14.0</version>
</dependency>

At the time of this writing, 3.14.0 version, was the second latest versoin that was available. I would recommend using the latest or later version that is available. Next, add the Java code below to your main function. This is the simpliest code to get an automation running with Firefox.

System.setProperty("webdriver.gecko.driver", "/home/almostengineer/java-workspace/geckodriver"); // define geckodriver location
WebDriver driver = new FirefoxDriver(); // open browser
driver.get("http://google.com"); // open the website of choice
Thread.sleep(5000); // allow results to be seen
driver.quit(); // close browser ```

In the above, /home/almostengineer/java-workspace/geckodriver will need to be replaced with the location that you have downloaded the Gecko Driver. If you choose, you may also change the website that the browser opens. Now you may run the application that you have built. If done correctly, you will see the browser open, then the Google home page appear, then the browser close. There may be some red text in the Java console, but there is no need to be alarmed by this unless there is an exception mentioned. The code used here can be downloaded from my Github repository at https://github.com/almostengr/seleniumtutorial

The entire file Java file would look like the following:

package com.thealmostengineer.seleniumtutorial;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Simple webdriver
 *
 */
public class App 
{
    static void geckoDriverTest() throws Exception {
    System.setProperty("webdriver.gecko.driver", "/home/almostengineer/java-workspace/geckodriver");
        WebDriver driver = new FirefoxDriver(); // open browser
        driver.manage().window().maximize(); // maximize the browser window
        driver.get("http://google.com");
        Thread.sleep(5000); // allow results to be seen
        driver.quit(); // close browser
    }

    public static void main( String[] args )
    {
        try {
        geckoDriverTest();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}
Published: 2019-04-05
Updated: 2020-07-15
Category: Technology
Read Time: 3 minute(s)