Skip to content

Help Make Bazel Python Tests More Robust #15813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions py/test/selenium/webdriver/common/devtools_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
15 changes: 11 additions & 4 deletions py/test/selenium/webdriver/common/implicit_waits_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand All @@ -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):
Expand Down
Loading