Script Examples

Script Examples #

Locating elements #

By using CSS selector

element = e(driver, "css", ".some-class")

By using xPath

element = e(driver, "xpath", "//some-element")

By using ID

element = e(driver, "id", "some-id")

By using class name

element = e(driver, "className", "some-class")

By using name

element = e(driver, "name", "some-name")
def test(driver: TestUIDriver):
    def move_to_element(element):
        # Moves element in view
        # param: element: TestUIElement
        element.get_element().location_once_scrolled_into_view

    testdevlab_title = "Experts in Software Quality Assurance Services | TestDevLab"

    driver.navigate_to("https://www.testdevlab.com/")

    # Verify page title    
    if driver.get_driver().title != testdevlab_title:
        driver.set_error("page title does not match expected")
        driver.raise_errors()

    # Take screenshot of landing page
    driver.save_screenshot("website_testdevlab.png")

    # Navigate to approach page
    e(
        driver, "css", "[class*='Menu_item']:nth-of-type(2)"
    ).wait_until_visible().click()

    # Take screenshot of approach page
    driver.save_screenshot("approach_page.png")

    time.sleep(5)

    move_to_element(
        e(driver, "css", "[class*='TestDevLab_comparison']")
    )

    # Take screenshot of TDL comparison with others
    driver.save_screenshot("testdevlab_comparison.png")
def test(driver: TestUIDriver):
    # Example of locating elements using CSS selector
    driver.navigate_to("https://www.bing.com")

    # Wait 10 seconds until page is visible and take a screenshot
    e(driver, "css", ".dimmer").wait_until_visible(seconds=10)
    driver.save_screenshot("website_bing.png")

    # Find search bar element and type Loadero into it
    e(driver, "css", "[type=search]").send_keys("Loadero")

    # Find search button and click it
    e(driver, "css", "#search_icon").click()

    # Wait 10 seconds until search results are visible and take a screenshot
    e(driver, "css", "[aria-label='Search Results']").wait_until_visible(seconds=10)
    driver.save_screenshot("search_results.png")

Using Py-TestUI Element Assertion methods #

element.
    (.no())
        .is_visible()
        .is_visible_in(seconds)
        .visible_for(seconds)
        .wait_until_visible(seconds, log)
        .wait_until_attribute(attr, text, seconds)
        .wait_until_contains_attribute(attr, text, seconds)
        .wait_until_contains_sensitive_attribute(attr, text, seconds, log)

You can find detailed descriptions and more examples in Py-TestUI wiki