Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_input.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from typing import Any, Optional, Union1819from selenium.common.exceptions import InvalidArgumentException20from selenium.webdriver.remote.webelement import WebElement2122from .input_device import InputDevice23from .interaction import POINTER, POINTER_KINDS242526class PointerInput(InputDevice):27DEFAULT_MOVE_DURATION = 2502829def __init__(self, kind, name):30super().__init__()31if kind not in POINTER_KINDS:32raise InvalidArgumentException(f"Invalid PointerInput kind '{kind}'")33self.type = POINTER34self.kind = kind35self.name = name3637def create_pointer_move(38self,39duration=DEFAULT_MOVE_DURATION,40x: float = 0,41y: float = 0,42origin: Optional[WebElement] = None,43**kwargs,44):45action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs}46if isinstance(origin, WebElement):47action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}48elif origin is not None:49action["origin"] = origin50self.add_action(self._convert_keys(action))5152def create_pointer_down(self, **kwargs):53data = {"type": "pointerDown", "duration": 0, **kwargs}54self.add_action(self._convert_keys(data))5556def create_pointer_up(self, button):57self.add_action({"type": "pointerUp", "duration": 0, "button": button})5859def create_pointer_cancel(self):60self.add_action({"type": "pointerCancel"})6162def create_pause(self, pause_duration: Union[int, float] = 0) -> None:63self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})6465def encode(self):66return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}6768def _convert_keys(self, actions: dict[str, Any]):69out = {}70for k, v in actions.items():71if v is None:72continue73if k in ("x", "y"):74out[k] = int(v)75continue76splits = k.split("_")77new_key = splits[0] + "".join(v.title() for v in splits[1:])78out[new_key] = v79return out808182