Skip to content

Commit 910a9c3

Browse files
committed
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.
1 parent 9f938ae commit 910a9c3

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

py/test/selenium/webdriver/common/devtools_tests.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pytest
1818

1919
from selenium.webdriver.support.ui import WebDriverWait
20-
20+
from selenium.common.exceptions import TimeoutException
2121

2222
@pytest.mark.xfail_safari
2323
@pytest.mark.xfail_firefox
@@ -32,9 +32,21 @@ def test_check_console_messages(driver, pages, recwarn):
3232
connection.on(devtools.runtime.ConsoleAPICalled, console_api_calls.append)
3333
driver.execute_script("console.log('I love cheese')")
3434
driver.execute_script("console.error('I love bread')")
35-
WebDriverWait(driver, 10).until(lambda _: len(console_api_calls) == 2)
3635

37-
assert console_api_calls[0].type_ == "log"
38-
assert console_api_calls[0].args[0].value == "I love cheese"
39-
assert console_api_calls[1].type_ == "error"
40-
assert console_api_calls[1].args[0].value == "I love bread"
36+
# Throw an exception if the api is not reached in time.
37+
try:
38+
WebDriverWait(driver, 20).until(lambda _: len(console_api_calls) == 2)
39+
except TimeoutException:
40+
pytest.fail("Console API calls did not reach expected count within timeout.")
41+
42+
# Retry assertions to handle failures.
43+
for _ in range(3):
44+
try:
45+
assert console_api_calls[0].type_ == "log"
46+
assert console_api_calls[0].args[0].value == "I love cheese"
47+
assert console_api_calls[1].type_ == "error"
48+
assert console_api_calls[1].args[0].value == "I love bread"
49+
break
50+
except AssertionError:
51+
if _ == 2:
52+
pytest.fail("Assertions failed after retries.")

py/test/selenium/webdriver/common/implicit_waits_tests.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import pytest
1919

20-
from selenium.common.exceptions import NoSuchElementException
20+
from selenium.webdriver.support.ui import WebDriverWait
21+
from selenium.webdriver.support import expected_conditions as EXPECTED
22+
from selenium.common.exceptions import TimeoutException, NoSuchElementException
2123
from selenium.webdriver.common.by import By
2224

2325

@@ -26,7 +28,10 @@ def test_should_implicitly_wait_for_asingle_element(driver, pages):
2628
add = driver.find_element(By.ID, "adder")
2729
driver.implicitly_wait(3)
2830
add.click()
29-
driver.find_element(By.ID, "box0") # All is well if this doesn't throw.
31+
try:
32+
WebDriverWait(driver, 5).until(EXPECTED.presence_of_element_located((By.ID, "box0")))
33+
except TimeoutException:
34+
pytest.fail("Element 'box0' was not found within the timeout.")
3035

3136

3237
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
5257
add.click()
5358
add.click()
5459

55-
elements = driver.find_elements(By.CLASS_NAME, "redbox")
56-
assert len(elements) >= 1
60+
try:
61+
WebDriverWait(driver, 5).until(lambda driver: len(driver.find_elements(By.CLASS_NAME, "redbox")) >= 1)
62+
except TimeoutException:
63+
pytest.fail("Expected elements were not found within the timeout.")
5764

5865

5966
def test_should_still_fail_to_find_an_elemenst_when_implicit_waits_are_enabled(driver, pages):

0 commit comments

Comments
 (0)