From 7c3b38d49b16e7d3510b8c3f8fd333129280df01 Mon Sep 17 00:00:00 2001 From: Martin Pelikan Date: Thu, 29 Dec 2016 12:31:00 -0800 Subject: [PATCH] Adds Finds support to is_element_present() 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. --- docs/is_element_present.rst | 9 ++++++++- tests/simple_page/test_is_element_present.py | 6 ++++++ webium/base_page.py | 10 ++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/is_element_present.rst b/docs/is_element_present.rst index 929e17c..ed9d688 100644 --- a/docs/is_element_present.rst +++ b/docs/is_element_present.rst @@ -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 ----------- @@ -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. \ No newline at end of file +*Note:* if an element is not shown this option will delay ``False`` value from being returned. diff --git a/tests/simple_page/test_is_element_present.py b/tests/simple_page/test_is_element_present.py index 94d8feb..d439a1f 100644 --- a/tests/simple_page/test_is_element_present.py +++ b/tests/simple_page/test_is_element_present.py @@ -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') diff --git a/webium/base_page.py b/webium/base_page.py index 4d849ed..f1953d5 100644 --- a/webium/base_page.py +++ b/webium/base_page.py @@ -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)