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
- __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:
driver –
Driverobject that holds appium / selenium / playwright driver to initialize
- quit(silent: bool = False, trace_path: str = 'trace.zip') None[source]¶
Quit the driver instance.
Selenium/Appium:
- Parameters:
trace_path (str) – Compatibility argument for Playwright.
Playwright:
- 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:
- 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 &
Elementnameargument & 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
Elementduring 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]]) –
Elementto remove from the screenshot. Can be a single element or a list of elements.cut_box (Optional[Box]) – A
Boxspecifying a region to cut from the screenshot. IfNone, no region is cut.hide (Optional[Element or List[Element]]) –
Elementto hide in the screenshot. Can be a single element or a list of elements.
- Returns:
- 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 &
Elementnameargument & 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
Elementduring 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]]) –
Elementto remove from the screenshot.cut_box (Optional[Box]) – A
Boxspecifying a region to cut from the screenshot. IfNone, no region is cut.hide –
Elementto hide in the screenshot. Can be a single element or a list of elements.
- Returns:
- set_local_storage_item(items: list[dict]) DriverWrapper[source]¶
Set one or more items in localStorage.
Each dict must contain
keyandvaluefields.- Parameters:
- 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
keyandvaluefields.- Parameters:
- 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.
- get_session_storage_item(key: str) str | None[source]¶
Retrieve a single item from sessionStorage by key.
- 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:
- 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:
- 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.
- 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_cookie(name: str) DriverWrapper¶
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:
- 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:
- Returns:
DriverWrapper- The current instance of the driver wrapper.
- get_cookies() list[dict]¶
Retrieve a list of cookie dictionaries corresponding to the cookies visible in the current session.
- 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
Sizeobject.
- 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
Sizeobject.
- 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.
- 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"
- 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.Imageobject representing the screenshot of the web page. Appium iOS: Removes native controls from image manually
- set_cookie(cookies: list[dict]) DriverWrapper¶
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
Sizeobject.- 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.
- 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:
- 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.