• Selenium Video Tutorials

Selenium WebDriver - Firefox Options



FirefoxOptions is a specific class in Selenium Webdriver which helps to handle options which are only applicable to the Firefox driver. It helps to modify the settings and capabilities of the browser while running an automated test on Firefox. The FirefoxOptions class extends another class known as the MutableCapabilities class.

The FirefoxOptions class is available in the latest version of Selenium. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.

Example 1 - Open in Headless Browser Using FirefoxOptions

In this example, we would open and launch an application in the headless browser using the FirefoxOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FirefoxOptnsHeadless {
   public static void main(String[] args) throws InterruptedException {

      // object of FirefoxOptions
      FirefoxOptions opt = new FirefoxOptions();

      // open in headless mode
      opt.addArguments("-headless");

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with headless mode
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Output

Getting the page title: Selenium Practice - Student Registration Form

Process finished with exit code 0

In the above example, we observed that the Firefox browser was launched in a headless mode. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - SSL Certificates Using FirefoxOptions

To handle SSL certificates in Firefox, we have to use the FirefoxOptions class along with the DesiredCapabilities class. SSL error shown in Firefox is Warning: Potential Security Risk Ahead.

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLErrorFirefox {
   public static void main(String[] args) throws InterruptedException {
   
      // Desired Capabilities class to profile Firefox
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      
      // Firefox Options class
      FirefoxOptions opt = new FirefoxOptions();
      opt.merge(dc);
      
      // Initiate the Webdriver with options
      WebDriver driver = new FirefoxDriver(opt);
      
      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // launch application with SSL error
      driver.get("https://expired.badssl.com");
      
      // get browser title
      System.out.println("Browser title in Firefox: " + driver.getTitle());
      
      // quitting the browser
      driver.quit();
   }
}

Output

Browser title in Firefox: Privacy error

Process finished with exit code 0

In the above example, we had the SSL certificate error in Firefox and launched the application and then obtained the browser title with the message in the console - Browser title in Firefox: Privacy error.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 3 - Firefox Profile Using FirefoxOptions

Let us take an example, where we would launch the Firefox browser with our own Firefox profile using the FirefoxOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import java.util.concurrent.TimeUnit;

public class FirefoxOptnsProfile {
   public static void main(String[] args) throws InterruptedException {

      // object of FirefoxOptions
      FirefoxOptions opt = new FirefoxOptions();

      // object of ProfilesIni
      ProfilesIni prof = new ProfilesIni();

      // object of ProfilesIni
      FirefoxProfile p = prof.getProfile("<profileName>");
      opt.setProfile(p);

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Output

Getting the page title: Selenium Practice - Student Registration Form

Process finished with exit code 0

In the above example, we observed that the Firefox browser was launched with a created profile. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 4 - Open Maximized Browser Using FirefoxOptions

In this example, we would open and launch an application in the browser maximized size using the FirefoxOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class FirefoxOptnsMaximized {
   public static void main(String[] args) throws InterruptedException {

      // object of FirefoxOptions
      FirefoxOptions opt = new FirefoxOptions();

      // open browser in maximized mode
      opt.addArguments("--kiosk")

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage 
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

      // quitting browser
      driver.quit();
   }
}

Output

Process finished with exit code 0

In the above example, we observed that the Firefox browser was launched in a maximized browser.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

This concludes our comprehensive take on the tutorial on Selenium Webdriver - Firefox Options. We’ve started with describing a FirefoxOptions class, and walked through examples of how to add profiles to Firefox browser, how to launch the Firefox browser maximized, how to open the Firefox browser in headless mode, and handle SSL certificates errors taking help of FirefoxOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the FirefoxOptions class in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements