Before Selenium 3, Mozilla Firefox browser was the default browser for Selenium. After Selenium 3, testers need to initialize the script to use Firefox using GeckoDriver explicitly. Selenium uses W3C Webdriver protocol to send requests to GeckoDriver, which translates them into a protocol named Marionette. Firefox will understand the commands transmitted in the form of Marionette protocol and executes them.
How to Download and Install GeckoDriver in Selenium
Gecko Driver is available as an executable file that can be downloaded on the system. The following are the list of steps to download gecko driver. Step 1 ) Select the appropriate version. At this page https://github.com/mozilla/geckodriver/releases ,Select the appropriate version for GeckoDriver download based on your operating system
Step 2) Extract the ZIP file. Once the ZIP file download is complete, extract the contents of ZIP File onto a file folder
Step 3) Note the location. Note the location where you extracted the driver. Location will be used later to instantiate the driver.
Ways to initialize GeckoDriver
There are three different ways to initialize GeckoDriver.
1. Using DesiredCapabilities:
First, set the system property for Gecko Driver. Syntax: Example: Next, set Desired Capabilities. Desired Capabilities help Selenium to understand the browser name, version and operating system to execute the automated tests. Below is the code to set gecko driver using DesiredCapabilities class. Here is the complete code
2. Using marionette property:
Gecko driver can also be initialized using marionette property as below If gecko driver is initialized using the above method, code for desired capabilities is not required.
3. Using FirefoxOptions:
Mozilla Firefox version 47+ has marionette driver as a legacy system. Taking advantage of this, marionette driver can be called using Firefox Options as below
Code for launching firefox using Gecko driver
Code Explanation:
@Before method:
Initially, we need to set the system property for gecko driver to the geckdriver.exe file download location. We need to set the marionette property to true for Selenium to use Marionette protocol to communicate with Gecko Driver. Finally, we need to start the Firefox browser instance using the object for Desired Capabilities. The below statements help to achieve the above task.
@Test method:
We are navigating to user-specified URL using the inbuilt “get” method provided by Selenium web driver. The below statement help to achieve the same.
@After method:
Finally, we are closing the browser instance using the quit method.
Modify a script for non- Gecko to Gecko
Non-gecko driver script used before Selenium 3 was straightforward. We need to create an instance of Firefox driver and use the instance variable. To convert to gecko, you need to simply add one line of code
Common exceptions occurred while using Gecko Driver
Following is a list of common exceptions that occur while using Gecko Driver and with resolution.
The resolution for the above exception is to set the system property for gecko driver with the location of geckodriver.exe file as below Please note that you need to set the property of gecko driver before creating an instance of Mozilla Firefox driver.
Use driver.quit() method to destroy earlier instances of web driverClean the browser cache before executing your automated testsClean the project workspace within Eclipse IDEAlways use the latest version of selenium gecko driver and most recent version of Firefox browser
Advantage of using Gecko Driver
Selenium Webdriver version 2.53 is not compatible with Mozilla Firefox version 47.0+. The Firefox driver used in earlier versions of Mozilla Firefox will be discontinued, and only the GeckoDriver implementation would be used. Hence testers are forced to use GeckoDriver if they want to run automated tests on Mozilla Firefox version 47.0+. But the big question – what is the advantage? The major advantage of using GeckoDriver as opposed to the default Firefox driver is Compatibility. GeckoDriver uses W3C WebDriver protocol to communicate with Selenium. W3C is a universally defined standard for Web Driver. This means Selenium Developers (People who code Selenium base) need not create a new version of Web Driver for each browser version. The same Web Driver can be used for multiple browser versions. Hence, GeckoDriver is preferred compared to the earlier implementation of Firefox driver.