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
| Attribute | Type | Description |
|---|---|---|
| browsers | list = [] | List of browsers to dynamically create test classes for. |
| selenium_hub | string = null | A selenium hub URL to test against. |
| external_host | string = null | The external host Selenium Hub can reach. |
| browser | string = null | Sentinel value to differentiate browser-specific instances. |
| headless | boolean = false | Run 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
| Name | Type | Description |
|---|---|---|
| browser | str | The name of the browser (e.g., 'chrome', 'firefox') used to resolve the import path |
Returns
| Type | Description |
|---|---|
type | The 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
| Name | Type | Description |
|---|---|---|
| browser | str | The name of the browser used to locate the appropriate Options module |
Returns
| Type | Description |
|---|---|
type | The 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
| Name | Type | Description |
|---|---|---|
| browser | str | The name of the browser for which to retrieve capabilities |
Returns
| Type | Description |
|---|---|
dict | A 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
| Type | Description |
|---|---|
selenium.webdriver.common.options.BaseOptions | An 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
| Type | Description |
|---|---|
selenium.webdriver.remote.webdriver.WebDriver | A live WebDriver instance ready to control the browser |