Running Selenium Script On Mozilla Firefox

Let us start executing our first call flow using Selenium WebDriver on Mozilla Firefox browser. Selenium supports almost all of the key vendors of Browsers like Mozilla Firefox, Google Chrome, Internet Explorer, Safari, etc. For Selenium 2.xx version the support for Mozilla (till v46 or below) browser was inbuilt in Selenium jar files, so no external executable file is required.

Update: If you are using Selenium 3.xx or (and) Mozilla Firefox version (47+) follow Selenium WebDriver with Mozilla Firefox using Gecko Driver.

Selenium WebDriver provides a class called FirefoxDriver (which implements interfaces like WebDriver and Remote WebDriver). It provides all the methods or APIs which are required to interact with Firefox browser.

What is an API?

An application-programming interface (API) is a set of programming instructions and standards for accessing a Web-based software application or Web tool. Java application programming interface (API) is a list of all classes that are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and constructors. These prewritten classes provide a tremendous amount of functionality to a programmer.

Sample Code –

package com.qatechhub.day1;

import org.openqa.selenium.firefox.FirefoxDriver;

public class WorkingWithFirefox {

	FirefoxDriver driver = new FirefoxDriver();

	String url = "";

	public void invokeBrowser(){
		/* To Maximize the window */

		/* To delete all the cookies of the browser */

		/* To navigate to a particular URL */

		String titleOfPage;

		/* To get The Title of the page */

		titleOfPage = driver.getTitle();

		System.out.println("Title of the page is : " + titleOfPage);

		String currentUrl;

		/* To get The url of the page */
		currentUrl = driver.getCurrentUrl();

		System.err.println("Title of the page is : "+ currentUrl);

		String pageSource;

		/* To get The source code current page */
		pageSource = driver.getPageSource();

		System.out.println("Page Source : "+ pageSource);


	public void closeBrowser(){
		/* To close current active window of the browser */

	public void closeAllBrowser(){

		/* To close all the opened browser session*/


Let us discuss each and every line from above code.

Variables declared:

FirefoxDriver driver = new FirefoxDriver();

The first step to start any browsing any website is to identify the browser. Here, we are creating an instance of Firefox Driver class called Driver, which represents the Firefox Browser of your system (Browser must be installed on the system). Driver instance will be used to access different methods or APIs which will be required to interact with Firefox Browser.

String url = “”;

Declared a variable with name “url” of type String – the address of the website where we will navigate to.
“url” must be specified with the protocol used, for example, HTTP or HTTPS, etc.

public void invokeBrowser(){

Methods in above code:

We have declared following methods in the above class

  • invokeBrowser() – This method is written to invoke the browser, do some configuration specific to browsers.
  • closeBrowser() – This method is written to close the current active window of the browser.
  • closeAllBrowser() – This method is written to close all the browsers which were opened by the Selenium session.

Basic Selenium API commands:


  • This method is used to maximize the window.


  • This method is used to bypass the cookies of the browser, cookies are some files which stores the browsing history of the user which can impact testing of your application.

To learn more about cookies handling follow Cookies Handling using Selenium WebDriver


  • driver.get() method navigates you to a particular URL passed as an argument.


  • driver.getTitle() method returns the title of the current page.


  • driver.getCurrentUrl() method returns the url of currently active page.


  • driver.getPageSource() method returns the source code of the page.


  • driver.close() method closes the current active window or tab.


  • driver.quit() method closes all windows which are opened by current active session.

To execute the above code we need a main method. Let’s create a new class with name “DemoWorkingWithFirefox” to demo this scenario.

public class DemoWorkingWithFirefox {
public static void main(String[] args) {

WorkingWithFirefox ff = new WorkingWithFirefox();





In the next tutorial, we will cover How to execute Selenium WebDriver scripts on Chrome Browser

Try and execute above scenario, If you have any questions, comments or queries feel free to write us at or comment below. Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: