DriverWrapper Interface

Warning

This is auto-generated interface documentation for the abstract DriverWrapper class.

This is essential for providing a comprehensive overview of all available methods for the DriverWrapper object.

For the actual signature, please refer to mops.base.driver_wrapper.DriverWrapper.

DriverWrapper provides a unified interface for managing different types of drivers (e.g., Playwright, Appium, Selenium), enabling consistent and flexible test automation. It centralizes driver management, including session handling and lifecycle operations, ensuring efficient resource usage and integration with test frameworks. Additionally, it offers advanced methods which be found below.


class mops.base.driver_wrapper.DriverWrapper(*args: Any, **kwargs: Any)[source]

A wrapper class for managing web and mobile driver instances, supporting Selenium, Appium, and Playwright.

This class serves as a crossroad for interacting with different driver types, allowing for flexible management of web and mobile sessions.

It also provides platform-specific flags and information to assist with automation tasks.

driver: SeleniumDriver | AppiumDriver | PlaywrightDriver
context: PlaywrightContext
browser: PlaywrightBrowser
session

alias of DriverWrapperSessions

anchor: Element | None = None
is_selenium: bool = False
is_playwright: bool = False
is_appium: bool = False
is_tablet: bool = False
is_ios: bool = False
is_ios_tablet: bool = False
is_ios_mobile: bool = False
is_android: bool = False
is_android_tablet: bool = False
is_android_mobile: bool = False
is_simulator: bool = False
is_real_device: bool = False
browser_name: str | None = None
__init__(driver: Driver)[source]

Initialize the DriverWrapper instance based on the provided driver source.

This constructor sets up the driver wrapper, which can support Appium, Selenium, or Playwright drivers. It also manages session tracking and platform-specific configurations, such as mobile resolution and platform type.

Parameters:

driverDriver object that holds appium / selenium / playwright driver to initialize

label: str | None = None
is_mobile_resolution: bool = False
is_desktop: bool = False
is_mobile: bool = False
quit(silent: bool = False, trace_path: str = 'trace.zip') None[source]

Quit the driver instance.

Parameters:

silent (bool) – If True, suppresses logging.

Selenium/Appium:

Parameters:

trace_path (str) – Compatibility argument for Playwright.

Playwright:

Parameters:

trace_path (str) – Path to the trace file.

Returns:

None

save_screenshot(file_name: str, screenshot_base: Image | bytes = None, convert_type: str | None = None) Image[source]

Take a full screenshot of the driver and save it to the specified path/filename.

Parameters:
  • file_name (str) – Path or filename for the screenshot.

  • screenshot_base (bytes, PIL.Image.Image) – Screenshot binary or image to use (optional).

  • convert_type (str) – Image conversion type before saving (optional).

Returns:

PIL.Image.Image

get_scroll_position() int[source]

Return the current vertical scroll position of the page.

Returns:

int - Current vertical scroll offset in pixels.

assert_screenshot(filename: str = '', test_name: str = '', name_suffix: str = '', threshold: float | None = None, delay: float | None = None, remove: Element | list[Element] = None, cut_box: Box = None, hide: Element | list[Element] = None) None[source]

Assert that the given screenshot matches the currently taken screenshot.

Parameters:
  • filename (str) – The full name of the screenshot file. If empty - filename will be generated based on test name & Element name argument & platform.

  • test_name (str) – The custom test name for generated filename. If empty - it will be determined automatically.

  • name_suffix (str) – A suffix to add to the filename. Useful for distinguishing between positive and negative cases for the same Element during one test.

  • threshold (Optional[int or float]) – The acceptable threshold for comparing screenshots. If None - takes default threshold or calculate its automatically based on screenshot size.

  • delay (Optional[int or float]) – The delay in seconds before taking the screenshot. If None - takes default delay.

  • remove (Optional[Element or List[Element]]) – Element to remove from the screenshot. Can be a single element or a list of elements.

  • cut_box (Optional[Box]) – A Box specifying a region to cut from the screenshot. If None, no region is cut.

  • hide (Optional[Element or List[Element]]) – Element to hide in the screenshot. Can be a single element or a list of elements.

Returns:

None

soft_assert_screenshot(filename: str = '', test_name: str = '', name_suffix: str = '', threshold: float | None = None, delay: float | None = None, remove: Element | list[Element] = None, cut_box: Box = None, hide: Element | list[Element] = None) tuple[bool, str][source]

Compare the currently taken screenshot to the expected screenshot and return a result.

Parameters:
  • filename (str) – The full name of the screenshot file. If empty - filename will be generated based on test name & Element name argument & platform.

  • test_name (str) – The custom test name for generated filename. If empty - it will be determined automatically.

  • name_suffix (str) – A suffix to add to the filename. Useful for distinguishing between positive and negative cases for the same Element during one test.

  • threshold (Optional[int or float]) – The acceptable threshold for comparing screenshots. If None - takes default threshold or calculate its automatically based on screenshot size.

  • delay (Optional[int or float]) – The delay in seconds before taking the screenshot. If None - takes default delay.

  • remove (Optional[Element or List[Element]]) – Element to remove from the screenshot.

  • cut_box (Optional[Box]) – A Box specifying a region to cut from the screenshot. If None, no region is cut.

  • hideElement to hide in the screenshot. Can be a single element or a list of elements.

Returns:

typing.Tuple (bool, str) - result state and result message

set_local_storage_item(items: list[dict]) DriverWrapper[source]

Set one or more items in localStorage.

Each dict must contain key and value fields.

Parameters:

items (List[dict]) – A list of dicts with key and value.

Returns:

DriverWrapper - The current instance of the driver wrapper.

set_session_storage_item(items: list[dict]) DriverWrapper[source]

Set one or more items in sessionStorage.

Each dict must contain key and value fields.

Parameters:

items (List[dict]) – A list of dicts with key and value.

Returns:

DriverWrapper - The current instance of the driver wrapper.

get_local_storage_item(key: str) str | None[source]

Retrieve a single item from localStorage by key.

Parameters:

key (str) – The key to look up.

Returns:

The value string, or None if the key does not exist.

Return type:

Union[str, None]

get_session_storage_item(key: str) str | None[source]

Retrieve a single item from sessionStorage by key.

Parameters:

key (str) – The key to look up.

Returns:

The value string, or None if the key does not exist.

Return type:

Union[str, None]

get_local_storage_items() dict[source]

Retrieve all items from localStorage as a dictionary.

Returns:

A dict mapping every key to its value.

Return type:

dict

get_session_storage_items() dict[source]

Retrieve all items from sessionStorage as a dictionary.

Returns:

A dict mapping every key to its value.

Return type:

dict

accept_alert() DriverWrapper

Appium/Selenium only: Wait for an alert, switch to it, and click accept.

Returns:

DriverWrapper - The current instance of the driver wrapper.

property bottom_bar_height: int

iOS only - Get the height of the bottom bar.

Returns:

int - The height of the bottom bar in pixels.

clear_cookies() DriverWrapper

Delete all cookies in the current session.

Returns:

DriverWrapper - The current instance of the driver wrapper.

click_by_coordinates(x: int, y: int, silent: bool = False) DriverWrapper

Click at the specified coordinates on the screen.

Parameters:
  • x (int) – The x-axis coordinate to click at.

  • y (int) – The y-axis coordinate to click at.

  • silent (bool) – If True, suppresses the log message. Default is False.

Returns:

DriverWrapper - The current instance of the driver wrapper.

close_unused_tabs() DriverWrapper

Selenium/Playwright only: Close all tabs except the original.

Returns:

DriverWrapper - The current instance of the driver wrapper, with all tabs except the original closed.

create_new_tab() DriverWrapper

Selenium/Playwright only: Create a new tab and switch to it.

Returns:

DriverWrapper - The current instance of the driver wrapper, now switched to the new tab.

property current_url: str

Retrieve the current page URL.

Returns:

str - The URL of the current page.

Delete a cookie by name.

Returns:

DriverWrapper - The current instance of the driver wrapper.

dismiss_alert() DriverWrapper

Appium/Selenium only: Wait for an alert, switch to it, and click dismiss.

Returns:

DriverWrapper - The current instance of the driver wrapper.

evaluate(expression: str, arg: Any | None = None) Any

Playwright only: Synchronously executes JavaScript in the current window or frame.

Parameters:
  • expression (str) – The JavaScript code to execute.

  • arg (list) – Any arguments to pass to the JavaScript.

Returns:

typing.Any - The result of the JavaScript execution.

execute_script(script: str, *args: Any) Any

Execute JavaScript synchronously in the current window or frame. Compatible with Selenium’s execute_script method.

Parameters:
  • script (str) – The JavaScript code to execute.

  • args (typing.Any) – Any arguments to pass to the JavaScript.

Returns:

typing.Any - The result of the JavaScript execution.

get(url: str, silent: bool = False) DriverWrapper

Navigate to the given URL.

Parameters:
  • url (str) – The URL to navigate to.

  • silent (bool) – If True, suppresses logging.

Returns:

DriverWrapper - The current instance of the driver wrapper.

get_all_contexts() list[str]

Appium only: Get all contexts within the current session.

Returns:

A list of available context names.

Return type:

List[str]

get_all_tabs() list[str]

Selenium/Playwright only: Retrieve all opened tabs.

Returns:

A list of str, each representing an open tab.

Return type:

List[str]

get_cookies() list[dict]

Retrieve a list of cookie dictionaries corresponding to the cookies visible in the current session.

Returns:

A list of dictionaries, each containing cookie data.

Return type:

List[Dict]

get_current_context() str

Appium only: Get the current context name.

Returns:

str - The name of the current context.

get_inner_window_size() Size

Retrieve the inner window size (viewport) of the current browser context.

Returns:

The size of the inner window as a Size object.

get_web_view_context() None | str

Appium only: Get the WEBVIEW context name.

Returns:

None if no WEBVIEW context is found, otherwise the name of the WEBVIEW context.

Return type:

Union[None, str]

get_window_size() Size

Retrieve the outer window size of the current browser context.

Note

Playwright behaves differently in headless mode, where the reported window

size may not reflect the actual dimensions.

In contrast, Appium does not support retrieving the window size in the

same way as traditional web browsers.

Returns:

The size of the outer window as a Size object.

go_back() DriverWrapper

Navigate backward in the browser.

Returns:

DriverWrapper - The current instance of the driver wrapper.

go_forward() DriverWrapper

Navigate forward in the browser.

Returns:

DriverWrapper - The current instance of the driver wrapper.

hide_keyboard(**kwargs: Any) DriverWrapper

Appium only: Hide the keyboard on a real device.

Parameters:

kwargs – Additional arguments passed to the Keyboard.hide_keyboard method.

Returns:

DriverWrapper - The current instance of the driver wrapper.

is_app_closed() bool

Appium only: Check if the app is closed.

Returns:

bool - True if the app is closed, otherwise False.

is_app_deleted() bool

Appium only: Check if the app is deleted.

Returns:

bool - True if the app is deleted, otherwise False.

is_app_in_background() bool

Appium only: Check if the app is in the background.

Returns:

bool - True if the app is in the background, otherwise False.

is_app_in_foreground() bool

Appium only: Check if the app is in the foreground.

Returns:

bool - True if the app is in the foreground, otherwise False.

is_app_installed() bool

Appium only: Check if the app is running.

Returns:

bool - True if the app is running, otherwise False.

property is_chrome: bool

Returns True if the current driver is Chrome, otherwise False.

Returns:

bool- True if the current driver is Chrome, otherwise False.

is_driver_closed() bool

Check if the driver is closed.

Returns:

bool - True if the driver is closed, otherwise False.

is_driver_opened() bool

Check if the driver is open.

Returns:

bool - True if the driver is open, otherwise False.

property is_firefox: bool

Returns True if the current driver is Firefox, otherwise False.

Returns:

bool- True if the current driver is Firefox, otherwise False.

is_native_context() bool

Appium only: Check if the current context is native.

Returns:

bool - True if the current context is native, otherwise False.

property is_safari: bool

Returns True if the current driver is Safari, otherwise False.

Returns:

bool- True if the current driver is Safari, otherwise False.

is_web_context() bool

Appium only: Check if the current context is web.

Returns:

bool - True if the current context is web, otherwise False.

log(message: str, level: str = 'info') None

Log a message with detailed context in the following format:

# Format
[time][level][driver_index][module][function:line] <message>
# Example
[Aug 14][16:04:22.767][I][2_driver][play_element.py][is_displayed:328] Check visibility of "Mouse page"
Parameters:
  • message (str) – The log message to record.

  • level (str) – The logging level, which should be one of the values from LogLevel

Returns:

None

original_tab: str | PlaywrightPage | None = None
refresh() DriverWrapper

Reload the current page.

Returns:

DriverWrapper - The current instance of the driver wrapper.

remove_local_storage_item(key: str) DriverWrapper[source]

Remove a single item from localStorage by key.

Parameters:

key (str) – The key to remove.

Returns:

DriverWrapper - The current instance of the driver wrapper.

property screenshot_base: bytes

Returns the binary screenshot data of the element.

Returns:

bytes - screenshot binary

screenshot_image(screenshot_base: bytes | None = None) Image

Return a PIL.Image.Image object representing the screenshot of the web page. Appium iOS: Removes native controls from image manually

Parameters:

screenshot_base (bytes) – Screenshot binary data (optional). If None is provided then takes a new screenshot

Returns:

PIL.Image.Image

Add a list of cookie dictionaries to the current session.

Note: The domain should be in the format “.google.com” for a URL like “https://google.com/some/url/”.

Parameters:

cookies (List[dict]) – A list of dictionaries, each containing cookie data.

Returns:

DriverWrapper - The current instance of the driver wrapper.

set_page_load_timeout(timeout: int = 30) DriverWrapper

Set the maximum time to wait for a page load to complete before throwing an error.

Parameters:

timeout (int) – The timeout duration to set, in seconds.

Returns:

DriverWrapper - The current instance of the driver wrapper.

set_window_size(size: Size) DriverWrapper

Set the inner window size (viewport) of the current browser context.

Parameters:

size – The desired inner window size as a Size object.

Returns:

The current instance of DriverWrapper.

switch_to_alert(timeout: float = 10) Alert

Appium/Selenium only: Wait for an alert and switch to it.

Parameters:

timeout (Union[int, float]) – The time to wait for the alert to appear (in seconds).

Returns:

selenium.webdriver.common.alert.Alert - The alert object.

switch_to_default_content() DriverWrapper

Switch back to the default content from a frame.

Returns:

DriverWrapper - The current instance of the driver wrapper.

switch_to_frame(frame: Element) DriverWrapper

Switch to a specified frame.

Parameters:

frame (Element) – The frame element to switch to.

Returns:

DriverWrapper - The current instance of the driver wrapper.

switch_to_native() DriverWrapper

Appium only: Switch to the native app context.

Returns:

DriverWrapper - The current instance of the driver wrapper, now in the native app context.

switch_to_original_tab() DriverWrapper

Selenium/Playwright only: Switch back to the original tab.

Returns:

DriverWrapper - The current instance of the driver wrapper, now switched to the original tab.

switch_to_tab(tab: int = -1) DriverWrapper

Selenium/Playwright only: Switch to a specific tab.

Parameters:

tab (int) – The index of the tab to switch to, starting from 1. Default is the latest tab.

Returns:

DriverWrapper - The current instance of the driver wrapper, now switched to the specified tab.

switch_to_web() DriverWrapper

Appium only: Switch to the web app context.

Returns:

DriverWrapper - The current instance of the driver wrapper, now in the web app context.

terminate_app(bundle_id: str) bool

Appium only: Terminates the application if it is running.

Parameters:

bundle_id (str) – The application ID of the app to terminate.

Returns:

bool - True if the app has been successfully terminated, otherwise False.

property top_bar_height: int

iOS only - Get the height of the top bar.

Returns:

int - The height of the top bar in pixels.

wait(timeout: float = 1, reason: str = '') DriverWrapper

Pauses the execution for a specified amount of time.

Parameters:
  • timeout (Union[int, float]) – The time to sleep in seconds (can be an integer or float).

  • reason (str) – The waiting reason.

Returns:

DriverWrapper - The current instance of the driver wrapper.

remove_session_storage_item(key: str) DriverWrapper[source]

Remove a single item from sessionStorage by key.

Parameters:

key (str) – The key to remove.

Returns:

DriverWrapper - The current instance of the driver wrapper.

clear_local_storage() DriverWrapper[source]

Remove all items from localStorage.

Returns:

DriverWrapper - The current instance of the driver wrapper.

clear_session_storage() DriverWrapper[source]

Remove all items from sessionStorage.

Returns:

DriverWrapper - The current instance of the driver wrapper.