Skip to content

Commit 04217e8

Browse files
authored
Merge pull request #2049 from MrTeale/wait-for-class-values
Wait for class testing utilities
2 parents 49fab85 + 12e6d97 commit 04217e8

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1515

1616
- [#2050](https://github.com/plotly/dash/pull/2050) Changed `find_element` and `find_elements` to accept an `attribute` argument that aligns with Selenium's `By` class, allowing you to search elements by other attributes. Default value is `CSS_SELECTOR` to maintain backwards compatibility with previous `find_elements`.
1717

18+
### Added
19+
20+
- [#2049](https://github.com/plotly/dash/pull/2043) Added `wait_for_class_to_equal` and `wait_for_contains_class` methods to `dash.testing`
21+
1822
## [2.4.1] - 2022-05-11
1923

2024
### Fixed

dash/testing/browser.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
MoveTargetOutOfBoundsException,
2121
)
2222

23-
from dash.testing.wait import text_to_equal, style_to_equal, contains_text, until
23+
from dash.testing.wait import (
24+
text_to_equal,
25+
style_to_equal,
26+
class_to_equal,
27+
contains_text,
28+
contains_class,
29+
until,
30+
)
2431
from dash.testing.dash_page import DashPageMixin
2532
from dash.testing.errors import DashAppLoadingError, BrowserError, TestingTimeoutError
2633
from dash.testing.consts import SELENIUM_GRID_DEFAULT
@@ -309,6 +316,17 @@ def wait_for_element_by_id(self, element_id, timeout=None):
309316
f"timeout {timeout or self._wait_timeout}s => waiting for element id {element_id}",
310317
)
311318

319+
def wait_for_class_to_equal(self, selector, classname, timeout=None):
320+
"""Explicit wait until the element's class has expected `value` timeout
321+
if not set, equals to the fixture's `wait_timeout` shortcut to
322+
`WebDriverWait` with customized `class_to_equal` condition."""
323+
return self._wait_for(
324+
method=class_to_equal,
325+
args=(selector, classname),
326+
timeout=timeout,
327+
msg=f"classname => {classname} not found within {timeout or self._wait_timeout}s",
328+
)
329+
312330
def wait_for_style_to_equal(self, selector, style, val, timeout=None):
313331
"""Explicit wait until the element's style has expected `value` timeout
314332
if not set, equals to the fixture's `wait_timeout` shortcut to
@@ -334,6 +352,20 @@ def wait_for_text_to_equal(self, selector, text, timeout=None):
334352
msg=f"text -> {text} not found within {timeout or self._wait_timeout}s",
335353
)
336354

355+
def wait_for_contains_class(self, selector, classname, timeout=None):
356+
"""Explicit wait until the element's classes contains the expected `classname`.
357+
358+
timeout if not set, equals to the fixture's `wait_timeout`
359+
shortcut to `WebDriverWait` with customized `contains_class`
360+
condition.
361+
"""
362+
return self._wait_for(
363+
method=contains_class,
364+
args=(selector, classname),
365+
timeout=timeout,
366+
msg=f"classname -> {classname} not found inside element within {timeout or self._wait_timeout}s",
367+
)
368+
337369
def wait_for_contains_text(self, selector, text, timeout=None):
338370
"""Explicit wait until the element's text contains the expected `text`.
339371

dash/testing/wait.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ def __call__(self, driver):
6868
return False
6969

7070

71+
class contains_class:
72+
def __init__(self, selector, classname):
73+
self.selector = selector
74+
self.classname = classname
75+
76+
def __call__(self, driver):
77+
try:
78+
elem = driver.find_element(By.CSS_SELECTOR, self.selector)
79+
classname = elem.get_attribute("class")
80+
logger.debug(
81+
"contains class {%s} => expected %s", classname, self.classname
82+
)
83+
return self.classname in str(classname).split(" ")
84+
except WebDriverException:
85+
return False
86+
87+
7188
class text_to_equal:
7289
def __init__(self, selector, text):
7390
self.selector = selector
@@ -99,3 +116,20 @@ def __call__(self, driver):
99116
return val == self.val
100117
except WebDriverException:
101118
return False
119+
120+
121+
class class_to_equal:
122+
def __init__(self, selector, classname):
123+
self.selector = selector
124+
self.classname = classname
125+
126+
def __call__(self, driver):
127+
try:
128+
elem = driver.find_element(By.CSS_SELECTOR, self.selector)
129+
classname = elem.get_attribute("class")
130+
logger.debug(
131+
"class to equal {%s} => expected %s", classname, self.classname
132+
)
133+
return str(classname) == self.classname
134+
except WebDriverException:
135+
return False

0 commit comments

Comments
 (0)