This article is from the official account of the project: “AirtestProject” Copyright notice: It is allowed to be reproduced, but the original link must be retained. Do not use it for commercial or illegal purposes

preface

As well as clicking, sliding is a very common action during testing. The following sections describe how swipe is used under Airtest and POCO and how swipe is used to solve practical problems such as skipping APP introduction pages and continuous swiping.

Swipe interface under Airtest

Swipe allows you to swipe across the screen of your current device in one swipe and is available on Android, IOS and Windows. The parameters are as follows:

  • V1 – The starting point for sliding, either a Template image instance, or absolute coordinates (x, y)
  • V2 – The end of the slide, either a Template image instance or absolute coordinates (x, y)
  • Vector – The vector coordinates of the slide action, either absolute (x,y) or percentage of the screen, for example (0.5, 0.5)
  • **kwargs– Platform related parameterskwargs

Swipe has two ways of passing in parameters:

  • I go from v1 to V2,swipe(v1, v2), such as:
Swipe (672121 (4), (5) 336130)Copy the code
  • I’m going to slide from v1 along the vector,swipe(v1, vector=(x, y)), such as:
swipe(Template(r"tpl1574067097045.png", record_pos=(0.042, 0.145), resolution=(1080, 1920), vector=[-0.2666, -0.0005])Copy the code

An Exception will be raised when there are not enough parameters to swipe, such as when we pass only one parameter to the SWIPE interface, the run report will look like this:

Android and IOS

In addition to the above parameters, Swipe has several other special parameters for Android and IOS:

  • Duration – The length of time you swipe on the screen, 0.5 by default
  • Steps – Number of steps in a slide. Default is 5
  • Fingers – The number of fingers that can be swiped, either 1 or 2. The default is 1

If you want to customize the slide, such as changing the length of the slide to 1, the number of steps to 6, and using two fingers, you can pass in the following parameters:

Swipe (672121 (4), 336130 (5), duration = 1, steps = 6, fingers = 2)Copy the code
The Windows platform

Different from Android and IOS, the Swipe interface on Windows only has parameters related to duration and Steps. And duration defaults to 0.8.

Swipe interface at PoCO

In the POCO framework, swipe starts from a UI and slides to a point or a distance in a direction. Swipe can be done either with UI selected or without UI selected:

# coding=utf-8

from poco.drivers.unity3d import UnityPoco

poco = UnityPoco()

# select UI to swipe
# Swipe up the list view
poco('Scroll View'). Swipe ([0, 0.1])# Same code as above, and can also pass down/left/right
poco('Scroll View').swipe('up')  
# Scroll down the list view
poco('Scroll View').swipe('down')

Swipe is executed without selecting UI
x, y = poco('Scroll View'). Get_position () end = [x, y - 0.1] dir = [0, 0.1]Slide from point A to point B
poco.swipe([x, y], end)  
# Slide some distance from point A in A certain direction
poco.swipe([x, y], direction=dir)  
Copy the code

Swipe is selected from UI and the default starting point for swipe is the anchor point of the UI (i.e. the center point of the UI), so simply pass swipe an end point or slide direction and distance as described above.

Swipe needs to be passed the complete starting and ending coordinates to swipe, or the starting point and the direction and distance of movement, as shown in the code above. Poco’s Swipe also slides to scale, unlike Airtest:

Poco. Swipe ([0.9, 0.5], [0.1, 0.5])Copy the code
Special case of sliding — Drag (darg_to)

There is also a UI operation in POCO that is very similar to the SWIPE interface: drag (drag_to). It’s going from ONE UI to another, and essentially, dragging is a special case of sliding.

As shown here, we want to drag the star into the shell, so we use drag_to:

# coding=utf-8

from poco.drivers.unity3d import UnityPoco

poco = UnityPoco()

# Drag the star onto the shell
poco('star').drag_to(poco('shell'))
Copy the code

Swipe applies special scenarios

Swipe skips the introductory page of the APP

When the Koala app opens, there are four introduction pages that need to be swiped to access. If we pass the AIRtest/POCO UI test statement, it takes half a day to run. But if you do four fixed position slides, you can easily skip these four intro pages.

It should be noted that the continuous operation of the coordinate script is too fast, and the device may not respond. Generally, sleep(1.0) should be added after each statement to wait for the device to respond.

If there are too many of these, encapsulate them into generic functions that can be called whenever needed. Save code + fast ~

Get the height and width of the device
width, height = device().get_current_resolution()
# Calibrate the start and end of the slideStart_pt = (width * 0.9, height / 2) end_pt = (width * 0.1, height / 2)# Slide 5 times:
for i in range(5):
    swipe(start_pt, end_pt)
    sleep(1)  Wait for a response from the device
Copy the code

Of course, in addition to calculating the resolution and coordinates ourselves and then using Airtest’s Swipe, we can also use poCO’s Swipe to scale to skip the intro pages:

for i inRange (5) : poco. Swipe ([0.9, 0.5], [0.1, 0.5]) sleep (1)Wait for a response from the device
Copy the code

This may seem to save us the steps of obtaining the height and width of the device and calculating the sliding coordinates, but in fact the running speed will be greatly reduced:



Airtest
swipe

Use swipe_along for continuous swiping

Sometimes on an Android phone, we need to perform a continuous swipe_along operation (for example, swipe_along to unlock the screen). The example code is as follows:

from airtest.core.api import *
dev = device()  Get the current mobile device
# Slide your finger across the three coordinates in sequence
dev.minitouch.swipe_along([(100, 100), (200, 200), (300, 300)])
Copy the code

Click options — Settings — check the real-time coordinate display to display the absolute coordinates of the mobile phone screen on the screen. Right click the mouse can also copy the coordinates to the clipboard, which can easily realize some requirements of sliding between coordinates, as shown in the picture:

Note that this interface is currently only available when using the default Minitouch mode.

Follow the public account below, you can view more past tutorials; Join our official test development forum by replying to “Forum”