Wait For Download Finished

Wait For Download Finished #

def wait_for_download_finished(
    driver: TestUIDriver,
    file_name: str,
    timeout: int = 1000
) -> None

This command waits until file download is completed or timeout is exceeded. This function is useful if download has to be completed before the end of the test. Download completion check is done every 500 ms, so it can be used for crude download time measurement as well.

The file_name parameter specifies file name to wait for in downloads directory. This parameter can not be None or empty, otherwise an Error will be thrown.

The timeout parameter specifies time in ms, how long to wait for file download to be completed. If no timeout parameter is provided, default value 1000 ms is used. When timeout is exceeded the function will raise an Py-TestUI error but test execution will continue.

def test(driver: TestUIDriver):
    # Example of waiting for file to be downloaded

    def move_to_element(element):
        # Moves element in view
        # param: element: TestUIElement
        element.get_element().location_once_scrolled_into_view

    # Open TestDevLab press page
    driver.navigate_to("https://www.testdevlab.com/newsroom")
    e(driver, "css", "body").wait_until_visible()

    driver.save_screenshot("testdevlab_press_page.png")

    download_element = e(driver, "xpath", "//div[contains(text(), 'Visuals')]")
    
    # Get download item in view
    move_to_element(download_element)

    driver.save_screenshot("before_download.png")

    # Start download
    download_element.click()
    wait_for_download_finished(driver, "Assets.zip", 30 * 1000)

    driver.save_screenshot("after_download.png")