This class provides a base for running browser-based integration tests using Selenium within a live server environment. It features built-in support for managing WebDriver instances, handling implicit waits, and capturing screenshots across various viewport sizes and themes. The class also includes specialized context managers for testing responsive layouts, right-to-left languages, and high-contrast accessibility modes.
Attributes
| Attribute | Type | Description |
|---|
| implicit_wait | integer = 10 | The number of seconds the WebDriver should wait when searching for elements before throwing an exception. |
| external_host | string = null | The hostname used to construct the live server URL, overriding the default host if provided. |
| screenshots | boolean = false | A boolean flag that enables the generation of separate test cases for different screenshot types and allows capturing screenshots during test execution. |
Methods
live_server_url()
@classmethod
def live_server_url() - > string
Constructs the full URL of the live test server using the configured host and port.
Returns
| Type | Description |
|---|
string | The absolute URL of the live server, including the protocol and port |
allowed_host()
@classmethod
def allowed_host() - > string
Determines the hostname allowed for the test server, prioritizing the external host if defined.
Returns
| Type | Description |
|---|
string | The hostname or IP address used for the test server |
setUpClass()
@classmethod
def setUpClass() - > null
Initializes the Selenium WebDriver, sets the implicit wait timeout, and registers the cleanup handler.
Returns
desktop_size()
@classmethod
def desktop_size() - > contextmanager
Context manager that resizes the browser window to a standard desktop resolution of 1280x720.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the window is at desktop size |
small_screen_size()
@classmethod
def small_screen_size() - > contextmanager
Context manager that resizes the browser window to a small screen resolution of 1024x768.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the window is at small screen size |
mobile_size()
@classmethod
def mobile_size() - > contextmanager
Context manager that resizes the browser window to a mobile resolution of 360x800.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the window is at mobile size |
rtl()
@classmethod
def rtl() - > contextmanager
Context manager that sets the browser to desktop size and overrides the language settings to a right-to-left (RTL) locale.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the environment is configured for RTL testing |
dark()
@classmethod
def dark() - > contextmanager
Context manager that enables dark mode via local storage and sets the browser to desktop size.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the dark theme is active |
@classmethod
def set_emulated_media(
media: string,
features: list
) - > null
Configures CSS media emulation for Chromium-based browsers using the Chrome DevTools Protocol.
Parameters
| Name | Type | Description |
|---|
| media | string | The media type to emulate, such as 'print' or 'screen' |
| features | list | A list of media feature objects to emulate, such as prefers-color-scheme |
Returns
high_contrast()
@classmethod
def high_contrast() - > contextmanager
Context manager that emulates a high-contrast environment by forcing active colors in the browser.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while high-contrast mode is emulated |
take_screenshot()
@classmethod
def take_screenshot(
name: string
) - > null
Captures and saves a screenshot to the local filesystem if screenshot testing is enabled.
Parameters
| Name | Type | Description |
|---|
| name | string | The descriptive name used to identify the specific state or element being captured in the filename |
Returns
get_browser_logs()
@classmethod
def get_browser_logs(
source: string,
level: string
) - > list
Return Chrome console logs filtered by level and optionally source.
Parameters
| Name | Type | Description |
|---|
| source | string | The specific log source to filter by, such as 'network' or 'javascript' |
| level | string | The severity level to filter logs by, defaults to 'ALL' |
Returns
| Type | Description |
|---|
list | A list of log entries matching the specified level and source criteria |
disable_implicit_wait()
@classmethod
def disable_implicit_wait() - > contextmanager
Disable the default implicit wait.
Returns
| Type | Description |
|---|
contextmanager | A context manager that yields while the implicit wait is set to zero |