Selenium
Core classes for interacting with the web driver and appium in general.
Selenium operations
- class selenium_operations.SeleniumOperations(driver)
- __init__(driver)
Initialize the class with a webdriver instance
- Parameters:
driver (WebDriver) – Selenium WebDriver instance
- _bool_click(element)
Internal helper that attempts element click and returns success status
- Parameters:
element – Element to click
- Returns:
True if click succeeded, False otherwise
- Return type:
Boolean
- _swipe_android(offset)
Executes a swipe gesture for Android devices
- Parameters:
offset (dict) – Dictionary specifying swipe coordinates with start_x, start_y, end_x, end_y values expressed as percentages (0-1) of viewport dimensions. For instance, start_y of 0.25 begins the swipe at 25% of the viewport height
- _swipe_ios(offset)
Executes a swipe gesture for iOS devices
- Parameters:
offset (dict) – Dictionary specifying swipe coordinates with start_x, start_y, end_x, end_y values expressed as percentages (0-1) of viewport dimensions. For instance, start_y of 0.25 begins the swipe at 25% of the viewport height
- _text_to_be_present_in_elements(locator, text)
Internal method that searches through elements until finding one containing the specified text
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
text (str) – Text content to search for within elements
- browser_tap(locator, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Performs a tap action on an element in mobile browser context :param locator: Tuple containing By strategy and selector string, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
- Parameters:
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to
settings.SELENIUM['TIMEOUT']
- drag_and_drop(source, target)
Executes a drag-and-drop operation by pressing the mouse button on the source element, moving to the target element, and releasing.
- Parameters:
source (WebElement) – Element where the drag begins.
target (WebElement) – Element where the drop occurs.
- Return None:
- drag_and_drop_by_offset(source, xoffset, yoffset)
Executes a drag-and-drop operation by pressing the mouse button on the source element, moving by the specified offset, and releasing.
- Parameters:
source (WebElement) – Element where the drag begins.
xoffset (Int) – Horizontal distance to move.
yoffset (Int) – Vertical distance to move.
- driver = None
Driver reference for web automation
- get_current_url()
Returns the current URL using JavaScript, which doesn’t wait for page load. Useful when you need to check the URL of a slow-loading page immediately.
- Returns:
Current page URL
- Return type:
str
- get_element(locator, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Locates and returns a web element after confirming its presence in the DOM
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to
settings.SELENIUM['TIMEOUT']
- Returns:
Located web element
- Return type:
WebElement
- get_elements(locator)
Locates and returns multiple web elements that match the specified criteria. Waits until at least one element is found in the DOM.
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
- Returns:
Collection of matching web elements
- Return type:
list
- swipe(offset=None)
Executes a swipe gesture based on the provided offset parameters
- Parameters:
offset (dict) – Dictionary specifying swipe coordinates with start_x, start_y, end_x, end_y values expressed as percentages (0-1) of viewport dimensions. For instance, start_y of 0.25 begins the swipe at 25% of the viewport height
- swipe_until_visible(locator, attempts=3)
Repeatedly swipes in the specified direction until the target element becomes visible. Does not include implicit wait, assumes element is already loaded but not in viewport.
- Parameters:
locator (Tuple) – Tuple containing locator strategy and selector
attempts (int) – Maximum number of swipe-and-check cycles to perform
- Returns:
Located web element
- Return type:
WebElement
- switch_to_new_window(original_window)
Switches to a newly opened browser window/tab, excluding the original window.
- Parameters:
original_window (str) – Handle of the original window to exclude from switching
- Returns:
Handle of the new window that was switched to
- Return type:
str
- tap(locator)
Performs a reliable click operation on an element, retrying until successful. Uses WebDriverWait and handles StaleElementReferenceException and NoSuchElementException
- Parameters:
locator (tuple) – Tuple containing locator strategy and value
- tap_android_button(button_text)
Clicks an Android button based on its text content Uses xpath expression:
//android.widget.Button[@text="{BUTTON_TEXT}"]Note: When multiple buttons share the same text, only the first match will be clicked- Parameters:
button_text – Text displayed on the button
- tap_element_at(web_element, x_offset, y_offset)
Performs a tap action at specific coordinates within an element’s boundaries
- Parameters:
web_element (WebElement) – Target element for the tap action
x_offset (int) – Horizontal position within element, where 0 is left edge and 1 is right edge
y_offset (int) – Vertical position within element, where 0 is top edge and 1 is bottom edge
- try_fetch(function)
Attempts to execute the provided function for element retrieval. Specifically designed for element fetching operations, catching only
NoSuchElementException. Useful when checking for optional elements that may or may not exist. Does not include implicit wait behavior. All other exceptions will be raised normally. Provide a function reference as the parameter.
- wait_for(method, expected_result, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Continuously evaluates the provided method until its result matches the expected value.
- Parameters:
method (func) – Function reference to evaluate
expected_result (bool) – Target value that the method should return, should be boolean-compatible
timeout (Int) – Maximum wait time in seconds for condition to be met
- Returns:
- wait_for_element_to_be_visible(locator, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Waits until the specified element is visible in the viewport
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to
settings.SELENIUM['TIMEOUT']
- wait_for_element_to_not_be_visible(locator, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Waits until the specified element is no longer visible in the viewport
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to
settings.SELENIUM['TIMEOUT']
- wait_for_text_in_elements(locator, text)
Waits until any element matching the locator contains the specified text content. Ignores stale element exceptions during the wait.
- Parameters:
locator (tuple) –
Tuple containing locator strategy and value, for example:
(By.XPATH, '//android.widget.Button[@resource-id="oaapprove"]')
text (str) – Text content to search for within elements
- Returns:
None
Web element operations
- class web_element_operations.WebElementOperations(controller, driver)
Collection of utility methods for simplified web element interactions. Reduces boilerplate code when working with elements, eliminating the need for custom controller methods for common operations.
- __init__(controller, driver)
Initialize the operations helper
- _scroll_into_view(element)
Scrolls an element into the visible center of the viewport. Only applies on desktop web platforms (Chrome/Firefox).
- Parameters:
element (WebElement) – WebElement to scroll into view
- _wait_for_dropdown_option(dropdown_element, match_by, match_value, timeout)
Waits until a specific option is present inside a <select> element. Handles dropdowns whose options are populated asynchronously.
- Parameters:
dropdown_element – The <select> WebElement
match_by – ‘text’ to match by visible text, ‘value’ to match by value attribute
match_value – The text or value to look for
timeout – Maximum wait time in seconds
- controller = None
- op_browser_tap(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Performs a tap action on the element (browser-specific implementation)
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- op_click_element(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Locates and clicks the element identified by locator_key
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- Returns:
- op_get_element(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Locates and returns the element identified by locator_key
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- Returns:
Located web element
- Return type:
WebElement
- op_get_element_enabled(locator_key)
Checks whether the element identified by locator_key is currently enabled
- Parameters:
locator_key – Dictionary key for the locators collection
- Returns:
True when element is enabled, False otherwise
- Return type:
bool
- op_get_element_text(locator_key)
Retrieves the text content from the element identified by locator_key
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- Returns:
Text content of the element
- Return type:
str
- op_get_element_value(locator_key)
Extracts the value attribute from the element identified by locator_key
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- Returns:
Value attribute of the element
- Return type:
str
- op_get_element_visibility(locator_key)
Checks whether the element identified by locator_key is currently visible
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- Returns:
True when element is visible, False otherwise
- Return type:
bool
- op_get_elements(locator_key)
Locates and returns all elements matching the specified locator_key
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- Returns:
Collection of matching web elements
- Return type:
list
- op_get_selected_dropdown_text(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Gets the visible text of the currently selected option in a dropdown.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- Returns:
Visible text of the selected option
- Return type:
str
- op_is_checkbox_checked(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Checks whether a checkbox is currently checked.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- Returns:
True if checkbox is checked, False otherwise
- Return type:
bool
- op_scroll_to_element(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Scrolls the page until the element identified by locator_key is visible in the viewport. Uses JavaScript scrollIntoView for desktop web browsers.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- Returns:
Located web element after scrolling
- Return type:
WebElement
- op_select_dropdown_by_text(locator_key, text, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Selects an option from a dropdown (select element) by visible text.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
text (str) – Visible text of the option to select
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- op_select_dropdown_by_value(locator_key, value, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Selects an option from a dropdown (select element) by value attribute.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
value (str) – Value attribute of the option to select
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- op_set_checkbox(locator_key, checked=True, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Sets a checkbox to checked or unchecked state.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
checked (bool) – Desired state - True for checked, False for unchecked
timeout (int) – Maximum wait time in seconds to find the element, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- op_swipe_until_visible(locator_key, attempts=3)
Repeatedly swipes until the target element becomes visible. Does not include implicit wait, assumes element is already loaded but not in viewport.
- Parameters:
locator_key (str) – Dictionary key for the locators collection
attempts (int) – Maximum number of swipe-and-check cycles to perform
- Returns:
Located web element
- Return type:
WebElement
- op_wait_for_element_to_be_visible(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Waits until the element is visible in the viewport
- Parameters:
locator_key (str) – Dictionary key for the locators collection
timeout (int) – Maximum wait time in seconds before raising an exception, defaults to settings.SELENIUM[‘TIMEOUT’] from settings.py
- op_wait_for_element_to_not_be_visible(locator_key, timeout=<MagicMock name='mock.SELENIUM.__getitem__()' id='140366350619152'>)
Waits until the element is no longer visible in the viewport
- Parameters:
locator_key (str) – Dictionary key for the locators collection
- op_wait_for_text_in_elements(locator_key, text)
Waits until elements matching the locator contain the specified text
- Parameters:
locator_key (str) – Dictionary key for the locators collection
text (str) – Text content to wait for