Selenium Tutorials

Running Test on Selenium Firefox Driver

Running Tests on Selenium using GeckoDriver

Gecko Marionette Firefox Driver with Selenium 3.0

Before Selenium 3 was introduced, many of you know that Mozilla Firefox browser was the default browser, for Selenium, where by default Firefox browser would be launched without any property settings. After Selenium 3.0 was introduced and released into the market, testers need to use GeckoDriver explicitly to initialize and to use the Firefox browser. 

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.

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.

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

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

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.”

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

Step 2: Click on Change settings

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

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

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

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.

Facebook Comments
Tags

Related Articles

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button
Close
Close