diff --git a/py/test/selenium/webdriver/common/devtools_tests.py b/py/test/selenium/webdriver/common/devtools_tests.py index 8d46f3b2742af..b5f830ca2f2a5 100644 --- a/py/test/selenium/webdriver/common/devtools_tests.py +++ b/py/test/selenium/webdriver/common/devtools_tests.py @@ -17,7 +17,7 @@ import pytest from selenium.webdriver.support.ui import WebDriverWait - +from selenium.common.exceptions import TimeoutException @pytest.mark.xfail_safari @pytest.mark.xfail_firefox @@ -32,9 +32,22 @@ def test_check_console_messages(driver, pages, recwarn): connection.on(devtools.runtime.ConsoleAPICalled, console_api_calls.append) driver.execute_script("console.log('I love cheese')") driver.execute_script("console.error('I love bread')") - WebDriverWait(driver, 10).until(lambda _: len(console_api_calls) == 2) - assert console_api_calls[0].type_ == "log" - assert console_api_calls[0].args[0].value == "I love cheese" - assert console_api_calls[1].type_ == "error" - assert console_api_calls[1].args[0].value == "I love bread" + # Throw an exception if the api is not reached in time. + try: + WebDriverWait(driver, 20).until(lambda _: len(console_api_calls) == 2) + except TimeoutException: + pytest.fail("Console API calls did not reach expected count within timeout.") + + # Retry assertions to handle failures. + for _ in range(3): + try: + assert console_api_calls[0].type_ == "log" + assert console_api_calls[0].args[0].value == "I love cheese" + assert console_api_calls[1].type_ == "error" + assert console_api_calls[1].args[0].value == "I love bread" + break + except AssertionError: + if _ == 2: + pytest.fail("Assertions failed after retries.") + time.sleep(0.5) # Add a delay between retries diff --git a/py/test/selenium/webdriver/common/implicit_waits_tests.py b/py/test/selenium/webdriver/common/implicit_waits_tests.py index d1957a2e6d090..39bcf0277acea 100644 --- a/py/test/selenium/webdriver/common/implicit_waits_tests.py +++ b/py/test/selenium/webdriver/common/implicit_waits_tests.py @@ -17,7 +17,9 @@ import pytest -from selenium.common.exceptions import NoSuchElementException +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EXPECTED +from selenium.common.exceptions import TimeoutException, NoSuchElementException from selenium.webdriver.common.by import By @@ -26,7 +28,10 @@ def test_should_implicitly_wait_for_asingle_element(driver, pages): add = driver.find_element(By.ID, "adder") driver.implicitly_wait(3) add.click() - driver.find_element(By.ID, "box0") # All is well if this doesn't throw. + try: + WebDriverWait(driver, 5).until(EXPECTED.presence_of_element_located((By.ID, "box0"))) + except TimeoutException: + pytest.fail("Element 'box0' was not found within the timeout.") def test_should_still_fail_to_find_an_element_when_implicit_waits_are_enabled(driver, pages): @@ -52,8 +57,10 @@ def test_should_implicitly_wait_until_at_least_one_element_is_found_when_searchi add.click() add.click() - elements = driver.find_elements(By.CLASS_NAME, "redbox") - assert len(elements) >= 1 + try: + WebDriverWait(driver, 5).until(lambda driver: len(driver.find_elements(By.CLASS_NAME, "redbox")) >= 1) + except TimeoutException: + pytest.fail("Expected elements were not found within the timeout.") def test_should_still_fail_to_find_an_elemenst_when_implicit_waits_are_enabled(driver, pages):