Geckodriver Tutorial: Automation Testing on Firefox with Selenium

Himanshu Sheth

Posted On: July 15, 2020

view count211580 Views

Read time10 Min Read

With 4.25% of browser market share worldwide in June 2020 as per statcounter, Mozilla Firefox browsers are considered inevitable for every Selenium testing checklist. Mozilla developers introduced Geckodriver, also known as the Selenium FirefoxDriver to help testers to automate browser test on Firefox browsers.

In this article, we will look into the basics of Selenium FirefoxDriver, how to download and set it up in your system. We will then run an automation script using Selenium FirefoxDriver to test on Mozilla Firefox browsers.

If you’re looking to improve your Selenium interview skills, check out our curated list of Selenium interview questions and answers.

What Is a Selenium FirefoxDriver?

Selenium Firefox Driver, also known as GeckoDriver, is an indispensable tool developed by Mozilla, designed to seamlessly integrate automated testing with the Firefox browser. Its pivotal role lies in bridging the gap between test cases and browser execution, making it an essential component for automated Selenium testing.

Every browser has a unique engine responsible for rendering the UI of your website. Gecko has been the browser engine responsible for displaying web elements over the Mozilla Firefox browsers.

Then GeckoDriver used to perform relevant actions in the browser instance based on the instructions and sends the response in HTTP protocol through an HTTP server. Here is an image that explains the architecture of a Selenium WebDriver. You can notice where Selenium FirefoxDriver comes into play.

selenium-webdriver-architecture

Should You Include GeckoDriver In Your Selenium Testing Scripts?

Although not the most popular browser, Mozilla Firefox has been a renowned participant in the browser wars ever since its inception in 2002. Firefox still commands a good amount of browser market share after Chrome. I am sure you might have entertained an argument over Google Chrome vs Mozilla Firefox at some point. In fact, many of my colleagues prefer Mozilla Firefox as their default browser instead of Google Chrome. Let us look at the browser market share from June 2019 to July 2020.

Desktop Browser share 2020

Source: Statscounter

Now, 8.48% looks a lot less when compared to 69.42% market share of Google Chrome but did you notice something?

Mozilla Firefox is the third most popular desktop browser and is having a neck to neck race with Safari.

So, Should You Include FirefoxDriver In Your Selenium Testing Scripts?

Yes, without a doubt. Having a solid 8.48% browser market share, Firefox browsers has become inevitable for cross browser testing. If you won’t include FirefoxDriver in your Selenium testing scripts then you could miss out on a lot of potential leads and promising customers.

With that said, let’s download & set up the Selenium FirefoxDriver in your operating system.

Download & Setup GeckoDriver / FirefoxDriver For Selenium Project

Step 1: Selenium FirefoxDriver or Selenium GeckoDriver can be downloaded from the official GitHub repository of Mozilla. Go to the link and scroll towards the bottom of the page. Open the Assets menu and download the Selenium FirefoxDriver respective to your operating system.

Setup GeckoDriver

Step 2: Extract the downloaded file.

Step 3: Copy the GeckoDriver (geckodriver.exe) to the folder where the Firefox browser is located. By doing so, you can avoid giving the absolute path of the Selenium GeckoDriver if the Selenium FirefoxDriver instance is created in the test code.

Selenium Firefox Driver

Invoking Selenium FirefoxDriver For Selenium Projects

In order to use the Selenium with GeckoDriver or Selenium FirefoxDriver, the corresponding package (or class) has to be included before initializing a new instance of Firefox class. Here are some of the ways in which Selenium with GeckoDriver can be used through popular languages.

Selenium C#

Python

Java

For further demonstration, we will be using the Selenium for GeckoDriver with NUnit framework for running Selenium C# testing. If you are new to Selenium C#, you can refer to our Selenium C# tutorial.

Testing with Selenium

Selenium C# Testing with FirefoxDriver Using NUnit Framework

NUnit is a popular open-source web-testing framework. It is used with Selenium C# for automated browser testing. The extensive feature-set of the NUnit framework makes it a more popular framework when compared to other frameworks like MSTest/Visual Studio, xUnit.NET, etc. These frameworks can also be used with C# and Selenium test suite.

Assertions in NUnit are instrumental in making the code more modular so that less effort is spent on the maintenance of the source code.

We won’t delve into the internals of the NUnit framework since we have already an article covering the NUnit framework with Selenium along with steps for installation on Visual Studio. You should check out the NUnit tutorial for getting started with the NUnit framework.

Here is the basic execution flow of an NUnit test. The necessary steps for Initialization and De-initialization are performed as a part of the [Setup] and [TearDown] annotation.

 NUnit test

With the basic flow in mind. Let’s get our hands dirty with test automation using Selenium and Geckodriver with NUnit. To demonstrate Selenium test automation with NUnit, we have two sample test cases; details of which are mentioned below-

Test Case – 1

  1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/
  2. Select the first two checkboxes
  3. Send ‘Adding item to the list’ to the textbox with id = sampletodotext
  4. Click the Add Button and verify whether the text has been added or not

