0
0
Selenium Pythontesting~8 mins

Firefox configuration in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Firefox configuration
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   └── login_page.py
├── utils/
│   ├── firefox_options.py
│   └── webdriver_factory.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── test_report.html
├── conftest.py
└── requirements.txt
Test Framework Layers
  • Driver Layer: webdriver_factory.py creates and configures Firefox WebDriver instances with desired options.
  • Page Objects: Classes in pages/ represent web pages, encapsulating locators and actions.
  • Tests: Test scripts in tests/ use pytest to run tests with Firefox browser.
  • Utilities: Helper modules like firefox_options.py define Firefox-specific settings (headless mode, preferences).
  • Configuration: YAML files in config/ store environment variables, URLs, and credentials.
Configuration Patterns

Use config.yaml to define environment URLs and browser settings. Use firefox_options.py to set Firefox preferences like headless mode, download folder, and disable notifications.

Example snippet from firefox_options.py:

from selenium.webdriver.firefox.options import Options

def get_firefox_options(headless: bool = False) -> Options:
    options = Options()
    if headless:
        options.headless = True
    # Disable notifications
    options.set_preference("dom.webnotifications.enabled", False)
    # Set download folder
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.dir", "/tmp/downloads")
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
    return options

In webdriver_factory.py, create Firefox driver with these options:

from selenium import webdriver
from utils.firefox_options import get_firefox_options

def create_firefox_driver(headless: bool = False):
    options = get_firefox_options(headless)
    driver = webdriver.Firefox(options=options)
    driver.implicitly_wait(10)
    return driver
Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports saved in reports/ folder.
  • Configure CI pipelines (GitHub Actions, Jenkins) to run tests on Firefox browser using headless mode for speed.
  • Store test reports as artifacts in CI for easy access and review.
  • Use environment variables in CI to switch between headless and headed modes or different Firefox versions.
Best Practices for Firefox Configuration
  1. Use Firefox Options Object: Always configure Firefox preferences and options via the Options class to keep setup clean and reusable.
  2. Headless Mode for CI: Run Firefox in headless mode in CI environments to save resources and avoid UI dependencies.
  3. Explicit Preferences: Set preferences like disabling notifications and setting download folders to avoid unexpected popups during tests.
  4. Centralize Configuration: Keep browser options and environment settings in separate utility and config files for easy maintenance.
  5. Implicit and Explicit Waits: Use waits to handle dynamic page elements reliably with Firefox.
Self Check

Where would you add a new Firefox preference to disable the PDF viewer in this framework structure?

Key Result
Use a dedicated Firefox options utility to configure browser preferences and create WebDriver instances for clean, reusable test setup.