|
3 | 3 | from selenium.webdriver.remote.webelement import WebElement |
4 | 4 | from Selenium2Library import utils |
5 | 5 | from Selenium2Library.locators import ElementFinder |
| 6 | +from Selenium2Library.locators import CustomLocator |
6 | 7 | from keywordgroup import KeywordGroup |
7 | 8 |
|
8 | 9 | try: |
@@ -72,6 +73,23 @@ def element_should_contain(self, locator, expected, message=''): |
72 | 73 | "its text was '%s'." % (locator, expected, actual) |
73 | 74 | raise AssertionError(message) |
74 | 75 |
|
| 76 | + def element_should_not_contain(self, locator, expected, message=''): |
| 77 | + """Verifies element identified by `locator` does not contain text `expected`. |
| 78 | +
|
| 79 | + `message` can be used to override the default error message. |
| 80 | +
|
| 81 | + Key attributes for arbitrary elements are `id` and `name`. See |
| 82 | + `Element Should Contain` for more details. |
| 83 | + """ |
| 84 | + self._info("Verifying element '%s' does not contain text '%s'." |
| 85 | + % (locator, expected)) |
| 86 | + actual = self._get_text(locator) |
| 87 | + if expected in actual: |
| 88 | + if not message: |
| 89 | + message = "Element '%s' should not contain text '%s' but " \ |
| 90 | + "it did." % (locator, expected) |
| 91 | + raise AssertionError(message) |
| 92 | + |
75 | 93 | def frame_should_contain(self, locator, text, loglevel='INFO'): |
76 | 94 | """Verifies frame identified by `locator` contains `text`. |
77 | 95 |
|
@@ -602,6 +620,38 @@ def xpath_should_match_x_times(self, xpath, expected_xpath_count, message='', lo |
602 | 620 | self._info("Current page contains %s elements matching '%s'." |
603 | 621 | % (actual_xpath_count, xpath)) |
604 | 622 |
|
| 623 | + # Public, custom |
| 624 | + def add_location_strategy(self, strategy_name, strategy_keyword, persist=False): |
| 625 | + """Adds a custom location strategy based on a user keyword. Location strategies are |
| 626 | + automatically removed after leaving the current scope by default. Setting `persist` |
| 627 | + to any non-empty string will cause the location strategy to stay registered throughout |
| 628 | + the life of the test. |
| 629 | +
|
| 630 | + Trying to add a custom location strategy with the same name as one that already exists will |
| 631 | + cause the keyword to fail. |
| 632 | +
|
| 633 | + Custom locator keyword example: |
| 634 | + | Custom Locator Strategy | [Arguments] | ${browser} | ${criteria} | ${tag} | ${constraints} | |
| 635 | + | | ${retVal}= | Execute Javascript | return window.document.getElementById('${criteria}'); | |
| 636 | + | | [Return] | ${retVal} | |
| 637 | +
|
| 638 | + Usage example: |
| 639 | + | Add Location Strategy | custom | Custom Locator Strategy | |
| 640 | + | Page Should Contain Element | custom=my_id | |
| 641 | +
|
| 642 | + See `Remove Location Strategy` for details about removing a custom location strategy. |
| 643 | + """ |
| 644 | + strategy = CustomLocator(strategy_name, strategy_keyword) |
| 645 | + self._element_finder.register(strategy, persist) |
| 646 | + |
| 647 | + def remove_location_strategy(self, strategy_name): |
| 648 | + """Removes a previously added custom location strategy. |
| 649 | + Will fail if a default strategy is specified. |
| 650 | +
|
| 651 | + See `Add Location Strategy` for details about adding a custom location strategy. |
| 652 | + """ |
| 653 | + self._element_finder.unregister(strategy_name) |
| 654 | + |
605 | 655 | # Private |
606 | 656 |
|
607 | 657 | def _element_find(self, locator, first_only, required, tag=None): |
@@ -733,4 +783,3 @@ def _page_should_not_contain_element(self, locator, tag, message, loglevel): |
733 | 783 | raise AssertionError(message) |
734 | 784 | self._info("Current page does not contain %s '%s'." |
735 | 785 | % (element_name, locator)) |
736 | | - |
|
0 commit comments