Skip to content

Commit 16f81f4

Browse files
author
Lachlan Teale
committed
Added wait_for_class_to_equal
1 parent cebad07 commit 16f81f4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

dash/testing/browser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
MoveTargetOutOfBoundsException,
2222
)
2323

24-
from dash.testing.wait import text_to_equal, style_to_equal, contains_text, until
24+
from dash.testing.wait import (
25+
text_to_equal,
26+
style_to_equal,
27+
class_to_equal,
28+
contains_text,
29+
until,
30+
)
2531
from dash.testing.dash_page import DashPageMixin
2632
from dash.testing.errors import DashAppLoadingError, BrowserError, TestingTimeoutError
2733
from dash.testing.consts import SELENIUM_GRID_DEFAULT
@@ -301,6 +307,17 @@ def wait_for_element_by_id(self, element_id, timeout=None):
301307
f"timeout {timeout or self._wait_timeout}s => waiting for element id {element_id}",
302308
)
303309

310+
def wait_for_class_to_equal(self, selector, classname, timeout=None):
311+
"""Explicit wait until the element's class has expected `value` timeout
312+
if not set, equals to the fixture's `wait_timeout` shortcut to
313+
`WebDriverWait` with customized `class_to_equal` condition."""
314+
return self._wait_for(
315+
method=class_to_equal,
316+
args=(selector, classname),
317+
timeout=timeout,
318+
msg=f"classname => {classname} not found within {timeout or self._wait_timeout}s",
319+
)
320+
304321
def wait_for_style_to_equal(self, selector, style, val, timeout=None):
305322
"""Explicit wait until the element's style has expected `value` timeout
306323
if not set, equals to the fixture's `wait_timeout` shortcut to

dash/testing/wait.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,20 @@ def __call__(self, driver):
9999
return val == self.val
100100
except WebDriverException:
101101
return False
102+
103+
104+
class class_to_equal:
105+
def __init__(self, selector, classname):
106+
self.selector = selector
107+
self.classname = classname
108+
109+
def __call__(self, driver):
110+
try:
111+
elem = driver.find_element(By.CSS_SELECTOR, self.selector)
112+
classname = elem.get_attribute("class")
113+
logger.debug(
114+
"class to equal {%s} => expected %s", classname, self.classname
115+
)
116+
return str(classname) == self.classname
117+
except WebDriverException:
118+
return False

0 commit comments

Comments
 (0)