Skip to main content

Playwright

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

info

In Tesmon scripts, the browser and page keywords hold special significance. When you name your instance browser or page, Tesmon automatically preserves the state of this instance 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.

danger

We recommend using Playwright async APIs because if sync APIs are not closed explicitly or exceptions are not handled correctly, the asyncio loop will not get closed. This can cause future sync API runs to fail and will require a restart of the Tesmon Desktop or the Test Run Engine in cloud environments.

Tesmon Playwright

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

import tesmon_utils.async_playwright as tm

browser = tm.start_playwright()

page = tm.goto(browser, "https://tesmon.io")
tm.close(browser)

Functions

start_playwright(headless=False)

Starts a Playwright instance with specified dimensions and headless option.

  • Parameters:
    • headless (bool, optional): Whether to run the browser in headless mode. Defaults to False.

goto(browser, url, width, height)

Navigates to a specified URL in a new browser context and page.

  • Parameters:
    • browser (Browser): The Playwright browser instance.
    • url (str): The URL to navigate to.
    • width (int): The width of the browser window.
    • height (int): The height of the browser window.

click(page, selector)

Performs a click action on a specified element.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to click.

mouse_over(page, selector, *, sleep=0)

Hovers the mouse over a specified element.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to hover over.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

send_enter_key(page, selector, *, sleep=0)

Sends an Enter key press to a specified element.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to send the Enter key press to.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

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

Scrolls the page to the specified coordinates.

  • Parameters:
    • page (Page): The Playwright page instance.
    • scroll_X (int): The horizontal scroll position.
    • scroll_Y (int): The vertical scroll position.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

element_scroll_to(page, selector, scroll_X, scroll_Y, *, sleep=0)

Scrolls a specified element to the given coordinates.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to scroll.
    • scroll_X (int): The horizontal scroll position.
    • scroll_Y (int): The vertical scroll position.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

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

Sets the browser window to the specified size.

  • Parameters:
    • page (Page): The Playwright page instance.
    • width (int): The width of the browser window.
    • height (int): The height of the browser window.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

send_input(page, selector, input, *, sleep=0)

Sends input to a specified element.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to send input to.
    • input (str): The input text to send.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

testbot_assert_text(page, selector, *, sleep=0)

Asserts the text content of a specified element.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to assert the text content.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

assert_text_equals(page, selector, expected_text, *, sleep=0)

Asserts that the text content of a specified element equals the expected text.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to assert the text content.
    • expected_text (str): The expected text content.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

assert_element_displayed(page, selector, *, sleep=0)

Asserts that a specified element is visible.

  • Parameters:
    • page (Page): The Playwright page instance.
    • selector (str): The selector of the element to assert visibility.
    • sleep (int, optional): The time in seconds to wait before performing the action. Defaults to 0.

get_screenshot(page, *, sleep=0)

Takes a screenshot of the current page and returns it as a base64-encoded string.

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

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

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

  • Parameters:

    • page (Page): The Playwright Page 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.

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

Takes a screenshot of a specified area of the page.

  • Parameters:
    • page (Page): The Playwright page instance.
    • top (int): The top coordinate of the area.
    • left (int): The left coordinate of the area.
    • width (int): The width of the area.
    • height (int): The height of the area.
    • inner_width (int): The inner width of the area.
    • sleep (int, optional): The time in seconds to wait before taking the screenshot. Defaults to 0.

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

Sets session data for the specified session type.

  • Parameters:
    • page (Page): The Playwright page 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(page, *, sleep=0)

Opens a new browser tab and switches to it.

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

close(browser, *, sleep=0)

Closes the browser instance.

  • Parameters:
    • browser (Browser): The Playwright browser instance.
    • sleep (int, optional): The time in seconds to wait before closing the browser. Defaults to 0.

Example Test

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

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