Path: blob/trunk/py/selenium/webdriver/common/actions/action_builder.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.161718from typing import Any, Optional, Union1920from selenium.webdriver.remote.command import Command2122from . import interaction23from .key_actions import KeyActions24from .key_input import KeyInput25from .pointer_actions import PointerActions26from .pointer_input import PointerInput27from .wheel_actions import WheelActions28from .wheel_input import WheelInput293031class ActionBuilder:32def __init__(33self,34driver,35mouse: Optional[PointerInput] = None,36wheel: Optional[WheelInput] = None,37keyboard: Optional[KeyInput] = None,38duration: int = 250,39) -> None:40mouse = mouse or PointerInput(interaction.POINTER_MOUSE, "mouse")41keyboard = keyboard or KeyInput(interaction.KEY)42wheel = wheel or WheelInput(interaction.WHEEL)43self.devices: list[Union[PointerInput, KeyInput, WheelInput]] = [mouse, keyboard, wheel]44self._key_action = KeyActions(keyboard)45self._pointer_action = PointerActions(mouse, duration=duration)46self._wheel_action = WheelActions(wheel)47self.driver = driver4849def get_device_with(self, name: str) -> Optional[Union["WheelInput", "PointerInput", "KeyInput"]]:50"""Get the device with the given name.5152Parameters:53-----------54name : str55The name of the device to get.5657Returns:58--------59Optional[Union[WheelInput, PointerInput, KeyInput]] : The device with the given name.60"""61return next(filter(lambda x: x == name, self.devices), None)6263@property64def pointer_inputs(self) -> list[PointerInput]:65return [device for device in self.devices if isinstance(device, PointerInput)]6667@property68def key_inputs(self) -> list[KeyInput]:69return [device for device in self.devices if isinstance(device, KeyInput)]7071@property72def key_action(self) -> KeyActions:73return self._key_action7475@property76def pointer_action(self) -> PointerActions:77return self._pointer_action7879@property80def wheel_action(self) -> WheelActions:81return self._wheel_action8283def add_key_input(self, name: str) -> KeyInput:84"""Add a new key input device to the action builder.8586Parameters:87-----------88name : str89The name of the key input device.9091Returns:92--------93KeyInput : The newly created key input device.9495Example:96--------97>>> action_builder = ActionBuilder(driver)98>>> action_builder.add_key_input(name="keyboard2")99"""100new_input = KeyInput(name)101self._add_input(new_input)102return new_input103104def add_pointer_input(self, kind: str, name: str) -> PointerInput:105"""Add a new pointer input device to the action builder.106107Parameters:108-----------109kind : str110The kind of pointer input device.111- "mouse"112- "touch"113- "pen"114115name : str116The name of the pointer input device.117118Returns:119--------120PointerInput : The newly created pointer input device.121122Example:123--------124>>> action_builder = ActionBuilder(driver)125>>> action_builder.add_pointer_input(kind="mouse", name="mouse")126"""127new_input = PointerInput(kind, name)128self._add_input(new_input)129return new_input130131def add_wheel_input(self, name: str) -> WheelInput:132"""Add a new wheel input device to the action builder.133134Parameters:135-----------136name : str137The name of the wheel input device.138139Returns:140--------141WheelInput : The newly created wheel input device.142143Example:144--------145>>> action_builder = ActionBuilder(driver)146>>> action_builder.add_wheel_input(name="wheel2")147"""148new_input = WheelInput(name)149self._add_input(new_input)150return new_input151152def perform(self) -> None:153"""Performs all stored actions.154155Example:156--------157>>> action_builder = ActionBuilder(driver)158>>> keyboard = action_builder.key_input159>>> el = driver.find_element(id: "some_id")160>>> action_builder.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys("keys").perform()161"""162enc: dict[str, list[Any]] = {"actions": []}163for device in self.devices:164encoded = device.encode()165if encoded["actions"]:166enc["actions"].append(encoded)167device.actions = []168self.driver.execute(Command.W3C_ACTIONS, enc)169170def clear_actions(self) -> None:171"""Clears actions that are already stored on the remote end.172173Example:174--------175>>> action_builder = ActionBuilder(driver)176>>> keyboard = action_builder.key_input177>>> el = driver.find_element(By.ID, "some_id")178>>> action_builder.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys("keys")179>>> action_builder.clear_actions()180"""181self.driver.execute(Command.W3C_CLEAR_ACTIONS)182183def _add_input(self, new_input: Union[KeyInput, PointerInput, WheelInput]) -> None:184"""Add a new input device to the action builder.185186Parameters:187-----------188new_input : Union[KeyInput, PointerInput, WheelInput]189The new input device to add.190"""191self.devices.append(new_input)192193194