"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/py/selenium/webdriver/common/actions/pointer_input.py" (17 Feb 2023, 2863 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "pointer_input.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.7.0_vs_4.8.0.

    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 import typing
   18 
   19 from selenium.common.exceptions import InvalidArgumentException
   20 from selenium.webdriver.remote.webelement import WebElement
   21 
   22 from .input_device import InputDevice
   23 from .interaction import POINTER
   24 from .interaction import 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(self, duration=DEFAULT_MOVE_DURATION, x=0, y=0, origin=None, **kwargs):
   39         action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs}
   40         if isinstance(origin, WebElement):
   41             action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
   42         elif origin is not None:
   43             action["origin"] = origin
   44         self.add_action(self._convert_keys(action))
   45 
   46     def create_pointer_down(self, **kwargs):
   47         data = {"type": "pointerDown", "duration": 0, **kwargs}
   48         self.add_action(self._convert_keys(data))
   49 
   50     def create_pointer_up(self, button):
   51         self.add_action({"type": "pointerUp", "duration": 0, "button": button})
   52 
   53     def create_pointer_cancel(self):
   54         self.add_action({"type": "pointerCancel"})
   55 
   56     def create_pause(self, pause_duration):
   57         self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})
   58 
   59     def encode(self):
   60         return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}
   61 
   62     def _convert_keys(self, actions: typing.Dict[str, typing.Any]):
   63         out = {}
   64         for k, v in actions.items():
   65             if v is None:
   66                 continue
   67             if k in ("x", "y"):
   68                 out[k] = int(v)
   69                 continue
   70             splits = k.split("_")
   71             new_key = splits[0] + "".join(v.title() for v in splits[1:])
   72             out[new_key] = v
   73         return out