Test Case – 2

  1. Navigate to the URL https://www.lambdatest.com
  2. Locate the hover element with description as ‘Automated’
  3. Click on the Learn More option to open the target web page
  4. Raise assert if the driver title does not match the expected title

Implementation for testing with Selenium FirefoxDriver & NUnit

Code Walkthrough (Common Step)

Since Firefox browser is used for cross browser testing, we created a Firefox Web Driver instance before any of the tests are executed. IWebDriver (which is a part of OpenQA.Selenium namespace) is used to create the Selenium FirefoxDriver instance.

This initialization is done as a part of the implementation in the [SetUp] annotation. The de-initialization process i.e. freeing Selenium FirefoxDriver instance is done as a part of the implementation in the [TearDown] annotation.

Test Case – 1

Using the Inspect tool of the Firefox browser, we find elements (check-boxes) with name li1, li2

tool of the Firefox browser

Once the check-boxes are located, we locate the text-box where the target text has to be added. We make use of XPath to do the same. Assert with Boolean condition constraint is used to verify the correctness of the test case.

Shown below is the execution snapshot where we can see that the new item has been added to the list-

firefoxdriver

Test Case – 2

To locate the element with display name Automation, we make use of the ExecuteScript method to execute JavaScript in the context of the current window.

Automation-testing

We perform a vertical scroll by 500 pixels since the element being searched can only be located after the scroll.

Wait of 30 seconds is added to ensure that the element is visible. Once the element (name = Automation) is visible, a Hover operation is performed using the MoveToElement() method of the Actions class.

A click on the ‘Learn More’ link for the Automation option (shown in the earlier snapshot) is performed. Once the new page opens, a comparison of the expected window title with the title of the current page is performed using EqualTo constraint. Assert is raised if the titles do not match.

selenium-automation-testing

Kudos! You have executed the test cases using the Selenium FirefoxDriver & NUnit framework on your machine.

“How do you plan to test these scripts over multiple Firefox browser versions?”

You can’t keep installing and uninstalling one browser version after the other in your machine and having different browser versions configured over multiple machines is going to leave you in a spot where you are more likely to spend more time in maintaining different Firefox browser versions rather than testing on them.

A better option for improving the test coverage is to perform Selenium automation testing on a cloud-based cross browser testing like LambdaTest. Using LambdaTest, you can perform automated and live interactive cross browser testing on 3000+ real browsers and operating systems. With LambdaTest, you can test over legacy & latest browser versions of Mozilla Firefox, Google Chrome, Safari, Opera, Edge, and more. Plus you can leverage our online Selenium Grid to help you execute multiple instances of Selenium FirefoxDriver in parallel.

Remote Testing with NUnit & Selenium FirefoxDriver Over LambdaTest

To get started, you need to create an account on LambdaTest. Once the account is created, you should make a note of the user-name & access-key required for accessing the remote Selenium Grid on LambdaTest. This information is available in your Profile section of LambdaTest.

Selenium Grid

You can generate browser and platform capabilities on which testing is to be performed using LambdaTest capabilities generator. Shown below are the desired capabilities for Firefox 62.0 on the Windows 10 operating system:

LambdaTest capabilities generator

For demonstration of Selenium test automation (using NUnit framework) on the remote Selenium GeckoDriver/ FirefoxDriver, we port the previous example implementation to make it work with LambdaTest infrastructure.

Below are the overall requirements for automated cross browser testing:

  • Browser – Firefox
  • Browser version – 62.0
  • Operating System – Windows 10
  • Resolution – 1024 * 768

Implementation

Code Walkthrough

In the current test case, we import the OpenQA.Selenium.Remote package/namespace where RemoteWebDriver class is defined.

Username and access key combination is passed to the remote Selenium Grid on which Selenium automation testing is performed.

The browser & platform capabilities that are generated using the LambdaTest capabilities generator passed as an argument to the Remote Selenium FirefoxDriver interface.

The rest of the implementation which contains the business logic of the test cases remains unchanged. The effort spent to port the existing code implementation from your Local Selenium FirefoxDriver to LambdaTest’s Remote Selenium FirefoxDriver is minimal.

After you execute the test scripts using Selenium, Geckodriver & LambdaTest, you can visit Automation Logs to check the status of the test.

 Selenium FirefoxDriver

As seen in the above snapshot, the test status is Completed i.e. test has passed. You can even find the execution video that was recorded in real-time when the Selenium automation testing was performed.

awesome

Conclusion

Mozilla Firefox holds a dominant position in the browser wars. So developers need to keep cross browser development practices in mind. And testers need to incorporate Selenium Geckodriver or Selenium FirefoxDriver in their Selenium testing suites.

Maintaining an in-house Selenium Grid infrastructure can come back to haunt you later down the road. It would be best to port your existing Selenium test suites to an online Selenium Grid. Your existing Selenium testing setup can be easily ported to LambdaTest’s cloud Selenium Grid. with minimal changes in the implementation. This enables developers to test their code against not just Firefox versions but many more major browser vendors such as Google Chrome, Safari, Edge, Opera, Yandex, and even the notorious IE. Use LambdaTest Selenium rid now for free and improve the overall test coverage – an important KPI for every business running online. Happy Testing ☺

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 128



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free