|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +from selenium.webdriver.common.action_chains import ActionChains |
| 4 | +from selenium.webdriver.common.keys import Keys |
| 5 | +from selenium.webdriver.remote.webelement import WebElement |
| 6 | + |
| 7 | + |
| 8 | +class Keyboard: |
| 9 | + """Representation of a keyboard. |
| 10 | +
|
| 11 | + Requires a WebDriver instance to use. |
| 12 | +
|
| 13 | + Arguments: |
| 14 | + driver: The WebDriver instance to use. |
| 15 | + element: Optionally, a WebElement to act on. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, driver, element: Union[WebElement, None] = None) -> None: |
| 19 | + self.driver = driver |
| 20 | + |
| 21 | + self.element = element |
| 22 | + |
| 23 | + def _resolve_key_down_action(self, action_chain: ActionChains, key: str) -> ActionChains: |
| 24 | + """Given the string <key>, select the correct action for key down. |
| 25 | +
|
| 26 | + For modifier keys, use ActionChains.key_down(). |
| 27 | + For other keys, use ActionChains.send_keys() or ActionChains.send_keys_to_element() |
| 28 | + """ |
| 29 | + key_value = getattr(Keys, key, None) |
| 30 | + |
| 31 | + if key_value: |
| 32 | + chain = action_chain.key_down(key_value, self.element) |
| 33 | + elif self.element: |
| 34 | + chain = action_chain.send_keys_to_element(self.element, key) |
| 35 | + else: |
| 36 | + chain = action_chain.send_keys(key) |
| 37 | + |
| 38 | + return chain |
| 39 | + |
| 40 | + def _resolve_key_up_action(self, action_chain: ActionChains, key: str) -> ActionChains: |
| 41 | + """Given the string <key>, select the correct action for key up. |
| 42 | +
|
| 43 | + For modifier keys, use ActionChains.key_up(). |
| 44 | + For other keys, use ActionChains.send_keys() or ActionChains.send_keys_to_element() |
| 45 | + """ |
| 46 | + key_value = getattr(Keys, key, None) |
| 47 | + |
| 48 | + chain = action_chain |
| 49 | + if key_value: |
| 50 | + chain = action_chain.key_up(key_value, self.element) |
| 51 | + |
| 52 | + return chain |
| 53 | + |
| 54 | + def down(self, key: str) -> "Keyboard": |
| 55 | + """Hold down on a key. |
| 56 | +
|
| 57 | + Arguments: |
| 58 | + key: The name of a key to hold. |
| 59 | +
|
| 60 | + Example: |
| 61 | +
|
| 62 | + >>> b = Browser() |
| 63 | + >>> Keyboard(b.driver).down('SHIFT') |
| 64 | + """ |
| 65 | + chain = ActionChains(self.driver) |
| 66 | + chain = self._resolve_key_down_action(chain, key) |
| 67 | + chain.perform() |
| 68 | + return self |
| 69 | + |
| 70 | + def up(self, key: str) -> "Keyboard": |
| 71 | + """Release a held key. |
| 72 | +
|
| 73 | + If <key> is not held down, this method has no effect. |
| 74 | +
|
| 75 | + Arguments: |
| 76 | + key: The name of a key to release. |
| 77 | +
|
| 78 | + Example: |
| 79 | +
|
| 80 | + >>> b = Browser() |
| 81 | + >>> Keyboard(b.driver).down('SHIFT') |
| 82 | + >>> Keyboard(b.driver).up('SHIFT') |
| 83 | + """ |
| 84 | + chain = ActionChains(self.driver) |
| 85 | + chain = self._resolve_key_up_action(chain, key) |
| 86 | + chain.perform() |
| 87 | + return self |
| 88 | + |
| 89 | + def press(self, key_pattern: str, delay: int = 0) -> "Keyboard": |
| 90 | + """Hold and release a key pattern. |
| 91 | +
|
| 92 | + Key patterns are strings of key names separated by '+'. |
| 93 | + The following are examples of key patterns: |
| 94 | + - 'CONTROL' |
| 95 | + - 'CONTROL+a' |
| 96 | + - 'CONTROL+a+BACKSPACE+b' |
| 97 | +
|
| 98 | + Arguments: |
| 99 | + key_pattern: Pattern of keys to hold and release. |
| 100 | + delay: Time, in seconds, to wait between the hold and release. |
| 101 | +
|
| 102 | + Example: |
| 103 | +
|
| 104 | + >>> b = Browser() |
| 105 | + >>> Keyboard(b.driver).press('CONTROL+a') |
| 106 | + """ |
| 107 | + keys_names = key_pattern.split("+") |
| 108 | + |
| 109 | + chain = ActionChains(self.driver) |
| 110 | + |
| 111 | + for item in keys_names: |
| 112 | + chain = self._resolve_key_down_action(chain, item) |
| 113 | + |
| 114 | + if delay: |
| 115 | + chain = chain.pause(delay) |
| 116 | + |
| 117 | + for item in keys_names: |
| 118 | + chain = self._resolve_key_up_action(chain, item) |
| 119 | + |
| 120 | + chain.perform() |
| 121 | + |
| 122 | + return self |
0 commit comments