diff --git a/dash/testing/wait.py b/dash/testing/wait.py index 202322e96b..d9bd8b07a9 100644 --- a/dash/testing/wait.py +++ b/dash/testing/wait.py @@ -1,5 +1,6 @@ # pylint: disable=too-few-public-methods """Utils methods for pytest-dash such wait_for wrappers.""" + import time import logging from selenium.common.exceptions import WebDriverException @@ -62,8 +63,9 @@ def __call__(self, driver): try: elem = driver.find_element(By.CSS_SELECTOR, self.selector) logger.debug("contains text {%s} => expected %s", elem.text, self.text) - return self.text in str(elem.text) or self.text in str( - elem.get_attribute("value") + value = elem.get_attribute("value") + return self.text in str(elem.text) or ( + value is not None and self.text in str(value) ) except WebDriverException: return False @@ -107,9 +109,9 @@ def __call__(self, driver): try: elem = self._get_element(driver) logger.debug("text to equal {%s} => expected %s", elem.text, self.text) - return ( - str(elem.text) == self.text - or str(elem.get_attribute("value")) == self.text + value = elem.get_attribute("value") + return str(elem.text) == self.text or ( + value is not None and str(value) == self.text ) except WebDriverException: return False diff --git a/tests/integration/test_duo.py b/tests/integration/test_duo.py index 9408c1c47c..fa5728b6ee 100644 --- a/tests/integration/test_duo.py +++ b/tests/integration/test_duo.py @@ -14,6 +14,11 @@ def test_duo001_wait_for_text_error(dash_duo): assert err.value.args[0] == "text -> Invalid not found within 1.0s, found: Content" + with pytest.raises(TimeoutException) as err: + dash_duo.wait_for_text_to_equal("#content", "None", timeout=1.0) + + assert err.value.args[0] == "text -> None not found within 1.0s, found: Content" + with pytest.raises(TimeoutException) as err: dash_duo.wait_for_text_to_equal("#none", "None", timeout=1.0) @@ -27,6 +32,14 @@ def test_duo001_wait_for_text_error(dash_duo): == "text -> invalid not found inside element within 1.0s, found: Content" ) + with pytest.raises(TimeoutException) as err: + dash_duo.wait_for_contains_text("#content", "None", timeout=1.0) + + assert ( + err.value.args[0] + == "text -> None not found inside element within 1.0s, found: Content" + ) + with pytest.raises(TimeoutException) as err: dash_duo.wait_for_contains_text("#none", "none", timeout=1.0) @@ -34,3 +47,18 @@ def test_duo001_wait_for_text_error(dash_duo): err.value.args[0] == "text -> none not found inside element within 1.0s, #none not found" ) + + +def test_duo002_wait_for_text_value(dash_duo): + app = Dash(__name__) + app.layout = html.Div([html.Ol([html.Li("Item", id="value-item", value="100")])]) + dash_duo.start_server(app) + + dash_duo.wait_for_text_to_equal("#value-item", "100") + with pytest.raises(TimeoutException) as err: + dash_duo.wait_for_contains_text("#value-item", "None", timeout=1.0) + + assert ( + err.value.args[0] + == "text -> None not found inside element within 1.0s, found: Item" + )