Skip to main content

SeleniumTestCase

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

AttributeTypeDescription
implicit_waitinteger = 10The number of seconds the WebDriver should wait when searching for elements before throwing an exception.
external_hoststring = nullThe hostname used to construct the live server URL, overriding the default host if provided.
screenshotsboolean = falseA 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

TypeDescription
stringThe 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

TypeDescription
stringThe 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

TypeDescription
null

desktop_size()

@classmethod
def desktop_size() - > contextmanager

Context manager that resizes the browser window to a standard desktop resolution of 1280x720.

Returns

TypeDescription
contextmanagerA 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

TypeDescription
contextmanagerA 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

TypeDescription
contextmanagerA 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

TypeDescription
contextmanagerA 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

TypeDescription
contextmanagerA context manager that yields while the dark theme is active

set_emulated_media()

@classmethod
def set_emulated_media(
media: string,
features: list
) - > null

Configures CSS media emulation for Chromium-based browsers using the Chrome DevTools Protocol.

Parameters

NameTypeDescription
mediastringThe media type to emulate, such as 'print' or 'screen'
featureslistA list of media feature objects to emulate, such as prefers-color-scheme

Returns

TypeDescription
null

high_contrast()

@classmethod
def high_contrast() - > contextmanager

Context manager that emulates a high-contrast environment by forcing active colors in the browser.

Returns

TypeDescription
contextmanagerA 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

NameTypeDescription
namestringThe descriptive name used to identify the specific state or element being captured in the filename

Returns

TypeDescription
null

get_browser_logs()

@classmethod
def get_browser_logs(
source: string,
level: string
) - > list

Return Chrome console logs filtered by level and optionally source.

Parameters

NameTypeDescription
sourcestringThe specific log source to filter by, such as 'network' or 'javascript'
levelstringThe severity level to filter logs by, defaults to 'ALL'

Returns

TypeDescription
listA 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

TypeDescription
contextmanagerA context manager that yields while the implicit wait is set to zero