From 5834a571bec41f30aebb1fecd5f1a5fd322c0eaf Mon Sep 17 00:00:00 2001 From: Muhammad Rasyid Fahroni Date: Fri, 2 Sep 2016 06:13:04 +0700 Subject: [PATCH] adding get matching xpath count and xpath should match x times keyword --- src/AppiumLibrary/keywords/_element.py | 50 +++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/AppiumLibrary/keywords/_element.py b/src/AppiumLibrary/keywords/_element.py index 7ffc1f38..35f8a89e 100644 --- a/src/AppiumLibrary/keywords/_element.py +++ b/src/AppiumLibrary/keywords/_element.py @@ -135,7 +135,7 @@ def element_should_be_disabled(self, locator, loglevel='INFO'): """Verifies that element identified with locator is disabled. Key attributes for arbitrary elements are `id` and `name`. See - `introduction` for details about locating elements. + `introduction` for details about locating elements. """ if self._element_find(locator, True, True).is_enabled(): self.log_source(loglevel) @@ -147,7 +147,7 @@ def element_should_be_enabled(self, locator, loglevel='INFO'): """Verifies that element identified with locator is enabled. Key attributes for arbitrary elements are `id` and `name`. See - `introduction` for details about locating elements. + `introduction` for details about locating elements. """ if not self._element_find(locator, True, True).is_enabled(): self.log_source(loglevel) @@ -289,7 +289,7 @@ def get_element_location(self, locator): """Get element location Key attributes for arbitrary elements are `id` and `name`. See - `introduction` for details about locating elements. + `introduction` for details about locating elements. """ element = self._element_find(locator, True, True) element_location = element.location @@ -300,13 +300,55 @@ def get_element_size(self, locator): """Get element size Key attributes for arbitrary elements are `id` and `name`. See - `introduction` for details about locating elements. + `introduction` for details about locating elements. """ element = self._element_find(locator, True, True) element_size = element.size self._info("Element '%s' size: %s " % (locator, element_size)) return element_size + # Public, xpath + + def get_matching_xpath_count(self, xpath): + """Returns number of elements matching `xpath` + + One should not use the xpath= prefix for 'xpath'. XPath is assumed. + + Correct: + | count = | Get Matching Xpath Count | //android.view.View[@text='Test'] + Incorrect: + | count = | Get Matching Xpath Count | xpath=//android.view.View[@text='Test'] + + If you wish to assert the number of matching elements, use + `Xpath Should Match X Times`. + """ + count = len(self._element_find("xpath=" + xpath, False, False)) + return str(count) + + def xpath_should_match_x_times(self, xpath, expected_xpath_count, error=None, loglevel='INFO'): + """Verifies that the page contains the given number of elements located by the given `xpath`. + + One should not use the xpath= prefix for 'xpath'. XPath is assumed. + + Correct: + | Xpath Should Match X Times | //android.view.View[@text='Test'] | 1 + Incorrect: + | Xpath Should Match X Times | xpath=//android.view.View[@text='Test'] | 1 + + `error` can be used to override the default error message. + + See `Log Source` for explanation about `loglevel` argument. + """ + actual_xpath_count = len(self._element_find("xpath=" + xpath, False, False)) + if int(actual_xpath_count) != int(expected_xpath_count): + if not error: + error = "Xpath %s should have matched %s times but matched %s times"\ + %(xpath, expected_xpath_count, actual_xpath_count) + self.log_source(loglevel) + raise AssertionError(error) + self._info("Current page contains %s elements matching '%s'." + % (actual_xpath_count, xpath)) + # Private def _is_index(self, index_or_name):