| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from typing import List |
| 19 |
from typing import Optional |
| 20 |
from typing import Union |
| 21 |
|
| 22 |
from selenium.webdriver.remote.command import Command |
| 23 |
|
| 24 |
from . import interaction |
| 25 |
from .key_actions import KeyActions |
| 26 |
from .key_input import KeyInput |
| 27 |
from .pointer_actions import PointerActions |
| 28 |
from .pointer_input import PointerInput |
| 29 |
from .wheel_actions import WheelActions |
| 30 |
from .wheel_input import WheelInput |
| 31 |
|
| 32 |
|
| 33 |
class ActionBuilder: |
| 34 |
def __init__( |
| 35 |
self, |
| 36 |
driver, |
| 37 |
mouse: Optional[PointerInput] = None, |
| 38 |
wheel: Optional[WheelInput] = None, |
| 39 |
keyboard: Optional[KeyInput] = None, |
| 40 |
duration: int = 250, |
| 41 |
) -> None: |
| 42 |
mouse = mouse or PointerInput(interaction.POINTER_MOUSE, "mouse") |
| 43 |
keyboard = keyboard or KeyInput(interaction.KEY) |
| 44 |
wheel = wheel or WheelInput(interaction.WHEEL) |
| 45 |
self.devices = [mouse, keyboard, wheel] |
| 46 |
self._key_action = KeyActions(keyboard) |
| 47 |
self._pointer_action = PointerActions(mouse, duration=duration) |
| 48 |
self._wheel_action = WheelActions(wheel) |
| 49 |
self.driver = driver |
| 50 |
|
| 51 |
def get_device_with(self, name: str) -> Optional[Union["WheelInput", "PointerInput", "KeyInput"]]: |
| 52 |
return next(filter(lambda x: x == name, self.devices), None) |
| 53 |
|
| 54 |
@property |
| 55 |
def pointer_inputs(self) -> List[PointerInput]: |
| 56 |
return [device for device in self.devices if device.type == interaction.POINTER] |
| 57 |
|
| 58 |
@property |
| 59 |
def key_inputs(self) -> List[KeyInput]: |
| 60 |
return [device for device in self.devices if device.type == interaction.KEY] |
| 61 |
|
| 62 |
@property |
| 63 |
def key_action(self) -> KeyActions: |
| 64 |
return self._key_action |
| 65 |
|
| 66 |
@property |
| 67 |
def pointer_action(self) -> PointerActions: |
| 68 |
return self._pointer_action |
| 69 |
|
| 70 |
@property |
| 71 |
def wheel_action(self) -> WheelActions: |
| 72 |
return self._wheel_action |
| 73 |
|
| 74 |
def add_key_input(self, name: str) -> KeyInput: |
| 75 |
new_input = KeyInput(name) |
| 76 |
self._add_input(new_input) |
| 77 |
return new_input |
| 78 |
|
| 79 |
def add_pointer_input(self, kind: str, name: str) -> PointerInput: |
| 80 |
new_input = PointerInput(kind, name) |
| 81 |
self._add_input(new_input) |
| 82 |
return new_input |
| 83 |
|
| 84 |
def add_wheel_input(self, name: str) -> WheelInput: |
| 85 |
new_input = WheelInput(name) |
| 86 |
self._add_input(new_input) |
| 87 |
return new_input |
| 88 |
|
| 89 |
def perform(self) -> None: |
| 90 |
enc = {"actions": []} |
| 91 |
for device in self.devices: |
| 92 |
encoded = device.encode() |
| 93 |
if encoded["actions"]: |
| 94 |
enc["actions"].append(encoded) |
| 95 |
device.actions = [] |
| 96 |
self.driver.execute(Command.W3C_ACTIONS, enc) |
| 97 |
|
| 98 |
def clear_actions(self) -> None: |
| 99 |
"""Clears actions that are already stored on the remote end.""" |
| 100 |
self.driver.execute(Command.W3C_CLEAR_ACTIONS) |
| 101 |
|
| 102 |
def _add_input(self, new_input: Union[KeyInput, PointerInput, WheelInput]) -> None: |
| 103 |
self.devices.append(new_input) |