GeckoDriver Selenium Tutorial: How to Use GeckoDriver in Selenium Projects

GeckoDriver Selenium Tutorial: Learn How to Use Gecko (Marionette) Driver in Selenium

In order to understand what GeckoDriver is, initially we need to know about Gecko and Web browser engine. This tutorial covers almost all the features involved with GeckoDriver, thereby giving you a complete overview of it.

So to start with, let us first know what Gecko is and what is a Web Browser Engine?

GeckoDriver Selenium Tutorial

What is Gecko?

Gecko is a web browser engine. There are several applications that require Gecko. Specifically, the applications which are developed by Mozilla Foundation and the Mozilla Corporation. Gecko is also a need for many open source software projects. Gecko is written in C++ and JavaScript.

Latest versions are written in Rust also. Gecko is a free and an open source web browser engine.

What is a Web Browser Engine?

Web Browser Engine is nothing but a software program. The main function of this program is collecting the content (like HTML, XML, images) & formatting the information (like CSS) and display this formatted content on the screen. Web Browser Engine is also called as Layout Engine or Rendering Engine.

Applications like Web browsers, email clients, e-book readers, on-line help systems etc. need displaying of web content. And to display the web content, the web browser engine is required and it is a part of all these applications. There are different web browser engines for each web browsers.

The following table shows web browsers and what web browser engines, they are using.

Table of Browsers and Web browser engines

Gecko runs on the following operating system without emulation:

  • Windows
  • Mac OS
  • Linux
  • BSD
  • Unix

It cannot run on a Symbian OS.

What is GeckoDriver?

GeckoDriver is a connecting link to the Firefox browser for your scripts in Selenium. GeckoDriver is a proxy which helps to communicate with the Gecko-based browsers (e.g. Firefox), for which it provides HTTP API.

Why does Selenium need GeckoDriver?

Firefox (version 47 and above) has done some changes to it and for some security reasons, it doesn’t allow any third party driver to directly interact with the browsers. Hence we cannot use Selenium2 with the latest versions of Firefox. So we need Selenium3.

Selenium3 has Marionette Driver. Selenium3 can directly interact with the Firefox browser using a proxy, which is nothing but the GeckoDriver.

How to use GeckoDriver in Selenium Project?

  • Let us consider that you have the latest version of Selenium WebDriver and the Firefox browser.
  • Then download the GeckoDriver from here. Later, choose the version which is suitable for your computer.

Gecko Download

  • Extract files from the compressed folder

Gecko Extract All

  • Add the references of Selenium3 libs in your project through- Right-click on the project => Build Path => Configure Build Path => Libraries => Add External Jars.

Add External Jars

  • Select Lib folder => Click Clt + A => Click Open.

Select Library

  • After you click open, you will see the following window:

Gecko REsult

  • Then click OK.
  • Now let us write our code and use the system property to specify the GeckoDriver Path.
  • Add the below line in your code:
System.setProperty(“webdriver.gecko.driver”,”Path of the GeckoDriver file”).

** [ How to copy the address of the extracted file. – (Press ’Shift’ from the keyboard and right- click the file, you will get an option. Then ‘Copy address of the file’.)]

** [In this copy pasted path, make sure that there is a double backslash otherwise code will have a syntax error.]

Let us take an example

Example

Here is just a simple script, where we open the Google web page in a Firefox browser and verify the title of the web page.

