Mozilla Firefox is one of the most widely used browsers in the world. It has enhanced features and is supported by many of the latest testing tools and techniques. One such tool is Selenium which uses Firefox WebDriver to link the test cases with the Firefox browser.
Let’s go through this detailed tutorial with an example of how to run Selenium tests using Firefox WebDriver (also known as the GeckoDriver). Before proceeding, it’s advisable to learn Selenium with Java for automation.
What is a Selenium Firefox Driver?
Selenium Firefox Driver, also called GeckoDriver, is a browser rendering engine developed by Mozilla for many applications. It provides a link between test cases and the Firefox browser. Without the help of GeckoDriver, one cannot instantiate the object of the Firefox browser and perform automated Selenium testing.
One can easily initialize the object of GeckoDriver using the following command:
WebDriver driver = new FirefoxDriver();
Why is GeckoDriver used?
After version 47, Mozilla Firefox came out with Marionette, an automation driver. It remotely controls the UI or the internal JavaScript of a Gecko platform, such as Firefox. Hence, we require GeckoDriver or Firefox WebDriver.
Know More: GeckoDriver vs Marionette
Walkthrough on GeckoDriver Setup
Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation, as shown below.
Step 2: After that, check the latest supported platforms of GeckoDriver versions in the documentation and click on GeckoDriver releases as shown below:
Now, it will navigate to the GeckoDriver downloads link, where one can download the suitable driver based on the OS as it is platform agnostic. The snapshot below depicts all the available Selenium Firefox Driver releases.
Step 3: Once the zip file is downloaded, open it to retrieve the geckodriver executable file.
Step 4: Copy the path of the GeckoDriver and set the properties to launch the browser, and perform testing.
Step 5: Understand the Selenium script to see how GeckoDriver is useful in instantiating the Mozilla Firefox browser and executing the test cases.
Also Read: Run Selenium Tests using IE Driver
Run Tests using Selenium Firefox Driver
In the example below, let’s understand how to search for the ‘Browserstack Guide’ in the Firefox browser. The code will launch the Firefox browser, navigate through google.com and locate the search box using the name locator.
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Firefox_Example{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver",Path_of_Firefox_Driver"); // Setting system properties of FirefoxDriver WebDriver driver = new FirefoxDriver(); //Creating an object of 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/"); driver.findElement(By.name("q")).sendKeys("Browserstack Guide"); //name locator for text box WebElement searchbutton = driver.findElement(By.name("btnK"));//name locator for google search searchbutton.click(); driver.quit(); } }
The code has used System.setproperty(“webdriver.gecko.driver”,Path_of_Firefox_Driver”); method to set the path of the Firefox Driver(GeckoDriver). Then it created an object of Firefox Driver to instantiate the Mozilla Firefox browser and execute the test cases.
On executing the code, GeckoDriver will launch the Firefox browser, navigate through google.com and give the search result of the Browserstack Guide page as shown below.
Try using the sample code to run a Selenium test with FirefoxDriver. Since Firefox is widely used, running automated and manual tests ensures that websites work well and offer users the best online experience. One can run tests on Firefox, using BrowserStack’s real device cloud.
Say goodbye to your device labs and virtual machines (VM) lab for Firefox Testing. Moving on from Firefox WebDriver, you can utilize the cross-browser testing tool to test across new and old versions of IE, Edge, Safari, and Chrome on Windows and macOS.
Test Firefox on Real Device Cloud
Pro-Tip – Don’t forget to tune in with Maja Frydrychowicz, Sr. Software Engineer at Mozilla, who examines automation support from a browser vendor’s perspective and learns about new developments in Firefox that will lead to a better cross-browser testing experience for all.