Skip to content

Commit

Permalink
Find visible elements (#2041)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
remcowesterhoud authored and AutomatedTester committed May 18, 2016
1 parent aadaa15 commit 71aa100
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
45 changes: 45 additions & 0 deletions common/src/web/hidden_partially.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var next = 0;

function addVisibleBox() {
var box = document.createElement('DIV');
box.id = 'box' + next++;
box.className = 'redbox';
box.style.width = '150px';
box.style.height = '150px';
box.style.backgroundColor = 'red';
box.style.border = '1px solid black';
box.style.margin = '5px';
box.style.visibility = 'visible'

window.setTimeout(function() {
document.body.appendChild(box);
}, 1000);
}

function addHiddenBox() {
var box = document.createElement('DIV');
box.id = 'box' + next++;
box.className = 'redbox';
box.style.width = '150px';
box.style.height = '150px';
box.style.backgroundColor = 'red';
box.style.border = '1px solid black';
box.style.margin = '5px';
box.style.visibility = 'hidden';

window.setTimeout(function() {
document.body.appendChild(box);
}, 1000);
}
</script>
</head>
<body>
<input id="addVisible" type="button" value="Add a visible box!" onclick="addVisibleBox()"/>
<input id="addHidden" type="button" value="Add a hidden box!" onclick="addHiddenBox();" />
</body>
</html>
12 changes: 12 additions & 0 deletions py/selenium/webdriver/support/expected_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def __init__(self, locator):

def __call__(self, driver):
return _find_elements(driver, self.locator)

class visibility_of_all_elements_located(object):
""" An expectation for checking that there is at least one element visible
on a web page.
locator is used to find the element
returns the list of WebElements once they are located
"""
def __init__(self, locator):
self.locator = locator

def __call__(self, driver):
return [element for element in _find_elements(driver, self.locator) if _element_if_visible(element)]

class text_to_be_present_in_element(object):
""" An expectation for checking if the given text is present in the
Expand Down
23 changes: 18 additions & 5 deletions py/test/selenium/webdriver/common/webdriverwait_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,25 @@ def testShouldExplicitlyWaituntilAtLeastOneElementIsFoundWhenSearchingForMany(se

def testShouldFailToFindElementsWhenExplicitWaiting(self):
self._loadPage("dynamic")
try:
with self.assertRaises(TimeoutException):
elements = WebDriverWait(self.driver, 0.7).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "redbox")))
except TimeoutException as e:
pass # we should get a timeout
except Exception as e:
self.fail("Expected TimeoutException but got " + str(e))

def testShouldWaitUntilAtLeastOneVisibleElementsIsFoundWhenSearchingForMany(self):
self._loadPage("hidden_partially")
add_visible = self.driver.find_element_by_id("addVisible")
add_hidden = self.driver.find_element_by_id("addHidden")

add_visible.click()
add_visible.click()
add_hidden.click()

elements = WebDriverWait(self.driver, 2).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))
self.assertTrue(len(elements) == 2)

def testShouldFailToFindVisibleElementsWhenExplicitWaiting(self):
self._loadPage("hidden_partially")
with self.assertRaises(TimeoutException):
elements = WebDriverWait(self.driver, 0.7).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "redbox")))

def testShouldWaitOnlyAsLongAsTimeoutSpecifiedWhenImplicitWaitsAreSet(self):
self._loadPage("dynamic")
Expand Down

0 comments on commit 71aa100

Please sign in to comment.