Skip to content

Commit

Permalink
Adds Finds support to is_element_present() (#17)
Browse files Browse the repository at this point in the history
Finds returns a list of WebElements, not a single WebElement, and thus
is_displayed() fails when called upon said list. This commit adds
handling to check if there are any elements in the list, and if so,
determines if they are all visible.
  • Loading branch information
martinpelikan authored and SUNx2YCH committed Jan 4, 2017
1 parent 75c63fb commit f3f5956
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion docs/is_element_present.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ It returns if an element with corresponding name is shown on the page.

.. literalinclude:: ../examples/12_is_element_present.py

*Note*: Containers with elements located via ``Finds`` also support
``is_element_present``, albeit with special behaviour. Since the attribute will
be a list of elements, rather than a single element, ``True`` will be returned
only if all elements in the list are present. In the event that no elements are
found (i.e. an empty list), or one or more elements do not qualify as present,
``False`` will be returned.

just_in_dom
-----------

Expand All @@ -17,4 +24,4 @@ timeout
-------
``timeout`` in seconds allows you to wait for ``True`` value.

*Note:* if an element is not shown this option will delay ``False`` value from being returned.
*Note:* if an element is not shown this option will delay ``False`` value from being returned.
6 changes: 6 additions & 0 deletions tests/simple_page/test_is_element_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ def test_is_element_present(self):
ok_(self.page.is_element_present('icon_link'))
assert_false(self.page.is_element_present('unexistent_element'))

def test_is_element_present_via_finds(self):
ok_(self.page.is_element_present('anchor_list'))

def test_is_element_present_via_finds_empty_list(self):
assert_false(self.page.is_element_present('empty_element_list'))

def test_no_attribute(self):
assert_raises(WebiumException, self.page.is_element_present, 'no_such_attribute')
10 changes: 8 additions & 2 deletions webium/base_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ def _get_driver():
_get_driver().implicitly_wait(timeout)
try:
def is_displayed():
element = getattr(self, element_name, None)
if not element:
try:
element = getattr(self, element_name)
except AttributeError:
raise WebiumException('No element "{0}" within container {1}'.format(element_name, self))
if isinstance(element, list):
if element:
return all(ele.is_displayed() for ele in element)
else:
return False
return element.is_displayed()

is_displayed() if just_in_dom else wait(lambda: is_displayed(), timeout_seconds=timeout)
Expand Down

0 comments on commit f3f5956

Please sign in to comment.