Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_input.py
4041 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 Any1819from selenium.common.exceptions import InvalidArgumentException20from selenium.webdriver.common.actions.input_device import InputDevice21from selenium.webdriver.common.actions.interaction import POINTER, POINTER_KINDS22from selenium.webdriver.remote.webelement import WebElement232425class PointerInput(InputDevice):26DEFAULT_MOVE_DURATION = 2502728def __init__(self, kind, name):29super().__init__()30if kind not in POINTER_KINDS:31raise InvalidArgumentException(f"Invalid PointerInput kind '{kind}'")32self.type = POINTER33self.kind = kind34self.name = name3536def create_pointer_move(37self,38duration=DEFAULT_MOVE_DURATION,39x: float = 0,40y: float = 0,41origin: WebElement | None = None,42**kwargs,43):44action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs}45if isinstance(origin, WebElement):46action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}47elif origin is not None:48action["origin"] = origin49self.add_action(self._convert_keys(action))5051def create_pointer_down(self, **kwargs):52data = {"type": "pointerDown", "duration": 0, **kwargs}53self.add_action(self._convert_keys(data))5455def create_pointer_up(self, button):56self.add_action({"type": "pointerUp", "duration": 0, "button": button})5758def create_pointer_cancel(self):59self.add_action({"type": "pointerCancel"})6061def create_pause(self, pause_duration: int | float = 0) -> None:62self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})6364def encode(self):65return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}6667def _convert_keys(self, actions: dict[str, Any]):68out = {}69for k, v in actions.items():70if v is None:71continue72if k in ("x", "y"):73out[k] = int(v)74continue75splits = k.split("_")76new_key = splits[0] + "".join(v.title() for v in splits[1:])77out[new_key] = v78return out798081