Table of contents
1.
Introduction
2.
Selenium FirefoxDriver
2.1.
Configuration
2.2.
Implementation
2.2.1.
Program
2.2.2.
Output
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Running Selenium Tests on Firefox

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Mozilla Firefox is among the browsers with the most extensive user base globally. With its enhanced features and multitude of the latest testing tools and techniques support, web testing on Firefox has become vital for developers and QAs. Selenium is one of the tools we can use to perform tests on Firefox. Selenium uses FirefoxDriver, also known as GeckoDriver, to link the test cases with the Firefox browser.

We will understand how we can run our test cases on Mozilla Firefox using Selenium GeckoDriver and Java programming through this article.

Selenium FirefoxDriver

Selenium FirefoxDriver or the GeckoDriver is the browser engine developed by Mozilla, providing a link between your Selenium test cases and the Firefox browser. You cannot instantiate the Firefox browser object and perform automated Selenium testing without the help of GeckoDriver. It acts as an agent between W3C WebDriver-compatible clients like Eclipse to interact with Gecko-based browsers, i.e., Mozilla Firefox.

Configuration

Before you can start testing on Firefox, you will need to download a suitable version of GeckoDriver on your system following these steps. 

  1. You can find the GeckoDriver on the official Selenium website. You will need to scroll down, select Browsers from the Platforms Supported by Selenium heading, and open the documentation for Firefox as shown below.

     
  2. Now you will find a table having information about the browser and Selenium version compatibility with each geckodriver version. You need to click on the geckodriver releases link above the table.

     
  3. Now on the Github page, you can select the suitable version of the driver and scroll down to find download links for the same under the Assets heading. 

     

Now that you will have the executable file of the geckodriver, You will also need to add the executable file to your path for it to run correctly. You can check out how to add an executable to the path for Windows and Mac or Linux by following the configuration steps from our article on Running Selenium Tests on Google Chrome(<--link to be added–>).

Implementation

Now that you have configured GeckoDriver for your system, you can write a test case and run it using Mozilla Firefox.

We will use the Eclipse IDE to run our Java Selenium test case on Firefox using the GeckoDriver. Our test case should open Firefox, go to www.google.com, and enter Coding Ninjas in the search box.

Firstly you will need to add Selenium as a dependency to your project. You can do this by adding the .jar file to your project directory or adding Selenium as a dependency on your Maven or Gradle build files. The latter is the recommended approach.

pom.xml(Maven)

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.3</version>
</dependency>

 

build.gradle(Gradle)

dependencies{
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version: 4.1.3
}

 

Now we can write the code for our test case. In the code, we will initialize the object of FirefoxDriver using this command.

WebDriver driver = new FirefoxDriver();
You can also try this code with Online Java Compiler
Run Code

Program

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxTest{
   public static void main(String[] args) {
       System.setProperty("webdriver.gecko.driver","/usr/local/bin/geckodriver"); // Setting system properties of FirefoxDriver
WebDriver driver = new FirefoxDriver();       driver.manage().window().maximize();       driver.manage().deleteAllCookies();       driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);       driver.get("https://www.google.com/");


//name locator for text box         driver.findElement(By.name("q")).sendKeys("Coding Ninjas"); 
//Clicks on the google search button     driver.findElement(By.name("btnK")).sendKeys(Keys.ENTER);
   }
}
You can also try this code with Online Java Compiler
Run Code

 

On running the above code as a Java Application using Eclipse or IntelliJ, a new Mozilla Firefox window should open automatically where Coding Ninjas should be searched on Google Search.

Output

FAQs

1. Why do we use GeckoDriver?

Ans: Mozilla Firefox came out with Marionette after version 47. Marionette is an automation driver that remotely controls either the UI or the internal JavaScript of a Gecko platform like Firefox. Hence, we need GeckoDriver to work with Marionette for Firefox.

 

2. What is the difference between Marionette and GeckoDriver?

Ans: Marionette and Geckodriver form part of the same architecture we need to run automated tests on the Firefox browser. While running computerized tests on Firefox, the browser implements WebDriver protocol using GeckoDriver, which starts a server communicating all tests. This server translates calls in the Marionette automation protocol. Here Marionette acts as a proxy between local and remote ends.

 

3. What are trace logs in GeckoDriver?

Ans: The geckodriver provides multiple bands of logs for different users and clients. Essential log entries are shown to everyone by default, including the port on which geckodriver provides the WebDriver API, informative warnings, errors, and fatal exceptions.

 

4. What is setProperty Selenium?

Ans: We use the setProperty method to configure the browser driver path. Selenium's client library communicates with the driver via the JSON Wire Protocol. The browser driver acts as a link between the Selenium implementation code and the web browser.

Key Takeaways

This article extensively discussed running Selenium tests on Mozilla Firefox using FirefoxDriver and its implementation in Java using the Eclipse IDE. There is much more potential for learning and testing with the Firefox driver in Selenium. The relevance and features of Mozilla Firefox make FirefoxDriver or GeckoDriver a vital tool for testing web applications.

We hope that this blog has helped you enhance your knowledge regarding Selenium. If you would like to learn more, check out our articles on Selenium Interview QuestionsMethods in Selenium, and Basics of Java. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass