| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import typing |
| 18 |
|
| 19 |
from selenium.common.exceptions import InvalidArgumentException |
| 20 |
from selenium.webdriver.remote.webelement import WebElement |
| 21 |
|
| 22 |
from .input_device import InputDevice |
| 23 |
from .interaction import POINTER |
| 24 |
from .interaction import POINTER_KINDS |
| 25 |
|
| 26 |
|
| 27 |
class PointerInput(InputDevice): |
| 28 |
DEFAULT_MOVE_DURATION = 250 |
| 29 |
|
| 30 |
def __init__(self, kind, name): |
| 31 |
super().__init__() |
| 32 |
if kind not in POINTER_KINDS: |
| 33 |
raise InvalidArgumentException(f"Invalid PointerInput kind '{kind}'") |
| 34 |
self.type = POINTER |
| 35 |
self.kind = kind |
| 36 |
self.name = name |
| 37 |
|
| 38 |
def create_pointer_move( |
| 39 |
self, |
| 40 |
duration=DEFAULT_MOVE_DURATION, |
| 41 |
x: float = 0, |
| 42 |
y: float = 0, |
| 43 |
origin: typing.Optional[WebElement] = None, |
| 44 |
**kwargs, |
| 45 |
): |
| 46 |
action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs} |
| 47 |
if isinstance(origin, WebElement): |
| 48 |
action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id} |
| 49 |
elif origin is not None: |
| 50 |
action["origin"] = origin |
| 51 |
self.add_action(self._convert_keys(action)) |
| 52 |
|
| 53 |
def create_pointer_down(self, **kwargs): |
| 54 |
data = {"type": "pointerDown", "duration": 0, **kwargs} |
| 55 |
self.add_action(self._convert_keys(data)) |
| 56 |
|
| 57 |
def create_pointer_up(self, button): |
| 58 |
self.add_action({"type": "pointerUp", "duration": 0, "button": button}) |
| 59 |
|
| 60 |
def create_pointer_cancel(self): |
| 61 |
self.add_action({"type": "pointerCancel"}) |
| 62 |
|
| 63 |
def create_pause(self, pause_duration: float) -> None: |
| 64 |
self.add_action({"type": "pause", "duration": int(pause_duration * 1000)}) |
| 65 |
|
| 66 |
def encode(self): |
| 67 |
return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions} |
| 68 |
|
| 69 |
def _convert_keys(self, actions: typing.Dict[str, typing.Any]): |
| 70 |
out = {} |
| 71 |
for k, v in actions.items(): |
| 72 |
if v is None: |
| 73 |
continue |
| 74 |
if k in ("x", "y"): |
| 75 |
out[k] = int(v) |
| 76 |
continue |
| 77 |
splits = k.split("_") |
| 78 |
new_key = splits[0] + "".join(v.title() for v in splits[1:]) |
| 79 |
out[new_key] = v |
| 80 |
return out |