Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_input.py
1864 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from typing import Any, Optional, Union
19
20
from selenium.common.exceptions import InvalidArgumentException
21
from selenium.webdriver.remote.webelement import WebElement
22
23
from .input_device import InputDevice
24
from .interaction import POINTER, 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: 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: Union[int, float] = 0) -> 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: dict[str, 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
81
82