Running Selenium Test On FireFox Browser

0
96

Introduction

Selenium is a multi-browser multi-platform automation tool. But, to achieve this feature Selenium needs browser-specific browser. Hence, to run the Selenium script, on different browsers, we need corresponding browser drivers. In this tutorial, we will learn how to execute the Selenium code on the Firefox browser.

Introducing GeckoDriver

What is Gecko Driver

Gecko is a web browser engine. Also, GeckoDriver provides a proxy to interact with Gecko-based browsers like Firefox. Typically, the applications developed by Mozilla Foundation and the Mozilla Corporation use this driver.

How Gecko Driver works 

Internally, Selenium tests interact with Firefox using an executable called GeckoDriver.exe. Basically, this executable starts a server on the local system. Then, all the tests communicate to this server to run the Selenium commands.

Why Selenium 3 needs Gecko driver

From Selenium 3 onwards, there is no native implementation of Firefox. So, we need Gecko Diver to establish the link between Selenium tests and Firefox browser.  Therefore, we direct all the Selenium commands to Firefox browser through Gecko Driver. Also, the latest version of Firefox(version 47.0 onwards) does not allow external drivers to interact with the browser. Hence, Selenium tests can communicate to it through Gecko driver only.

We do not need Gecko Driver when working on Selenium 2 and old versions of Firefox.

In this tutorial, we are using the latest available version of Firefox with Selenium 3.

What is Marionette Driver

Marionette is the automation driver which uses the remote protocol of Firefox to control the UI. With Selenium 3, automation script needs to initialize Gecko driver to control Firefox browser. Also, Selenium sends commands to Geckodriver using W3C protocol, which is a world standard. On the other hand, Geckodriver uses Marionette protocol to communicate with the Firefox browser. In short, Gecko Driver is Marionette based solution used for automating Mozilla Firefox.

In this tutorial, we will learn how we can download the Gecko driver and set the path to execute the Selenium script on Firefox browser using Gecko driver. Also, we will learn the following points in this tutorial:

  • Download Gecko driver for Firefox browser
  • Set the Path variable or set the system property in the script
  • Execute the code

Pre-requisites

The prerequisites to execute test on Firefox browser are:

In case you have missed any of the above set up, please refer to the individual tutorials available in our course for them.

Run Selenium tests on Firefox

Mozilla Firefox Browser is a free and open-source web browser. Additionally, it works on multiple operating systems like Windows, macOS, Linux. Also, the web and mobile devices both use it.

To run the automation on Firefox, you need to initialize the driver to call Firefox driver.

WebDriver driver = new FirefoxDriver();

Then, the test script looks for the appropriate driver when you initialize the driver to Firefox driver. Let us try to execute our first test case automation script If the script does not find the driver when you run it, it would throw an exception as shown in the image below:

 

Note: If you have not yet executed the script, you can do so by doing the right click on the java class > Run As > Java Application.

The error clearly says that path to driver executable should be set. Therefore, it implies that it is not able to locate the driver required to run the automation on the desired browser. Hence, to execute Selenium script on the Firefox, we need to set the path to Firefox driver executable.

Download GeckoDriver 

  1. First, check the version of the Firefox browser installed in your machine. You can do so by Help > About Firefox. It will show the current version installed.

2. Next, we need to download the latest version of the driver for Firefox which is GeckoDriver. For that, go to the official site of Selenium and move to third-party browser drivers section. Click on Mozilla GeckoDriver link.

3. It will navigate to the git repository for Geckdriver. There click on Release under Downloads.

4. Click to download suitable GeckoDriver. If you have 64-bit windows, you should download geckodriver-v0.24-win64.zip. Save the zip file in the local system.

 

Note: Please select the Firefox driver version which is available for your Firefox browser version. Otherwise, Selenium will not be able to establish the Firefox browser session and it will throw an exception. 

  1. Extract the content of the saved zip file in a folder and you will get GeckDriver driver executable.

Code Changes

After saving Gecko driver exe, we need to tell the Selenium script where to find it. There are two ways of achieving this:

  1. Set the driver location using the Path variable which is shown in detail in the First Selenium Test Case tutorial. With this method, there is no need to do any changes in the code. Hence, you can directly go to the execution part.
  2. Set the driver location in Selenium script using System.setProperty().

For the second method, we will set a system property “webdriver.Firefox.driver” which points to the location of Firefox driver executable. Please refer to the code snippet below.

package seleniumAutomationTests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyFirstTest {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
     //Set the system property for Gecko driver
      System.setProperty("webdriver.gecko.driver","C:\\softwares\\drivers\\geckodriver.exe");
      
      //declare instance of WebDriver and run using firefoxdriver
      WebDriver driver = new FirefoxDriver();
    
    //load the webpage of application under test
    driver.get("https://www.facebook.com/");
        
    //get the title of the page in a string variable
    String pageTitle = driver.getTitle();
    
    //print the page title on console
    System.out.println(pageTitle);
    
    //close the browser
    driver.close();

  }

}

Execution

To execute the script, right click on the java class name and go to Run As > Java Application. This will start the execution of the automation script:

  • Locate the driver at the location set by the System.setProperty method.
  • Start Firefox browser using Gecko driver
  • Launch the application under test
  • Fetch the value of page title of the web page and store it in a string.
  • Print the page title on the console
  • Close the web page and the execution of the script is complete.

Checking the output 

When we start the execution of our script then the application under test will be started in a new Firefox browser window. And, it will perform the commands provided in the script. Like, in our case it will fetch the page title and the driver.close() method will close the browser.

We can observe the output of the script on the console window at the bottom of the Eclipse IDE. It will print the page title here.

Commonly encountered problems:

  • java.lang.IllegalStateException: When System.setProperty does not set the path to the Geckodriver, this exception is thrown. So, Selenium is not able to locate the gecko driver executable. Hence, we can resolve it by setting the value of “webdriver.gecko.driver” using System.setProperty method in the code.

System.setProperty(“webdriver.gecko.driver”,”C://softwares//drivers//geckodriver.exe”)

  • org.openqa.selenium.SessionNotCreatedException: This exception occurs when the version of Selenium, Geckodriver, and Firefox are not compatible with each other. Resolve it by upgrading Firefox version to the latest with latest Gecko driver.
  • org.openqa.selenium.firefox.NotConnectedException: When the Firefox version is upgraded to the latest in the local system due to an available upgrade, this exception is thrown. But, the latest Firefox version is not compatible with current Selenium jars and the gecko driver. To resolve it, upgrade Selenium jars and gecko to the latest version compatible with the new version of Firefox browser.

Conclusion

In this tutorial, we learned about how to set up the Gecko driver for executing Selenium code on Firefox browser. Next, we will see how to execute the same Selenium code on the other browsers like Safari and IE to achieve multi-browser feature of WebDriver.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here