Skip to content

Commit c5fec8c

Browse files
BeyondEvillmtierney
authored andcommitted
Add expected condition that waits for all found elements to be visible (#3532)
1 parent e0e8f01 commit c5fec8c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

py/selenium/webdriver/support/expected_conditions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ def __call__(self, driver):
124124
return [element for element in _find_elements(driver, self.locator) if _element_if_visible(element)]
125125

126126

127+
class visibility_of_all_elements_located(object):
128+
""" An expectation for checking that all elements are present on the DOM of a
129+
page and visible. Visibility means that the elements are not only displayed
130+
but also has a height and width that is greater than 0.
131+
locator - used to find the elements
132+
returns the list of WebElements once they are located and visible
133+
"""
134+
def __init__(self, locator):
135+
self.locator = locator
136+
137+
def __call__(self, driver):
138+
try:
139+
elements = _find_elements(driver, self.locator)
140+
for element in elements:
141+
if _element_if_visible(element, visibility=False):
142+
return False
143+
return elements
144+
except StaleElementReferenceException:
145+
return False
146+
147+
127148
class text_to_be_present_in_element(object):
128149
""" An expectation for checking if the given text is present in the
129150
specified element.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ def testShouldFailToFindVisibleElementsWhenExplicitWaiting(driver, pages):
9090
WebDriverWait(driver, 0.7).until(EC.visibility_of_any_elements_located((By.CLASS_NAME, "redbox")))
9191

9292

93+
def testShouldWaitUntilAllVisibleElementsAreFoundWhenSearchingForMany(driver, pages):
94+
pages.load("hidden_partially.html")
95+
add_visible = driver.find_element_by_id("addVisible")
96+
97+
add_visible.click()
98+
add_visible.click()
99+
100+
elements = WebDriverWait(driver, 2).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))
101+
assert len(elements) == 2
102+
103+
104+
def testShouldFailIfNotAllElementsAreVisible(driver, pages):
105+
pages.load("hidden_partially.html")
106+
add_visible = driver.find_element_by_id("addVisible")
107+
add_hidden = driver.find_element_by_id("addHidden")
108+
109+
add_visible.click()
110+
add_hidden.click()
111+
with pytest.raises(TimeoutException):
112+
WebDriverWait(driver, 0.7).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))
113+
114+
93115
def testShouldWaitOnlyAsLongAsTimeoutSpecifiedWhenImplicitWaitsAreSet(driver, pages):
94116
pages.load("dynamic.html")
95117
driver.implicitly_wait(0.5)

0 commit comments

Comments
 (0)