-
Notifications
You must be signed in to change notification settings - Fork 752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow locator strategy of xpath= for matching xpath keywords. #527
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,41 +602,54 @@ def page_should_not_contain_image(self, locator, message='', loglevel='INFO'): | |
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. | ||
As of release 1.8 `Get Matching Xpath Count` allows the | ||
argument `xpath` to start with the xpath= locator strategy. | ||
|
||
Correct: | ||
Example: | ||
| count = | Get Matching Xpath Count | //div[@id='sales-pop'] | ||
Incorrect: | ||
| count = | Get Matching Xpath Count | xpath=//div[@id='sales-pop'] | ||
|
||
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) | ||
if xpath.startswith('//'): | ||
xpath = "xpath=" + xpath | ||
|
||
if xpath.startswith('xpath='): | ||
count = len(self._element_find(xpath, False, False)) | ||
return str(count) | ||
else: | ||
raise ValueError("Element locator must be xpath and start with xpath= or //") | ||
|
||
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`. | ||
def xpath_should_match_x_times(self, locator, expected_xpath_count, message='', loglevel='INFO'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left the argument in Xpath Should Match X Times xpath=//input[@type="text"] ${1} and the argument was xpath, Robot Framework would think you were placing a named/optional argument, xpath, before a positional argument of expected_xpath_count. They don't have to be the same. For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to avoid an issue with the named argument processing of RF, I would suggest calling the argument xpath_, rather than locator. |
||
"""Verifies that the page contains the given number of elements located by the given `locator`. | ||
|
||
One should not use the xpath= prefix for 'xpath'. XPath is assumed. | ||
As of release 1.8 `Xpath Should Match X Times` allows the | ||
argument `locator` to start with the xpath= locator strategy. | ||
|
||
Correct: | ||
Example: | ||
| Xpath Should Match X Times | //div[@id='sales-pop'] | 1 | ||
Incorrect: | ||
| Xpath Should Match X Times | xpath=//div[@id='sales-pop'] | 1 | ||
|
||
See `Page Should Contain Element` for explanation about `message` and | ||
`loglevel` arguments. | ||
""" | ||
actual_xpath_count = len(self._element_find("xpath=" + xpath, False, False)) | ||
if locator.startswith('//'): | ||
locator = "xpath=" + locator | ||
|
||
if locator.startswith('xpath='): | ||
actual_xpath_count = len(self._element_find(locator, False, False)) | ||
else: | ||
raise ValueError("Element locator must be xpath and start with xpath= or //") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a backwards incompatible change. Previously, the XPath did not have to start with //. For example: |
||
|
||
if int(actual_xpath_count) != int(expected_xpath_count): | ||
if not message: | ||
message = "Xpath %s should have matched %s times but matched %s times"\ | ||
%(xpath, expected_xpath_count, actual_xpath_count) | ||
%(locator, expected_xpath_count, actual_xpath_count) | ||
self.log_source(loglevel) | ||
raise AssertionError(message) | ||
self._info("Current page contains %s elements matching '%s'." | ||
% (actual_xpath_count, xpath)) | ||
% (actual_xpath_count, locator)) | ||
|
||
# Public, custom | ||
def add_location_strategy(self, strategy_name, strategy_keyword, persist=False): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The detection of the xpath locator prefix is both case- and space-sensitive. This is not consistent with S2L.