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.
-
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.
-
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.
-
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();
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);
}
}
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.