Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Adds Finds support to is_element_present() #17

Merged
merged 1 commit into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nested if is not needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, all([]) produces True. So, it depends on what you expect from Finds which does not contain any visible elements

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are needed to display what behaviour we are trying to achieve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantically, it might be more clear if I wrote this as if element != []: instead of if element:. I can change it to that if you prefer. This is mostly my habit when checking for empty sequences/iterables.

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