Skip to content

Commit

Permalink
adding get matching xpath count and xpath should match x times keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
semutmerah committed Sep 1, 2016
1 parent 35ef299 commit 5834a57
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/AppiumLibrary/keywords/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 5834a57

Please sign in to comment.