|
| 1 | +""" Backport of SeleniumLibrary >3.2.0's `Press Keys` |
| 2 | +""" |
| 3 | +from robot.utils import plural_or_not |
| 4 | + |
| 5 | +# flake8: noqa |
| 6 | +from selenium.webdriver.common.action_chains import ActionChains |
| 7 | +from selenium.webdriver.common.keys import Keys |
| 8 | +from SeleniumLibrary.base import LibraryComponent, keyword |
| 9 | +from SeleniumLibrary.utils import is_truthy |
| 10 | + |
| 11 | + |
| 12 | +class KeysKeywords(LibraryComponent): |
| 13 | + @keyword |
| 14 | + def press_keys(self, locator=None, *keys): |
| 15 | + """Simulates user pressing key(s) to an element or on the active browser. |
| 16 | + If ``locator`` evaluates as false, see `Boolean arguments` for more |
| 17 | + details, then the ``keys`` are sent to the currently active browser. |
| 18 | + Otherwise element is searched and ``keys`` are send to the element |
| 19 | + identified by the ``locator``. In later case, keyword fails if element |
| 20 | + is not found. See the `Locating elements` section for details about |
| 21 | + the locator syntax. |
| 22 | + ``keys`` arguments can contain one or many strings, but it can not |
| 23 | + be empty. ``keys`` can also be a combination of |
| 24 | + [https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html|Selenium Keys] |
| 25 | + and strings or a single Selenium Key. If Selenium Key is combined |
| 26 | + with strings, Selenium key and strings must be separated by the |
| 27 | + `+` character, like in `CONTROL+c`. Selenium Keys |
| 28 | + are space and case sensitive and Selenium Keys are not parsed |
| 29 | + inside of the string. Example AALTO, would send string `AALTO` |
| 30 | + and `ALT` not parsed inside of the string. But `A+ALT+O` would |
| 31 | + found Selenium ALT key from the ``keys`` argument. It also possible |
| 32 | + to press many Selenium Keys down at the same time, example |
| 33 | + 'ALT+ARROW_DOWN`. |
| 34 | + If Selenium Keys are detected in the ``keys`` argument, keyword |
| 35 | + will press the Selenium Key down, send the strings and |
| 36 | + then release the Selenium Key. If keyword needs to send a Selenium |
| 37 | + Key as a string, then each character must be separated with |
| 38 | + `+` character, example `E+N+D`. |
| 39 | + `CTRL` is alias for |
| 40 | + [https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html#selenium.webdriver.common.keys.Keys.CONTROL|Selenium CONTROL] |
| 41 | + and ESC is alias for |
| 42 | + [https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html#selenium.webdriver.common.keys.Keys.ESCAPE|Selenium ESCAPE] |
| 43 | + New in SeleniumLibrary 3.3 |
| 44 | + Examples: |
| 45 | + | `Press Keys` | text_field | AAAAA | | # Sends string "AAAAA" to element. | |
| 46 | + | `Press Keys` | None | BBBBB | | # Sends string "BBBBB" to currently active browser. | |
| 47 | + | `Press Keys` | text_field | E+N+D | | # Sends string "END" to element. | |
| 48 | + | `Press Keys` | text_field | XXX | YY | # Sends strings "XXX" and "YY" to element. | |
| 49 | + | `Press Keys` | text_field | XXX+YY | | # Same as above. | |
| 50 | + | `Press Keys` | text_field | ALT+ARROW_DOWN | | # Pressing "ALT" key down, then pressing ARROW_DOWN and then releasing both keys. | |
| 51 | + | `Press Keys` | text_field | ALT | ARROW_DOWN | # Pressing "ALT" key and then pressing ARROW_DOWN. | |
| 52 | + | `Press Keys` | text_field | CTRL+c | | # Pressing CTRL key down, sends string "c" and then releases CTRL key. | |
| 53 | + | `Press Keys` | button | RETURN | | # Pressing "ENTER" key to element. | |
| 54 | + """ |
| 55 | + parsed_keys = self._parse_keys(*keys) |
| 56 | + if is_truthy(locator): |
| 57 | + self.info("Sending key(s) %s to %s element." % (keys, locator)) |
| 58 | + else: |
| 59 | + self.info("Sending key(s) %s to page." % str(keys)) |
| 60 | + self._press_keys(locator, parsed_keys) |
| 61 | + |
| 62 | + def _map_ascii_key_code_to_key(self, key_code): |
| 63 | + map = { |
| 64 | + 0: Keys.NULL, |
| 65 | + 8: Keys.BACK_SPACE, |
| 66 | + 9: Keys.TAB, |
| 67 | + 10: Keys.RETURN, |
| 68 | + 13: Keys.ENTER, |
| 69 | + 24: Keys.CANCEL, |
| 70 | + 27: Keys.ESCAPE, |
| 71 | + 32: Keys.SPACE, |
| 72 | + 42: Keys.MULTIPLY, |
| 73 | + 43: Keys.ADD, |
| 74 | + 44: Keys.SEPARATOR, |
| 75 | + 45: Keys.SUBTRACT, |
| 76 | + 56: Keys.DECIMAL, |
| 77 | + 57: Keys.DIVIDE, |
| 78 | + 59: Keys.SEMICOLON, |
| 79 | + 61: Keys.EQUALS, |
| 80 | + 127: Keys.DELETE, |
| 81 | + } |
| 82 | + key = map.get(key_code) |
| 83 | + if key is None: |
| 84 | + key = chr(key_code) |
| 85 | + return key |
| 86 | + |
| 87 | + def _map_named_key_code_to_special_key(self, key_name): |
| 88 | + try: |
| 89 | + return getattr(Keys, key_name) |
| 90 | + except AttributeError: |
| 91 | + message = "Unknown key named '%s'." % (key_name) |
| 92 | + self.debug(message) |
| 93 | + raise ValueError(message) |
| 94 | + |
| 95 | + def _press_keys(self, locator, parsed_keys): |
| 96 | + if is_truthy(locator): |
| 97 | + element = self.find_element(locator) |
| 98 | + else: |
| 99 | + element = None |
| 100 | + for parsed_key in parsed_keys: |
| 101 | + actions = ActionChains(self.driver) |
| 102 | + special_keys = [] |
| 103 | + for key in parsed_key: |
| 104 | + if self._selenium_keys_has_attr(key.original): |
| 105 | + special_keys = self._press_keys_special_keys( |
| 106 | + actions, element, parsed_key, key, special_keys |
| 107 | + ) |
| 108 | + else: |
| 109 | + self._press_keys_normal_keys(actions, element, key) |
| 110 | + for special_key in special_keys: |
| 111 | + self.info("Releasing special key %s." % special_key.original) |
| 112 | + actions.key_up(special_key.converted) |
| 113 | + actions.perform() |
| 114 | + |
| 115 | + def _press_keys_normal_keys(self, actions, element, key): |
| 116 | + self.info("Sending key%s %s" % (plural_or_not(key.converted), key.converted)) |
| 117 | + if element: |
| 118 | + actions.send_keys_to_element(element, key.converted) |
| 119 | + else: |
| 120 | + actions.send_keys(key.converted) |
| 121 | + |
| 122 | + def _press_keys_special_keys(self, actions, element, parsed_key, key, special_keys): |
| 123 | + if len(parsed_key) == 1 and element: |
| 124 | + self.info("Pressing special key %s to element." % key.original) |
| 125 | + actions.send_keys_to_element(element, key.converted) |
| 126 | + elif len(parsed_key) == 1 and not element: |
| 127 | + self.info("Pressing special key %s to browser." % key.original) |
| 128 | + actions.send_keys(key.converted) |
| 129 | + else: |
| 130 | + self.info("Pressing special key %s down." % key.original) |
| 131 | + actions.key_down(key.converted) |
| 132 | + special_keys.append(key) |
| 133 | + return special_keys |
| 134 | + |
| 135 | + def _parse_keys(self, *keys): |
| 136 | + if not keys: |
| 137 | + raise AssertionError('"keys" argument can not be empty.') |
| 138 | + list_keys = [] |
| 139 | + for key in keys: |
| 140 | + separate_keys = self._separate_key(key) |
| 141 | + separate_keys = self._convert_special_keys(separate_keys) |
| 142 | + list_keys.append(separate_keys) |
| 143 | + return list_keys |
| 144 | + |
| 145 | + def _parse_aliases(self, key): |
| 146 | + if key == "CTRL": |
| 147 | + return "CONTROL" |
| 148 | + if key == "ESC": |
| 149 | + return "ESCAPE" |
| 150 | + return key |
| 151 | + |
| 152 | + def _separate_key(self, key): |
| 153 | + one_key = "" |
| 154 | + list_keys = [] |
| 155 | + for char in key: |
| 156 | + if char == "+" and one_key != "": |
| 157 | + list_keys.append(one_key) |
| 158 | + one_key = "" |
| 159 | + else: |
| 160 | + one_key += char |
| 161 | + if one_key: |
| 162 | + list_keys.append(one_key) |
| 163 | + return list_keys |
| 164 | + |
| 165 | + def _convert_special_keys(self, keys): |
| 166 | + KeysRecord = namedtuple("KeysRecord", "converted, original") |
| 167 | + converted_keys = [] |
| 168 | + for key in keys: |
| 169 | + key = self._parse_aliases(key) |
| 170 | + if self._selenium_keys_has_attr(key): |
| 171 | + converted_keys.append(KeysRecord(getattr(Keys, key), key)) |
| 172 | + else: |
| 173 | + converted_keys.append(KeysRecord(key, key)) |
| 174 | + return converted_keys |
| 175 | + |
| 176 | + def _selenium_keys_has_attr(self, key): |
| 177 | + try: |
| 178 | + return hasattr(Keys, key) |
| 179 | + except UnicodeError: # To support Python 2 and non ascii characters. |
| 180 | + return False |
0 commit comments