Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_actions.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.16from typing import Optional1718from selenium.webdriver.remote.webelement import WebElement1920from . import interaction21from .interaction import Interaction22from .mouse_button import MouseButton23from .pointer_input import PointerInput242526class PointerActions(Interaction):27def __init__(self, source: Optional[PointerInput] = None, duration: int = 250):28"""29Args:30- source: PointerInput instance31- duration: override the default 250 msecs of DEFAULT_MOVE_DURATION in source32"""33if source is None:34source = PointerInput(interaction.POINTER_MOUSE, "mouse")35self.source = source36self._duration = duration37super().__init__(source)3839def pointer_down(40self,41button=MouseButton.LEFT,42width=None,43height=None,44pressure=None,45tangential_pressure=None,46tilt_x=None,47tilt_y=None,48twist=None,49altitude_angle=None,50azimuth_angle=None,51):52self._button_action(53"create_pointer_down",54button=button,55width=width,56height=height,57pressure=pressure,58tangential_pressure=tangential_pressure,59tilt_x=tilt_x,60tilt_y=tilt_y,61twist=twist,62altitude_angle=altitude_angle,63azimuth_angle=azimuth_angle,64)65return self6667def pointer_up(self, button=MouseButton.LEFT):68self._button_action("create_pointer_up", button=button)69return self7071def move_to(72self,73element,74x=0,75y=0,76width=None,77height=None,78pressure=None,79tangential_pressure=None,80tilt_x=None,81tilt_y=None,82twist=None,83altitude_angle=None,84azimuth_angle=None,85):86if not isinstance(element, WebElement):87raise AttributeError("move_to requires a WebElement")8889self.source.create_pointer_move(90origin=element,91duration=self._duration,92x=int(x),93y=int(y),94width=width,95height=height,96pressure=pressure,97tangential_pressure=tangential_pressure,98tilt_x=tilt_x,99tilt_y=tilt_y,100twist=twist,101altitude_angle=altitude_angle,102azimuth_angle=azimuth_angle,103)104return self105106def move_by(107self,108x,109y,110width=None,111height=None,112pressure=None,113tangential_pressure=None,114tilt_x=None,115tilt_y=None,116twist=None,117altitude_angle=None,118azimuth_angle=None,119):120self.source.create_pointer_move(121origin=interaction.POINTER,122duration=self._duration,123x=int(x),124y=int(y),125width=width,126height=height,127pressure=pressure,128tangential_pressure=tangential_pressure,129tilt_x=tilt_x,130tilt_y=tilt_y,131twist=twist,132altitude_angle=altitude_angle,133azimuth_angle=azimuth_angle,134)135return self136137def move_to_location(138self,139x,140y,141width=None,142height=None,143pressure=None,144tangential_pressure=None,145tilt_x=None,146tilt_y=None,147twist=None,148altitude_angle=None,149azimuth_angle=None,150):151self.source.create_pointer_move(152origin="viewport",153duration=self._duration,154x=int(x),155y=int(y),156width=width,157height=height,158pressure=pressure,159tangential_pressure=tangential_pressure,160tilt_x=tilt_x,161tilt_y=tilt_y,162twist=twist,163altitude_angle=altitude_angle,164azimuth_angle=azimuth_angle,165)166return self167168def click(self, element: Optional[WebElement] = None, button=MouseButton.LEFT):169if element:170self.move_to(element)171self.pointer_down(button)172self.pointer_up(button)173return self174175def context_click(self, element: Optional[WebElement] = None):176return self.click(element=element, button=MouseButton.RIGHT)177178def click_and_hold(self, element: Optional[WebElement] = None, button=MouseButton.LEFT):179if element:180self.move_to(element)181self.pointer_down(button=button)182return self183184def release(self, button=MouseButton.LEFT):185self.pointer_up(button=button)186return self187188def double_click(self, element: Optional[WebElement] = None):189if element:190self.move_to(element)191self.pointer_down(MouseButton.LEFT)192self.pointer_up(MouseButton.LEFT)193self.pointer_down(MouseButton.LEFT)194self.pointer_up(MouseButton.LEFT)195return self196197def pause(self, duration: float = 0):198self.source.create_pause(duration)199return self200201def _button_action(self, action, **kwargs):202meth = getattr(self.source, action)203meth(**kwargs)204return self205206207