Skip to main content

SeleniumTestCaseBase

This class provides a base for dynamically creating browser-specific test cases using Selenium and LiveServerTestCase. It supports running tests across multiple browsers, configuring headless modes, and connecting to a remote Selenium Hub. The class automatically generates subclasses for each specified browser and manages the initialization of corresponding WebDriver instances and options.

Attributes

AttributeTypeDescription
browserslist = []List of browsers to dynamically create test classes for.
selenium_hubstring = nullA selenium hub URL to test against.
external_hoststring = nullThe external host Selenium Hub can reach.
browserstring = nullSentinel value to differentiate browser-specific instances.
headlessboolean = falseRun browsers in headless mode.

Constructor

Signature

def SeleniumTestCaseBase()

Methods


import_webdriver()

@classmethod
def import_webdriver(
browser: str
) - > type

Imports and returns the Selenium WebDriver class for a specific browser. This allows the test runner to dynamically load the correct driver based on the test configuration.

Parameters

NameTypeDescription
browserstrThe name of the browser (e.g., 'chrome', 'firefox') used to resolve the import path

Returns

TypeDescription
typeThe Selenium WebDriver class corresponding to the specified browser

import_options()

@classmethod
def import_options(
browser: str
) - > type

Imports and returns the Selenium Options class for a specific browser. This is used to configure browser-specific settings like headless mode or arguments.

Parameters

NameTypeDescription
browserstrThe name of the browser used to locate the appropriate Options module

Returns

TypeDescription
typeThe Selenium Options class corresponding to the specified browser

get_capability()

@classmethod
def get_capability(
browser: str
) - > dict

Retrieves the default desired capabilities for a specific browser. It includes specialized logging preferences for Chrome to capture browser console logs.

Parameters

NameTypeDescription
browserstrThe name of the browser for which to retrieve capabilities

Returns

TypeDescription
dictA dictionary of Selenium desired capabilities for the specified browser

create_options()

@classmethod
def create_options() - > selenium.webdriver.common.options.BaseOptions

Instantiates and configures the browser options based on the current test state. It applies headless flags and disables intrusive UI elements like info bars or password manager pop-ups.

Returns

TypeDescription
selenium.webdriver.common.options.BaseOptionsAn initialized and configured Options object for the current browser

create_webdriver()

@classmethod
def create_webdriver() - > selenium.webdriver.remote.webdriver.WebDriver

Initializes the Selenium WebDriver instance for the test. It handles both local driver execution and remote execution via a Selenium Hub if a hub URL is configured.

Returns

TypeDescription
selenium.webdriver.remote.webdriver.WebDriverA live WebDriver instance ready to control the browser