Code1:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
publicclass First_Class {

publicstaticvoid main(String[] args) {

System.setProperty("webdriver.gecko.driver","E:\\GekoDriver\\geckodriver-v0.15.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
String appTitle=driver.getTitle();
String expTitle="Google";
if (appTitle.equals (expTitle)){
System.out.println("Verification Successfull");
}
else{
System.out.println("Verification Failed");

}
driver.close();
System.exit(0);
}
}

Understanding the Code

#1) import org.openqa.selenium.WebDriver- Here we are importing all the references to WebDriver interface. Later on, this WebDriver interface is required to instantiate a new browser.

#2) import org.openqa.selenium.firefox.FirefoxDriver- Here we are importing all the references to FirefoxDriver class.

#3) setProperty(String key, String value)- Here we are setting the system property by providing the name of the property which is called as Key and its path which is called as Value.

Key-Name of the system property i.e. webdriver.gecko.driver.

Value– Address of Gecko Driver’s exe file.

#4) WebDriver driver=new FirefoxDriver() – In this line of code we are creating the reference variable ‘driver’ of the WebDriver and this reference variable is initiated using FirefoxDriver class. A Firefox profile without extensions and plugins will be launched with Firefox instance.

#5) get(“URL”)- Using this Get method we can open the specified URL in the browser. This Get method is called using the WebDriver’s reference variable i.e. driver. The string is passed to Get method, which means our application URL is passed into this Get method.

#6) manage().window().maximize()- Using this line of code we are maximizing the browser window. As soon as the browser opens the specified URL, it is maximized using this line.

#7) getTitle()– Using this line of code, we will be able to find the title of the web page. This method is also called using the WebDriver’s reference variable ‘driver’. We are saving this title in String variable ‘appTitle’.

#8) Comparison– Here we are comparing the appTitle (which will get through driver.getTitle() method) and the expTitle (which is “Google”) using the If statement. It is just a simple If-else statement. When the “If” condition is satisfied, we are printing the message “Verification Successful” otherwise we are the printing message “Verification Failed”.

if 
(appTitle.equals (expTitle))
{
System.out.println ("Verification Successful");
}
else
{
System.out.println("Verification Failed");
}

#9) driver.close()– This line of code closes the browser. This line closes only the current window.

#10) System.exit(0)– This line of code method is used to terminate running Java Virtual Machine. So it is recommended to close all the open windows or files before this line.

GeckoDriver and TestNG

There is not a much difference in the code, but here I am adding a code just for your reference.

EXAMPLE:

Let’s move to the example. Our example is to open the Google.com web page, get its title and print it.

Code2:

import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

publicclass TstNG {

@Test
publicvoid f() {
System.setProperty("webdriver.gecko.driver","E:\\GekoDriver\\geckodriver-v0.15.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
String appurl=driver.getTitle();
System.out.println(appurl);
driver.close();
// System.exit(0);
}
}

Points to remember while writing TestNG code:

#1) Use the System.setProperty(String key, String value) method inside the function f() same as the previous example. In that example, we wrote it in the main function. However, in TestNG, there are no main () functions. If you write it outside the function you will get a syntax error.

#2) The second most important thing to remember is System.exit(0). There is no need to add this line of code in your TestNG script. There is one reason for that which is – after running the TestNG script, output folder is generated where you can view the generated reports and result, if you add System.exit(0) in your script this folder(output folder) will not get generated and you will not be able to view the reports.

Steps to Add a Path in System’s PATH Environmental Variable

  • On Windows system right click on My Computer or This PC.
  • Select Properties.
  • Select advanced system settings.
  • Click on the Environment Variables button.
  • From System Variables select PATH.
  • Click on Edit button.
  • Click New button
  • Paste the path of GeckoDriver file.
  • Click OK.

Issues without Gecko Driver

 You might face some issues like the ones given below.

#1) If you are using an old version of Firefox and Selenium3, then you will get the following exception:

Exception in thread “main” java.lang.IllegalStateException

#2) If you are using the latest version of Firefox and an old version of Selenium, then you will get the following exception:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000ms

#3) If you are using the latest version of Firefox and WebDriver, but not using GeckoDriver, you will get the following exception:

Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see here. The latest version can be downloaded from here.

Additional Information about GeckoDriver

As we know GeckoDriver is a proxy which helps to communicate with Gecko-based browsers (e.g. Firefox), for which it provides HTTP API.

This HTTP API can be understood using the WebDriver protocol. There are some nodes in the WebDriver protocol which include Local end, Remote end, Intermediary node and Endpoint node. Communication between these nodes is described in the WebDriver protocol.

The Local end is the client side of the WebDriver protocol. Remote end means server side of the WebDriver protocol. The Intermediary node performs the role of a proxy. Endpoint node is put into effect by a user agent or a similar program.

Gecko Flowchart

Commands and responses sent by the WebDriver to GeckoDriver are translated to Marionette Protocol and then transferred to Marionette Driver by GeckoDriver. So we conclude saying that GeckoDriver is acting as a proxy between these two WebDriver and Marionette.

Marionette is divided into 2 parts, which is the server part and the client part. Commands which are sent by the client part are executed by the server part.

This command execution work is performed inside the browser. Marionette is nothing but a combination of a gecko component (which is a Marionette server) and an outside component (which is called as Marionette Client). GeckoDriver is written in Rust programming language.

Conclusion

GeckoDriver is an intermediate factor between your Selenium scripts and Gecko-based browsers like Firefox.

GeckoDriver is a proxy to communicate with Gecko-based browsers (E.g. Firefox). Firefox (version47 and above) has done some changes, which has led to prevention of supporting third-party drivers to interact directly with the browsers.

This is the primary reason for which we need to use the GeckoDriver. The easiest way to use GeckoDriver in your script is to implement the use of System.set property. [System.setProperty(“webdriver.gecko.driver”, ”Path of the Gecko Driver file”)].

Are you new to GeckoDriver? Did you learn something new today in this GeckoDriver Selenium? Or do you have something interesting to share with us about GeckoDriver? Feel free to express your thoughts in the comments section below.

3 thoughts on “GeckoDriver Selenium Tutorial: How to Use GeckoDriver in Selenium Projects

Leave a Comment