Skip to content

Commit 116adb8

Browse files
remcowesterhoudlukeis
authored andcommitted
Find visible elements (SeleniumHQ#2041)
* EC for finding visible elements There already was a way of finding a list of elements that are present on the page, but there was none for finding visible elements. I've created a class that makes this possible. * Clean * Changed to list comprehension * visibility_of_all_elements tests added * Created hidden_partially.html Used in the visibility_of_all_elements tests * Now uses assertRaises * Fixed usage of assertRaises * Fixed exception name I'm an idiot
1 parent f493d82 commit 116adb8

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

common/src/web/hidden_partially.html

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<script type="text/javascript">
6+
var next = 0;
7+
8+
function addVisibleBox() {
9+
var box = document.createElement('DIV');
10+
box.id = 'box' + next++;
11+
box.className = 'redbox';
12+
box.style.width = '150px';
13+
box.style.height = '150px';
14+
box.style.backgroundColor = 'red';
15+
box.style.border = '1px solid black';
16+
box.style.margin = '5px';
17+
box.style.visibility = 'visible'
18+
19+
window.setTimeout(function() {
20+
document.body.appendChild(box);
21+
}, 1000);
22+
}
23+
24+
function addHiddenBox() {
25+
var box = document.createElement('DIV');
26+
box.id = 'box' + next++;
27+
box.className = 'redbox';
28+
box.style.width = '150px';
29+
box.style.height = '150px';
30+
box.style.backgroundColor = 'red';
31+
box.style.border = '1px solid black';
32+
box.style.margin = '5px';
33+
box.style.visibility = 'hidden';
34+
35+
window.setTimeout(function() {
36+
document.body.appendChild(box);
37+
}, 1000);
38+
}
39+
</script>
40+
</head>
41+
<body>
42+
<input id="addVisible" type="button" value="Add a visible box!" onclick="addVisibleBox()"/>
43+
<input id="addHidden" type="button" value="Add a hidden box!" onclick="addHiddenBox();" />
44+
</body>
45+
</html>

py/selenium/webdriver/support/expected_conditions.py

+12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ def __init__(self, locator):
101101

102102
def __call__(self, driver):
103103
return _find_elements(driver, self.locator)
104+
105+
class visibility_of_all_elements_located(object):
106+
""" An expectation for checking that there is at least one element visible
107+
on a web page.
108+
locator is used to find the element
109+
returns the list of WebElements once they are located
110+
"""
111+
def __init__(self, locator):
112+
self.locator = locator
113+
114+
def __call__(self, driver):
115+
return [element for element in _find_elements(driver, self.locator) if _element_if_visible(element)]
104116

105117
class text_to_be_present_in_element(object):
106118
""" An expectation for checking if the given text is present in the

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,25 @@ def testShouldExplicitlyWaituntilAtLeastOneElementIsFoundWhenSearchingForMany(se
7272

7373
def testShouldFailToFindElementsWhenExplicitWaiting(self):
7474
self._loadPage("dynamic")
75-
try:
75+
with self.assertRaises(TimeoutException):
7676
elements = WebDriverWait(self.driver, 0.7).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "redbox")))
77-
except TimeoutException as e:
78-
pass # we should get a timeout
79-
except Exception as e:
80-
self.fail("Expected TimeoutException but got " + str(e))
77+
78+
def testShouldWaitUntilAtLeastOneVisibleElementsIsFoundWhenSearchingForMany(self):
79+
self._loadPage("hidden_partially")
80+
add_visible = self.driver.find_element_by_id("addVisible")
81+
add_hidden = self.driver.find_element_by_id("addHidden")
82+
83+
add_visible.click()
84+
add_visible.click()
85+
add_hidden.click()
86+
87+
elements = WebDriverWait(self.driver, 2).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))
88+
self.assertTrue(len(elements) == 2)
89+
90+
def testShouldFailToFindVisibleElementsWhenExplicitWaiting(self):
91+
self._loadPage("hidden_partially")
92+
with self.assertRaises(TimeoutException):
93+
elements = WebDriverWait(self.driver, 0.7).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))
8194

8295
def testShouldWaitOnlyAsLongAsTimeoutSpecifiedWhenImplicitWaitsAreSet(self):
8396
self._loadPage("dynamic")

0 commit comments

Comments
 (0)