Running first call using Selenium WebDriver 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 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 below 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();
In this piece of code, we are maximizing the browser window. By default browser window opens in not-so-maximised mode.

Driver.manage().deleteAllCookies();
Above API will delete all the cookies from your browser session. It’s a good practice to delete all the cookies before starting the test or as the application demands.

There is a lot to learn in cookies handling which is covered in the tutorial – Cookies Handling using Selenium WebDriver

Driver.get(url);
get() method takes a string argument (url of the page), this API will navigate you to the respective page of the application under test.

Driver.getTitle();
getTitle() method returns the title of current page in string format.

Driver.getCurrentUrl();
getCurrentUrl() method returns the url of currently active page as a string.

Driver.getPageSource();
getPageSource() method returns the complete html source code of the page.

Driver.close();
close() method closes only the browser window that is currently active.

Driver.quit();
quit() method closes all windows that FirefoxDriver has opened. In the case of multiple windows, parent window, as well as all the other windows, will be closed.

To execute 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: