Preface
Today we will talk about the new solutions of appium2.0+ single-touch and multi-touch. The editor will not say much nonsense. Remember to like and follow the editor!
Before appium2.0, the touch screen operation on mobile devices, the single-finger touch screen and the multi-finger touch screen were respectively implemented by the TouchAction class and the Multiaction class.
After appium2.0, these 2 methods will be discarded.
"[Deprecated] 'TouchAction' action is deprecated. Please use W3C actions instead."
, what is w3c action?
In the actions of w3c, the input source is divided into three categories:
- keyboard class - Key
- pointer class - Pointer
- None
For the Pointer pointer class input source, there are 3 types: Mouse mouse, Touch touch screen, Pen stroke
input source, which is a virtual device that provides input events.
Each input source is an input id and input source type. Like real devices, each input source has a state and an input event.
In the source code of python selenium, the InputDevices class in selenium/common/actions/input_devices.py defines the input source class.
, null input source (null input source)
provides the following behavior:
pause: No operation is done for a period of time, or the duration of the action
, keyboard input source (key input source)
provides the following behavior:
KeyDown: Press a key
KeyUp: Release a key
In the source code of python selenium, selenium/common/actions/key_input.py The KeyInput class defines the button input source class.
, pointer input source, provide the following behavior:
PointerDown: Press the mouse key, or touch screen or touch screen brush touch screen
PointerUp: Release the mouse key, or leave the screen with your hand, or touch screen pen with your screen
PointerMove: Move to a certain point on the screen
PointerCancel: Delete a pointer operation
in python selenium In the source code of selenium/common/actions/pointer_input.py, the PointerInput class defines the pointer input source class.
. Based on the input source, the keyboard operation class KeyActions
is defined. In the source code of python selenium, the KeyActions class in selenium/common/actions/key_actions.py defines the keyboard operation class.
. Based on the input source, the mouse/touch screen operation is defined. PointerActions class is defined:
In the source code of python selenium, the PointerActions class in selenium/common/actions/pointer_actions.py defines the mouse/touch screen operation class.
summary of the above classes:
, ActionBuilder class
Initialization method:
- input source device list, 2 input sources will be placed: mouse input source, keyboard input source.
- has 2 private properties: keyboard operation object (KeyActions class instantiation**)**, mouse/touch screen operation object (PointerActions class instantiation)
Attributes: key_action, pointer_action
In the ActionChains class, these 2 properties are used to call the mouse and keyboard operations.
Add new input sources: add_key_input, add_pointer_inputtml2
, ActionChains class
selenium, the mouse action class is used.
Initialization:
. Single touch - ActionChains class
directly use the ActionChains class, w3c_actions to implement it.
For example, the swipe screen sliding method in appium:
moves to a certain coordinate point → press → moves to another coordinate point → release
. Multi-touch - ActionChains type
multi-touch, is a single touch operation that occurs simultaneously, such as 2 fingers, and slides on the screen at the same time.
is still the ActionChains class, but you need to add a new single-touch operation inside.
actions = ActionChains(driver)# The input source device list is empty actions.w3c_actions.devices = []# Add a new input source to the device, the input source type is Touchnew_input = actions.w3c_actions.add_pointer_input('touch', f'finger{finger}')# The action of the input source: move to a certain point, press, move to another point, release new_input.create_pointer_move(x=start_x, y=start_y)new_input.create_pointer_down(MouseButton.LEFT)new_input.create_pause(duration / 1000)new_input.create_pointer_move(x=end_x, y=end_y)new_input.create_pointer_up(MouseButton.LEFT)# And so on, multiple input sources can be added to the device. It can be a mouse operation, or a touch screen, button, etc.
For example, the code for enlarging the Baidu Map app is as follows:
from time import sleepfrom appium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.actions.mouse_button import MouseButton#I want to open Baidu Map app desired_caps = {"automationName":"UiAutomator2","platformName":"Android","platformVersion":"7.1.2","deviceName":"HuaWei","noReset":True,"appPackage":"com.baidu.BaiduMap","appActivity":"com.baidu.baidumaps.WelcomeScreen","systemPort": 8225,"newCommandTimeout": 1200}#Connect appium server first. Pass the command. appium server connection address driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)sleep(20)# Get the size of the device - sizesize_dict = driver.get_window_size()# ============================================================================================================================================================================================================================================================================================================================================================================================================================================== 1st finger: Slide from the center to the upper right corner ======================#Add a new input source to the device, the input source type is Touch, the id is finger0new_input = actions.w3c_actions.add_pointer_input('touch','finger0')#The action of the input source: Move to a certain point, press, move to another point, release new_input.create_pointer_move(x=size_dict["width"] * 0.5, y=size_dict["height"] * 0.5)new_input.create_pointer_down()# new_input.create_pointer_down(MouseButton.LEFT)new_input.create_pause(0.2) # 200msnew_input.create_pointer_move(x=size_dict["width"] * 0.9, y=size_dict["height"] * 0.1)new_input.create_pointer_up(MouseButton.LEFT)# ==================================#Add a new input source to the device, and the input source type is Touch. id is finger1new_input = actions.w3c_actions.add_pointer_input('touch','finger1')#Input source action: move to a certain point, press, move to another point, release new_input.create_pointer_move(x=size_dict["width"] * 0.5, y=size_dict["height"] * 0.5)new_input.create_pointer_down()# new_input.create_pointer_down(MouseButton.LEFT)new_input.create_pause(0.2) # 200msnew_input.create_pointer_move(x=size_dict["width"] * 0.1, y=size_dict["height"] * 0.9)new_input.create_pointer_up(MouseButton.LEFT)#Execution action actions.perform()
This is all for today. Everyone is welcome to comment and communicate. If you like the editor, you can follow the editor.