| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from typing import Union |
| 18 |
|
| 19 |
from selenium.webdriver.remote.webelement import WebElement |
| 20 |
|
| 21 |
from . import interaction |
| 22 |
from .input_device import InputDevice |
| 23 |
|
| 24 |
|
| 25 |
class ScrollOrigin: |
| 26 |
def __init__(self, origin: Union[str, WebElement], x_offset: int, y_offset: int) -> None: |
| 27 |
self._origin = origin |
| 28 |
self._x_offset = x_offset |
| 29 |
self._y_offset = y_offset |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0): |
| 33 |
return cls(element, x_offset, y_offset) |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def from_viewport(cls, x_offset: int = 0, y_offset: int = 0): |
| 37 |
return cls("viewport", x_offset, y_offset) |
| 38 |
|
| 39 |
@property |
| 40 |
def origin(self) -> Union[str, WebElement]: |
| 41 |
return self._origin |
| 42 |
|
| 43 |
@property |
| 44 |
def x_offset(self) -> int: |
| 45 |
return self._x_offset |
| 46 |
|
| 47 |
@property |
| 48 |
def y_offset(self) -> int: |
| 49 |
return self._y_offset |
| 50 |
|
| 51 |
|
| 52 |
class WheelInput(InputDevice): |
| 53 |
def __init__(self, name) -> None: |
| 54 |
super().__init__(name=name) |
| 55 |
self.name = name |
| 56 |
self.type = interaction.WHEEL |
| 57 |
|
| 58 |
def encode(self) -> dict: |
| 59 |
return {"type": self.type, "id": self.name, "actions": self.actions} |
| 60 |
|
| 61 |
def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int, origin) -> None: |
| 62 |
if isinstance(origin, WebElement): |
| 63 |
origin = {"element-6066-11e4-a52e-4f735466cecf": origin.id} |
| 64 |
self.add_action( |
| 65 |
{ |
| 66 |
"type": "scroll", |
| 67 |
"x": x, |
| 68 |
"y": y, |
| 69 |
"deltaX": delta_x, |
| 70 |
"deltaY": delta_y, |
| 71 |
"duration": duration, |
| 72 |
"origin": origin, |
| 73 |
} |
| 74 |
) |
| 75 |
|
| 76 |
def create_pause(self, pause_duration: float) -> None: |
| 77 |
self.add_action({"type": "pause", "duration": int(pause_duration * 1000)}) |