Running Test on Selenium Firefox Driver

Filed Under: Selenium

Gecko Marionette Firefox Driver with Selenium 3.0

Many of you know that before Selenium 3, Mozilla Firefox browser was the default browser for Selenium where we launch Firefox by default without any property settings. After Selenium 3.0 has been released in the market and in this version of Selenium, testers need to initialize the script to use Firefox using GeckoDriver explicitly.

What is Gecko?

Gecko is a web browser engine which is developed by Mozilla Foundation and written in C++. It is an Open Source. Web browser engine is a software program that is used to control and render the content (like HTML, CSS, XML, images) on the browser.

What is Gecko Driver?

Gecko is a Web Browser engine used in various applications developed by Mozilla Foundation and the Mozilla Corporation. It is a proxy which is used to interact with the browsers that run on Gecko-based browsers like Firefox. Gecko driver is the link between your tests in Selenium WebDriver and Mozilla Firefox browser. As Selenium 3.0 will not have any native implementation of Firefox, we need to direct all the driver commands through Gecko Driver. Gecko Driver is an executable file that you need to have in one of the system paths before running your tests.
The Firefox browser implements the WebDriver protocol using an executable called GeckoDriver.exe. Selenium uses WebDriver protocol to send requests to GeckoDriver, which translates calls into the Marionette automation protocol and Firefox will understand the commands transmitted in the form of Marionette protocol and executes them.

GeckoDriver

GeckoDriver

Selenium 3 Test without GeckoDriver.exe

Let’s see what happens, if we run the Selenium test without GeckoDriver. It was a very straight forward process where you were not required to use GeckoDriver. You just write the code to instantiate the WebDriver and open Firefox browser. Below is the sample program to launch a Firefox browser with Selenium 2.0


package com.journaldev.selenium.Firefox;

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

public class GeckoDriver {

    public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://journaldev.com");
    String PageTitle = driver.getTitle();
    System.out.println("Page Title is:" + PageTitle);
    }
 }

When you run above program we get an exception called java.lang.IllegalStateException. which tells The path to the driver executable must be set by webdriver.gecko.driver.

Exception

Exception

To overcome the above problem we need to download the GeckoDriver in order to work with selenium commands which we are writing on Mozilla. Every browser as a driver. The driver for Mozilla is the GeckoDriver. The selenium commands will be interpreted by GeckoDriver and it will be executed on Mozilla.

Download and Install Gecko Driver

Gecko Driver is available as an executable file that starts as a server on your system. The following are the steps to download Gecko Driver.

  • Step 1: Go to the Eclipse official website and select appropriate version for GeckoDriver based on your operating system
  • Note: Here we are working on Windows Operating system, we need to download the corresponding Gecko driver of Windows version. If your Operating System is Linux or Mac then you need to download the corresponding Gecko driver.

    GeckoDriver Version

    GeckoDriver Version

  • Step 2: Once the ZIP file download is complete, extract the ZIP file
  • GeckoDriver Extraction

    GeckoDriver Extraction

  • Step 3: Note the location where you extracted the geckodriver. Location will be later used to instantiate the driver
  • GeckoDriver Location

    GeckoDriver Location

    Different ways to initialize GeckoDriver:

    There are 2 methods to initialize GeckoDriver.

    Set System Properties for Gecko Driver:-

    Code to set the System properties is

    System.setProperty(“webdriver.gecko.driver”,“Path to geckodriver.exe”);

    The complete program to launch the GeckoDriver will be like this:

    
    package com.journaldev.selenium.Firefox;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class GeckoDriver {
    
        public static void main(String[] args) {
        System.setproperty("Webdriver.gecko.driver","D:\\Drivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://journaldev.com");
        String PageTitle = driver.getTitle();
        System.out.println("Page Title is:" + PageTitle);
        driver.close();
        }
     }
    

    When you run the above program you will notice that Journaldev.com is opened in the new Firefox window and it will print the website title in the console.

    Setting Gecko Driver Path in Windows Environment Variables:-

  • Step 1: Go to My Computer and Right click to get the context menu.
  • MyComputer Properties

    MyComputer Properties

  • Step 2: Click on the Change Setting on the opened window.
  • Change Settings

    Change Settings

  • Step 3: Click on Advance tab and click on Environment Variables.
  • Environment Variables

    Environment Variables

  • Step 4: Select Path under System Variables and click on Edit.
  • Path

    Path

  • Step 5: At the end of the string use semicolon and paste the path of the GeckoDriver. On my machine my FirefoxDriver exe resides in D:\Drivers\
  • Variable Value

    Variable Value

    Note: Once the path is set you would not need to set the System property every time in the script. Your script will work without the System Property code.

    The complete program to launch the GeckoDriver will be like this:

    
    package com.journaldev.selenium.Firefox;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class GeckoDriver {
    
        public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://journaldev.com");
        String PageTitle = driver.getTitle();
        System.out.println("Page Title is:" + PageTitle);
        driver.close();
        }
     }
    

    Conclusion

    GeckoDriver will act as an intermediate factor between your Selenium scripts and Gecko-based browsers like Firefox. Firefox has done some changes, which has led to prevention of supporting third-party drivers to interact directly with the browsers.
    This is the main reason for which we need to use the GeckoDriver. The easiest way to use GeckoDriver in your script is to use the System.set property. [System.setProperty(“webdriver.gecko.driver”, ”Path of the Gecko Driver file”)].

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    close
    Generic selectors
    Exact matches only
    Search in title
    Search in content
    Search in posts
    Search in pages