Skip to content
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
7 changes: 5 additions & 2 deletions src/Selenium2Library/keywords/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,12 @@ def page_should_not_contain_image(self, locator, message='', loglevel='INFO'):

# Public, xpath

def get_matching_xpath_count(self, xpath):
def get_matching_xpath_count(self, xpath, return_str=True):
"""Returns number of elements matching `xpath`

The default return type is `str` but it can changed to `int` by setting
the ``return_str`` argument to Python False.

One should not use the xpath= prefix for 'xpath'. XPath is assumed.

Correct:
Expand All @@ -636,7 +639,7 @@ def get_matching_xpath_count(self, xpath):
`Xpath Should Match X Times`.
"""
count = len(self._element_find("xpath=" + xpath, False, False))
return str(count)
return str(count) if return_str else count

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

IMHO we should follow DateTime's lead and use a util method to make sure that str('false') is treated as False, etc.

from robot.utils import is_truthy
...
return str(count) if is_truthy(return_str) else count

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, but would like to do it same time for all the keywords using booleans. There is #719 for this problem.


def xpath_should_match_x_times(self, xpath, expected_xpath_count, message='', loglevel='INFO'):
"""Verifies that the page contains the given number of elements located by the given `xpath`.
Expand Down
7 changes: 7 additions & 0 deletions test/acceptance/keywords/elements.robot
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Documentation Tests elements
Test Setup Go To Page "links.html"
Resource ../resource.robot
Library String

*** Test Cases ***
Get Elements
Expand Down Expand Up @@ -61,6 +62,12 @@ Get Matching XPath Count
[Documentation] Get Matching XPath Count
${count}= Get Matching XPath Count //a
Should Be Equal ${count} 19
${count}= Get Matching XPath Count //a ${True}
Should Be Equal ${count} 19
Should Be String ${count}
${count}= Get Matching XPath Count //a ${False}
Should Be Equal ${count} ${19}
Should Not Be String ${count}
${count}= Get Matching XPath Count //div[@id="first_div"]/a
Should Be Equal ${count} 2

Expand Down