How to upload an extension on Firefox using pytest?

How to upload an extension on Firefox using pytest?

Hi Tom!

To upload an extension on Firefox using pytest, you can refer to the below sample pytest script -

# FFExt.py
from selenium import webdriver
import time
import pytest
 
# Pytest fixture is used when before running the test you need to execute some code
 
@pytest.fixture
def FFExt():
        driver = webdriver.Firefox()
        extension_path = "G:\\jsalert\\vpnxpi\\ghostery_privacy_ad_blocker-8.5.5-an+fx.xpi"
        driver.install_addon(extension_path, temporary=True)
        driver.get("about:support")
        addons = driver.find_element_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
        driver.execute_script("arguments[0].scrollIntoView();", addons)
        time.sleep(10)
        addons1 = driver.find_elements_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]/following-sibling::*[1]/*[2]/*/*[1]')
 
# a list is created to return names of all the installed addons
        addonsList = []
        for addon in addons1:
            addonsList.append(addon.text)
        time.sleep(10)
        driver.quit()
        return addonsList
 
# This is the test function that will ASSERT whether addon is present in the list of installed add-ons returned by the FFExt function.
 
def test_addon(FFExt):
    ghostery = "Ghostery – Privacy Ad Blocker"
    assert ghostery in FFExt

Now run the following command on the terminal with the file name

pytest FFExt.py

For more information, you can refer to our article on adding Firefox Extensions in Selenium Python: