From 910a9c32f04d9a246cf4fbfab54ac418202243e7 Mon Sep 17 00:00:00 2001 From: Bradley Latreille Date: Wed, 28 May 2025 21:57:58 -0400 Subject: [PATCH 1/2] Help Make Bazel Python Tests More Robust - Added WebDriverWaits to any dynamic elements to ensure they are loaded before interacting with them. - Increase some implicit waits to handle slower environments to hopefully prevent as many rebuilds. - Made use of pytest.fail to output clearer failure reporting when elements arent found within the timeout. - Added some retry mechanisms such as loops to retry a few times before failing. --- .../webdriver/common/devtools_tests.py | 24 ++++++++++++++----- .../webdriver/common/implicit_waits_tests.py | 15 ++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/py/test/selenium/webdriver/common/devtools_tests.py b/py/test/selenium/webdriver/common/devtools_tests.py index 8d46f3b2742af..77981e4716e1b 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,21 @@ 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.") 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): From 9044cac5fb083d026b7d4947976898d2e873cea9 Mon Sep 17 00:00:00 2001 From: Bradley Latreille Date: Wed, 28 May 2025 22:09:36 -0400 Subject: [PATCH 2/2] Take Bot Suggestions to Improve Tests - Implement the PR bots suggestions to add a sleep between retries. --- py/test/selenium/webdriver/common/devtools_tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/py/test/selenium/webdriver/common/devtools_tests.py b/py/test/selenium/webdriver/common/devtools_tests.py index 77981e4716e1b..b5f830ca2f2a5 100644 --- a/py/test/selenium/webdriver/common/devtools_tests.py +++ b/py/test/selenium/webdriver/common/devtools_tests.py @@ -50,3 +50,4 @@ def test_check_console_messages(driver, pages, recwarn): except AssertionError: if _ == 2: pytest.fail("Assertions failed after retries.") + time.sleep(0.5) # Add a delay between retries