Exclusive hard goods: appium2 0 + new solutions for single touch and multi touch


♥ preface

At appium2 Before 0, the touch screen operation on mobile devices, single finger touch screen and multi finger touch screen were implemented by TouchAction class and multi action class respectively.

At appium2 After 0, these 2 methods will be discarded.

"[Deprecated] 'TouchAction' action is deprecated. Please use W3C actions instead."

1. What is w3c action?

In w3c's actions, input sources are divided into three categories:

  1. Keyboard class - Key
  2. Pointer class - pointer
  3. None

There are three types of Pointer type input sources: Mouse, Touch screen and Pen stroke

An input source is a virtual device that provides input events.

Each input source is an input id and an input source type. Like real devices, each input source has status and input events.

In the source code of python selenium, selenium / common / actions / input_ devices. The InputDevices class in py defines the input source class.

1. Null input source

Provide the following behaviors:

pause: do nothing for a period of time, or the duration of the action

2. Key input source

Provide the following behaviors:

KeyDown: press a key

KeyUp: release a key

In the source code of python selenium, selenium / common / actions / key_ input. The KeyInput class in py defines the button input source class.

3. Pointer input source provides the following behaviors:

PointerDown: press the mouse button, or touch screen or touch screen pen touch screen

PointerUp: release the mouse button, or leave the screen with your hand, or leave the screen with your stylus

PointerMove: move to a point on the screen

PointerCancel: deletes a pointer operation

In the source code of python selenium, selenium/common/actions/pointer_input.py defines the pointer input source class.

4. Based on the input source, the keyboard operation class KeyActions is defined

In the source code of python selenium, selenium / common / actions / key_ actions. The KeyActions class in py defines the keyboard operation class.

5. Based on the input source, the PointerActions class of mouse / touch screen operation is defined:

In the source code of python selenium, selenium / common / actions / pointer_ actions. The PointerActions class in py defines the mouse / touch screen operation class.

Summarize the above categories:

6. ActionBuilder class

Initialization method:

Input source device list, there will be two input sources: mouse input source and keyboard input source.

There are two private properties: keyboard operation object (KeyActions class instantiation * *) *, mouse / touch screen operation object (PointerActions class instantiation)

Attribute: key_action,pointer_action

In the ActionChains class, these two properties are used to call mouse and keyboard operations.

Add a new input source: add_key_input,add_pointer_input

2. ActionChains class

The mouse operation class and mouse behavior in selenium are ActionBuilder classes.

initialization:

3. Single touch - ActionChains class

Directly use the in ActionChains class, w3c_actions to implement.

For example, the swipe sliding screen method in appium:

Move to a coordinate point → press → move to another coordinate point → release

4. Multi touch - ActionChains class

Multi touch is a single touch operation that occurs simultaneously, such as two fingers sliding on the screen at the same time.

It is still the ActionChains class, but you need to add a new single touch operation in it.

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 Touch
new_input = actions.w3c_actions.add_pointer_input('touch', f'finger{finger}')
# Input the action of the source: move to a 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, you can add multiple input source operations to the device. It can be mouse operation, touch screen, key press and other operations

For example, the code for zooming in on Baidu map app is as follows:

from time import sleep
from appium import webdriver

from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.mouse_button import MouseButton

#I want to be in Android 7 1.2 on the device, 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 instructions. 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 - size
size_dict = driver.get_window_size()

# ==========Zoom in on the map: slide diagonally from the center of the map to zoom in - 2 fingers slide at the same time==================
actions = ActionChains(driver)
#The input source device list is empty
actions.w3c_actions.devices = []

# ==========The first finger: slide from the center to the upper right corner==================
#Add a new input source to the device. The input source type is touch and the ID is finger0
new_input = actions.w3c_actions.add_pointer_input('touch','finger0')
#Input the action of the source: move to a 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(MouseButton.LEFT)
new_input.create_pause(0.2) # 200ms
new_input.create_pointer_move(x=size_dict["width"] * 0.9, y=size_dict["height"] * 0.1)
new_input.create_pointer_up(MouseButton.LEFT)

# ==========Second finger: slide from the center to the lower left corner==================
#Add a new input source to the device. The input source type is Touch. id is finger1
new_input = actions.w3c_actions.add_pointer_input('touch','finger1')
#Input the action of the source: move to a 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(MouseButton.LEFT)
new_input.create_pause(0.2) # 200ms
new_input.create_pointer_move(x=size_dict["width"] * 0.1, y=size_dict["height"] * 0.9)
new_input.create_pointer_up(MouseButton.LEFT)

#Execute action
actions.perform()

Now there is such an opportunity. I invite you to join our software testing learning exchange group: 914172719. Note "csdn". You can discuss and exchange software testing together, learn software testing technology, interview and other aspects of software testing together, and have free live classes to gain more testing skills. Let's advance Python automated testing / test development together, The road to high pay.

Let's give you a word of encouragement: when our ability is insufficient, the first thing to do is internal practice! When our ability is strong enough, we can look outside!

Finally, we have prepared a matching learning resource for you. You can get a 216 page Software Test Engineer Interview document information without official account: heartbroken spicy bar. And the corresponding video learning tutorials for free!, The materials include basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced test continuation, test architecture, development test framework, performance test, security test, etc.

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Another software testing post, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer should be enclosed in double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

As a test engineer with 1 year working experience, my suggestions before the interview are as follows

"After a year in office, the automation software test hired by high salary was persuaded to quit."

4 months of self-study software testing into Ali! How to move from functional testing to automation... What have I experienced

6000 yuan applied for the training course. Three months later, I successfully "cheated" into Tencent's big factory with a monthly salary of 15000 yuan

Keywords: Python Programmer unit testing software testing IT

Added by WesPear on Wed, 05 Jan 2022 17:43:35 +0200