Skip to content

Commit 147913a

Browse files
committed
[py] support for custom error messages through functions for WebDriverWait.until/until_not
1 parent 2534420 commit 147913a

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

py/selenium/webdriver/support/wait.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
import time
19+
from typing import Any
1920
from typing import Callable
2021
from typing import Generic
2122
from typing import Literal
@@ -92,7 +93,9 @@ def __init__(
9293
def __repr__(self) -> str:
9394
return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'
9495

95-
def until(self, method: Callable[[D], Union[Literal[False], T]], message: str = "") -> T:
96+
def until(
97+
self, method: Callable[[D], Union[Literal[False], T]], message: Union[str, Callable[[Any], str]] = ""
98+
) -> T:
9699
"""Wait until the method returns a value that is not False.
97100
98101
Calls the method provided with the driver as an argument until the
@@ -103,7 +106,7 @@ def until(self, method: Callable[[D], Union[Literal[False], T]], message: str =
103106
method: callable(WebDriver)
104107
- A callable object that takes a WebDriver instance as an argument.
105108
106-
message: str
109+
message: Union[str, Callable[[Any], str]]
107110
- Optional message for :exc:`TimeoutException`
108111
109112
Return:
@@ -143,9 +146,13 @@ def until(self, method: Callable[[D], Union[Literal[False], T]], message: str =
143146
if time.monotonic() > end_time:
144147
break
145148
time.sleep(self._poll)
146-
raise TimeoutException(message, screen, stacktrace)
147149

148-
def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Literal[True]]:
150+
final_msg = message() if callable(message) else message
151+
raise TimeoutException(final_msg, screen, stacktrace)
152+
153+
def until_not(
154+
self, method: Callable[[D], T], message: Union[str, Callable[[Any], str]] = ""
155+
) -> Union[T, Literal[True]]:
149156
"""Wait until the method returns a value that is not False.
150157
151158
Calls the method provided with the driver as an argument until the
@@ -156,7 +163,7 @@ def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Lit
156163
method: callable(WebDriver)
157164
- A callable object that takes a WebDriver instance as an argument.
158165
159-
message: str
166+
message: Union[str, Callable[[Any], str]]
160167
- Optional message for :exc:`TimeoutException`
161168
162169
Return:
@@ -192,4 +199,5 @@ def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Lit
192199
if time.monotonic() > end_time:
193200
break
194201
time.sleep(self._poll)
195-
raise TimeoutException(message)
202+
final_msg = message() if callable(message) else message
203+
raise TimeoutException(final_msg)

py/test/selenium/webdriver/common/webdriverwait_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ def test_should_still_fail_to_find_an_element_with_explicit_wait(driver, pages):
5858
WebDriverWait(driver, 0.01).until(EC.presence_of_element_located((By.ID, "box0")))
5959

6060

61+
def test_should_still_fail_to_find_an_element_with_explicit_wait_with_custom_timeout_messages(driver, pages):
62+
pages.load("dynamic.html")
63+
with pytest.raises(TimeoutException) as exception:
64+
WebDriverWait(driver, 0.01).until(
65+
EC.presence_of_element_located((By.ID, "box0")), message=lambda: "custom timeout message"
66+
)
67+
assert "custom timeout message" in str(exception.value)
68+
69+
with pytest.raises(TimeoutException) as exception:
70+
WebDriverWait(driver, 0.01).until(
71+
EC.presence_of_element_located((By.ID, "box0")), message="custom timeout message"
72+
)
73+
assert "custom timeout message" in str(exception.value)
74+
75+
6176
def test_should_explicitly_wait_until_at_least_one_element_is_found_when_searching_for_many(driver, pages):
6277
pages.load("dynamic.html")
6378
add = driver.find_element(By.ID, "adder")

0 commit comments

Comments
 (0)