Running Selenium Script On Mozilla Firefox

July 20, 2016

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 –

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 = “http://www.qatechhub.com”;

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.

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:

driver.manage().window().maximize();

  • This method is used to maximize the window.

driver.manage().deleteAllCookies();

  • 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(url);

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

driver.getTitle();

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

driver.getCurrentUrl();

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

driver.getPageSource();

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

driver.close();

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

driver.quit();

  • 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.

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 amrita@qatechhub.com or comment below. Happy Learning 🙂

Amrita Joshi

About the Author

Amrita Joshi

Leave a Comment: