Skip to main content

Selenium

Tesmon supports Selenium integration for end-to-end testing, allowing you to automate browser interactions seamlessly.

info

In Tesmon scripts, the driver keyword holds special significance. When you name your WebDriver instance driver, Tesmon automatically preserves the state of this driver throughout the Test Run, unless it is explicitly closed or reinitialized. This functionality is particularly valuable for tests where the browser needs to stay open and retain its state across various steps.

Tesmon Selenium

We have developed a custom layer on top of Selenium to simplify its usage, yet you can still utilize any standard Selenium syntax.

import tesmon_utils.selenium as tm

driver = tm.create_driver(1211, 944, headless=True)
driver.get("https://www.tesmon.io/")

Functions

Prints "hello world" to the console.

create_driver(width, height, *, webdriver_type=webdriver.Chrome, headless=False, remote_url=None)

Creates a WebDriver with specified dimensions and optional headless mode.

  • Parameters:
    • width (int): The width of the browser window.
    • height (int): The height of the browser window.
    • webdriver_type (WebDriver, optional): Specifies the type of WebDriver. Defaults to webdriver.Chrome.
    • headless (bool, optional): If set to True, the browser is run in headless mode.
    • remote_url (string, optional): Specifies the URL for the Selenium server

create_action_chains(driver)

Creates an instance of ActionChains for the provided driver.

  • Parameters:
    • driver (WebDriver): The WebDriver instance for which to create ActionChains.

click(driver, selector, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Clicks on an element specified by the selector.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element to click.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before attempting the click. Defaults to 0.

mouse_over(driver, selector, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Moves the mouse over an element specified by the selector.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element to hover over.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before performing the mouse over. Defaults to 0.

send_enter_key(driver, selector, *, selector_type=By.CSS_SELECTOR, sleep=0)

Sends an ENTER key press to an element specified by the selector.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • sleep (int, optional): The time in seconds to wait before sending the key. Defaults to 0.

scroll_to(driver, scroll_X, scroll_Y, *, sleep=0)

Scrolls the browser window to the specified coordinates.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • scroll_X (int): The horizontal coordinate to scroll to.
    • scroll_Y (int): The vertical coordinate to scroll to.
    • sleep (int, optional): The time in seconds to wait before scrolling. Defaults to 0.

set_window_size(driver, width, height, *, sleep=0)

Sets the size of the browser window.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • width (int): The desired width of the window.
    • height (int): The desired height of the window.
    • sleep (int, optional): The time in seconds to wait before resizing. Defaults to 0.

send_input(driver, selector, input, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Sends input to an element specified by the selector.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element.
    • input (str): The input to send to the element.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before sending the input. Defaults to 0.

testbot_assert_text(driver, selector, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Asserts the text of an element specified by the selector.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before performing the assert. Defaults to 0.

assert_text_equals(driver, selector, expected_text, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Asserts that the text of an element specified by the selector equals the expected text.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element.
    • expected_text (str): The expected text to compare against.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before performing the assert. Defaults to 0.

assert_element_displayed(driver, selector, *, selector_type=By.CSS_SELECTOR, timeout=30, sleep=0)

Asserts that an element specified by the selector is displayed.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • selector (str): The CSS selector of the element.
    • selector_type (By, optional): The type of selector to use. Defaults to By.CSS_SELECTOR.
    • timeout (int, optional): The time in seconds before timing out. Defaults to 30.
    • sleep (int, optional): The time in seconds to wait before performing the assert. Defaults to 0.

get_screenshot(driver, *, sleep=0)

Captures a screenshot of the current state of the browser window.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • sleep (int, optional): The time in seconds to wait before taking the screenshot. Defaults to 0.

get_selection_screenshot(driver, top, left, width, height, inner_width, *, sleep=0)

Captures a screenshot of a specified selection area within the current browser window.

  • Parameters:

    • driver (WebDriver): The WebDriver instance controlling the browser.
    • top (int): The top coordinate of the screenshot area relative to the page.
    • left (int): The left coordinate of the screenshot area relative to the page.
    • width (int): The width of the screenshot area.
    • height (int): The height of the screenshot area.
    • innerWidth (int): The inner width of the browser's viewport.
    • sleep (int, optional): The time in seconds to wait before taking the screenshot. Defaults to 0.
  • Returns:

    • A tuple containing:
      • screenshot (str): A base64-encoded string of the screenshot.
      • metadata (dict): A dictionary containing metadata about the screenshot dimensions and type.

set_session_data(driver, session_data, session_type, *, sleep=0)

Sets session data for the specified session type.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • session_data (dict): The session data to set.
    • session_type (str): The type of session storage to use (localStorage, sessionStorage, or cookies).
    • sleep (int, optional): The time in seconds to wait before setting the session data. Defaults to 0.

open_new_tab(driver, *, sleep=0)

Opens a new browser tab and switches to it.

  • Parameters:
    • driver (WebDriver): The WebDriver instance.
    • sleep (int, optional): The time in seconds to wait before opening the new tab. Defaults to 0.

Example (Local Machine)

tasks:
- action: |-
import tesmon_utils.selenium as tm

driver = tm.create_driver(1211, 944, headless=True)
driver.get("https://www.tesmon.io/")
- action: screenshot = tm.get_screenshot(driver)
- action: screenshot, metadata = tm.get_selection_screenshot(driver, "256", "473.78125", "229.4375", "54.5", "1177")
- action: result = tm.testbot_assert_text(driver, ".px-5 > .font-heading")
- action: tm.click(driver, ".navbar__item:nth-child(4)")
- action: tm.assert_text_equals(driver, ".pricing_bOlL:nth-child(1) .flex > .text-4xl", "Pricing for Teams of all sizes")
- action: tm.click(driver, ".navbar__item > .bg-\\[\\#07c\\]")
- action: tm.assert_element_displayed(driver, ".px-6:nth-child(2) > .flex")
- action: tm.scroll_to(driver, 0, 909.5)
- action: tm.assert_element_displayed(driver, ".flex-col:nth-child(3) .footer__item:nth-child(1) > a")

Example (Selenium Server)

tasks:
- action: |-
import tesmon_utils.selenium as tm

driver = tm.create_driver(1211, 944, headless=True, remote_url="http://selenium:4444")
driver.get("https://www.tesmon.io/")
- action: screenshot = tm.get_screenshot(driver)