Running Test on Selenium Firefox Driver1

Running Test on Selenium Firefox Driver

Table of Contents

Running Tests on Selenium using GeckoDriver

Running Tests on Selenium using GeckoDriver enables compatibility with the Firefox browser after Selenium 3. GeckoDriver is required to establish communication between Selenium WebDriver and Firefox, ensuring proper browser automation. It provides better control over browser configurations, improving test execution and reliability in Selenium test suites.

Gecko Marionette Firefox Driver with Selenium 3.0

Running Test on Selenium was simpler before Selenium 3, as Mozilla Firefox was the default browser, launching without property settings. However, after Selenium 3.0 was introduced, testers needed to use GeckoDriver explicitly to initialize and use the Firefox browser for automation testing. This change provided more control over browser configuration, ensuring better compatibility and more reliable test execution when using Firefox.

What is Gecko?

Gecko is a web browser engine used in many applications developed my Mozilla Foundation and the Mozilla Corporation and written in C++ and Java Script. It is an Open Source web browser engine.

What is GeckoDriver?

GeckoDriver is the link between your tests in Selenium and your Mozilla Firefox browser. It acts as a proxy for using W3C WebDriver compatible clients to work together with Gecko-based browsers e.g Firefox.

Running Tests on Selenium using GeckoDriver

GeckoDriver is an executable file, before starting your selenium tests it needs to be present in one of the system paths. The Mozilla Firefox browser implements the WebDriver protocol called GeckoDriver.exe which is an executable file.

Why Selenium needs GeckoDriver?

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

How to use GeckoDriver to launch Firefox?

We follow the below steps to launch Firefox browser using GeckoDriver

  1. Download GeckoDriver and then copy its path
  2. We get geckodriver.exe file, after downloading GeckoDriver zip file
  3. By using below 2 ways, you can use GeckoDriver and launch the Firefox browser.
  1. Using webdriver.gecko.driver System Property
  2. Setting Gecko Driver Path in Windows Environment Variables

Download and Install Gecko Driver

To work with GeckoDriver first, we have to download the latest version of GeckoDriver. To download the latest version of GeckoDriver we need to follow the below steps:

Step1: Go to the official website of gecko driver (https://github.com/mozilla/geckodriver/releases) and download the latest version of GeckoDriver based on your operating system.

Running Test on Selenium Firefox Driver

Note: Based on your Windows Operating System, you need to download the corresponding GeckoDriver. Here we are working on Windows Operating System, you need to download the Windows version gecko driver. If your operating system is Mac or Linux you need to download the corresponding Gecko driver.

Step2: Click on geckodriver-v0.26.0-win64.zip to download GeckoDriver for Windows Operating System.

Running Test on Selenium Firefox Driver

Step3: Extract the ZIP file, once the ZIP file download is complete.

Running Test on Selenium Firefox Driver

Step4: To instantiate the driver, you need to note down the location where you extracted the GeckoDriver.

Running Test on Selenium Firefox Driver

Selenium 3 Test without geckodriver.exe

Below is the sample program to launch a Firefox browser without GeckoDriver. It is a straight forward process to launch Firefox.

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

When you run the above program we get an exception java.lang.IllegalStateException. Which gives “The executable driver must be set by webdriver.gecko.driver.”

Running Test on Selenium Firefox Driver

To overcome the above exception we need to download the GeckoDriver to work with selenium commands.  For every browser have a driver, the driver for Mozilla is GeckoDriver.

 Method 1: Using webdriver.gecko.driver System Property

Set the System Property for geckodriver

Code to set System properties:

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

Below is the complete program to launch the GeckoDriver looks like

public class GeckoDriverTest {

public static void main(String[] args) {

                System.setProperty("webdriver.gecko.driver","D:\\Drivers\\geckodriver.exe");

   WebDriver driver= new FirefoxDriver();

   driver.get("http://www.facebook.com");

                String PageTitle = driver.getTitle();

                System.out.println("Page Title is:" + PageTitle);

                driver.close();

     }

}

When you run the above program it will open facebook.com website in the new Firefox Browser and prints the website title.

Method2: Setting Gecko Driver Path in Windows Environment Variables:-

Step 1: Go to My Computer -> Right click and click on Properties

Running Test on Selenium Firefox Driver

Step 2: Click on Change settings

Running Test on Selenium Firefox Driver

Step 3: Click on the Advanced tab and Click on Environment Variables.

Running Test on Selenium Firefox Driver

Step 4: Select Path under System Variables and Click Edit button.

Running Test on Selenium Firefox Driver

Step 5: Use semicolon at the end of the Variable value and paste the GeckoDriver path. On my computer, my GeckoDriver exe is at D:\Drivers

Running Test on Selenium Firefox Driver

Note: You would not need to set the GeckoDriver System property every time in the script it is a one time job. Now your script will work without giving System Property code.

Below is the program to launch the GeckoDriver

package  com.testing.selenium.Firefox;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriverTest {

    public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();

    driver.get("http://www.facebook.com");

    String PageTitle = driver.getTitle();

    System.out.println("Page Title is:" + PageTitle);

    driver.close();

    }

 }

When you run the above program and your script will run without the System Property code and it will open facebook.com website in the new Firefox Browser and prints the website title.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class