Adding Firefox Extensions With Selenium In Python

Posted by Nishant Choudhary | June 9, 2021
Selenium Python •

36360 Views | 7 Min Read

FireFox Extensions With Selenium In Python

Modern browsers are equipped with all sorts of functionalities, i.e., from bookmarks to GPS location tracking, developer tools, and more. Almost all the modern web browsers have immense capabilities – Firefox has inbuilt screenshot capabilities, Opera has a free VPN, and Edge comes with in-built support for displaying ebooks with EPUB extension. Though browsers like Firefox, Chrome, and others are majorly used for web browsing, the fact is that Add Ons (or extensions) in Firefox provide additional power to the web browser! The same rule of thumb is also applicable to other browsers like Chrome, Edge, etc.

pasted image 0 (14)

Source

Additional add-ons, plugins, extensions are required to extend or customize browser functionalities, boost productivity, and suit specific requirements of users and developers. Test automation engineers that use Selenium test automation can also benefit from browser extensions like normal end-users. In fact, testers often need to automate the testing of these extensions itself 🙂

The catch is that the extensions installed on the Firefox browser present on the local machine will not be available when you fire up a Firefox instance using Gecko Driver and Selenium WebDriver. In this part of the Selenium Python tutorial, we deep dive into adding extensions in Firefox using Selenium WebDriver and Python.

What is Firefox WebExtensions API?

In Sept 2015, Mozilla started signing and validating every extension to ensure that the third-party extensions enable Firefox customization without sacrificing security or exposing users vulnerable to malware. Mozilla deprecated XPCOM and XUL-based add-ons and adapted a new extensions API (i.e., WebExtensions API).

New extensions API is compatible with the model used by Opera, Chrome, and other latest browsers. Thus, a Firefox extension developer can easily develop and manage cross-browser extensions.

What is XPI?

XPI is a cross-platform installer module file. It’s the same as zip files and can be accessed using any archive/compression program. XPI installation files are in use by Firefox, SeaMonkey, and Thunderbird. It is used for installing a web browser plugin, addons, extensions, etc.

XPI add ons can be installed by simply dragging and dropping into any open Firefox browser window. Mozilla uses XPInstall to read the installation instructions contained in the XPI file, which are later executed for the installation.

pasted image 0 (13)

Chromium-based browsers like Chrome, Opera, and Edge use CRX files as extension installers. Safari uses SAFARIEXTZ. CRX files can’t be used to install extensions in Mozilla. XPIs can’t be used in Chromium-based browsers. However, the underlying API is the same for all these browsers, due to which maintenance of extensions is pretty easy.

How to add extensions in Firefox using Selenium WebDriver and Python?

Now that we have covered the fundamentals of extensions in Firefox, let’s look at how to add Firefox extensions using Selenium WebDriver and Python. Let’s look at the various approaches available to leverage Selenium and Firefox extensions for Selenium web automation.

Using install_addon API of Selenium WebDriver to add Firefox extensions using Python

The install_addon method of the Selenium WebDriver takes two input arguments – path to the desired Firefox extension and a Boolean value which indicates whether installation of the extension is temporary (or not).

Step 1: Let’s download the XPIs of the target Firefox extensions.

You can point your cursor to the “ADD to Firefox button,” right-click and copy the XPI link. Then, open it in a new tab.

pasted image 0 (12)

Alternatively, you can find a dedicated XPI download link by opening the extension’s landing page in a different browser than Firefox.

pasted image 0 (11)

For demonstration, we’re downloading the following Firefox extensions – “VPN Master” and “Ghostery Privacy Ad Blocker.” The XPI downloader link for VPN Master is available below the green-colored button (visible in the below image) if opened using Chrome.

Step 2: Here’s how the webdriver’s install_addon function is implemented for installing install the add-on in Mozilla:

Now, let’s use this install_addon method to install the Ghostery Firefox extension temporarily.

Shown below is the Python implementation that uses Selenium WebDriver and install_addon method to install the Ghoserty plugin. The first two lines import the dependent libraries. I have added sufficient comments in the try, except, and finally python code block for easing the code walkthrough process.

Filename: FFExt.py

Test Execution

Run the test by triggering the following command on the terminal:

Here is the execution output of the Firefox extension example that used Selenium WebDriver:

pasted image 0 (10)

As seen from the execution snapshot, the Ghostery plugin icon appears at the top right corner. The Firefox extension installed using the install_addon method (of Selenium WebDriver) is enabled by default.

Let’s try this: Comment out the following line.

As seen in the below snapshot, the Ghostery extension is no longer a part of the installed extensions since it was installed temporarily.

pasted image 0 (9)

Using PyTest to add Firefox extensions using Selenium and Python

PyTest is an open-source Python testing framework that is more extensively in place of the PyUnit framework. You can use PyTest for writing small tests and complex functional tests for python-based applications/libraries. On Github, PyTest clocks 7.4k+ stars. You can check out our existing blog for a quick recap on Selenium web automation in Python using PyTest.

File Name: FFExt.py

Run the test by triggering the following command on the terminal:

Here is the execution snapshot, which indicates that one test was successful.

pasted image 0 (8)

Note: For every python project, pytest is the perfect choice for a reliable and maintainable testing architecture. For a quick overview on getting started with pytest, check out the video below from LambdaTest YouTube Channel.

Read – Selenium Python Tutorial: Getting Started With Pytest

Using pre-loaded Firefox extensions and FirefoxProfile in Selenium WebDriver

FirefoxProfile is a collection of settings, extensions, and customizations done on top of the Firefox browser. You can create several Firefox profiles to tailor-make the look & feel of the browser as per your preferences.

To add Firefox extensions in Selenium using FirefoxProfile, please follow the below-mentioned steps:

Step 1: Create a Firefox profile. Before doing so, you need to close all the active Firefox windows.

Step 2: Press “win + R” to open the Run dialog window. Type “firefox.exe -P” and press OK.

pasted image 0 (7)

Step 3: Now, click on the Create Profile button.

pasted image 0 (6)

Step 4: Click Next.

pasted image 0 (5)

Step 5: Enter a name for your new Firefox profile. Copy path to the destination folder (this will be required later). Click Finish to create the profile.

pasted image 0 (4)

Step 6: Launch Firefox with your newly created profile.

pasted image 0 (3)

Step 7: Add Ghostery Addon to this profile.

pasted image 0 (2)

Now, let’s write a PyTest script to check if the Ghostery extension is installed when we launch the created Firefox profile in a remotely-controlled Firefox instance using Selenium Python.

Existing Firefox profile can be loaded in Selenium webdriver using:

Filename: FFExt_method_2.py

Run the following command on the terminal to trigger the test:

Here is the execution snapshot:

pasted image 0 (1)

Conclusion:

Extensions, especially VPN extensions, are often required to test how your application would perform across different geographies. Also, if you’ve implemented an adBlocker detection technology in your web application, then you need to install adBlocker extensions to check if your adBlocker detection technology is effective.

In this part of the Selenium Python tutorial, we had a look at two different mechanisms to install addons in Selenium-controlled Firefox instances. Talking of extensions, we hope you already have LambdaTest chrome extension installed to test applications on the go. LambdaTest is a stellar tool for all your Selenium web automation requirements in the cloud. Do check out LT Browser and test applications across 50+ mobile screens.

Happy Testing!

Written by

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *