Appium analog based single point or multipoint touchscreen operation

A single touch

TouchAction class: A series of operations in a chain, the chain is then passed to the server, the server accepts the chain, each parsing operation, performed by one, TouchAction class provides the following methods:

  • Short laying on: press
  • Long press: langPress
  • Click: tap
  • Mobile to: move_to
  • Wait: wait
  • Release: release
  • Execution: perform
  • Cancel: cancel

1. Single Touch

  Example squares to draw (assuming the Z-type drawing pattern):

  

  Attribute value of the element bounds the region [90,542] [990,1442], respectively, it represents the coordinates of the upper left corner and lower right corner of the coordinate position, a standard is calculated to be square, circular button from each of the distribution , we can average six equal length and width, can be positioned by the coordinate values ​​of the center position of each point, we want to achieve by appium squares drawn performed as follows:

  • Step 1: Define the starting point coordinates (the upper left corner coordinates of the point), according to the object webdriver squared positioning elements, calling location method, suggested adding explicit wait
  • Step 2: determining the overall size of the squares, squared positioning the object according webdriver elements, calling a method size
  • Step 3: calculate the step size obtained by the above analysis is equally divided into six equal parts, then step on the overall length of 1/6
  • Step 4: determining the position coordinates of each point, determined by one (step of adding or subtracting) by the position of a point, you may have an initial reference coordinate calculation
  • Step 5: performing drawing operation, a first short point hold the next slide, until the last point of release, to complete the drawing action, each action need to add an appropriate wait, the code needs to call the last perform ( ) method of performing these actions

  Squares to achieve the following codes:

# Define the starting point coordinate 
loc_nine_button = (MobileBy.ID, ' com.xxzb.fenwoo: ID / gesturepwd_create_lockview ' )
wait.until(EC.visibility_of_element_located(loc_nine_button))
ele = driver.find_element(*loc_nine_button)

Start = ele.location   # starting point coordinates

# Determined magnitude squared entire 
size = ele.size

# Calculated step size 
STEP size = [ ' width ' ] /. 6

# First coordinate point 
Pl = (Start [ ' X ' ] + STEP, Start [ ' Y ' ] + STEP)

# Second point coordinates 
P2 = (Pl [0] + * STEP 2, Pl [. 1 ])

# Third point coordinates 
P3 = (P2 [0] + * STEP 2, P2 [. 1 ])

# Coordinate fourth point 
P4 = (P3 [0] - STEP * 2, P3 [. 1] + 2 * STEP )

# Coordinate fifth point 
P5 = (P4 [0] - STEP * 2, P4 [. 1] + 2 * STEP )

# Sixth coordinate point 
P6 = (P5 [0] + * STEP 2, P5 [. 1 ])

# Seventh coordinate point 
P7 = (P6 [0] + * 2 STEP, P6 [. 1 ])

# Implementation of action drawn 
TouchAction (driver) .press (
    x=P1[0], y=P1[1]).wait(200).move_to(
    x=P2[0], y=P2[1]).wait(200).move_to(
    x=P3[0], y=P3[1]).wait(200).move_to(
    x=P4[0], y=P4[1]).wait(200).move_to(
    x=P5[0], y=P5[1]).wait(200).move_to(
    x=P6[0], y=P6[1]).wait(200).move_to(
    x=P7[0], y=P7[1]).wait(200).release().perform()

  We use animation to show a drawing process Jiugongge

  

2. Slide list

  There is a scene, when we use the phone Taobao commodity screening time, because too many goods, we manually slide the screen, so that the back of the display of goods in front of us, a list like this slide, appium How to achieve it? Next, in order to do certain exemplary app exam, analysis of the specific steps are as follows:

  • Step 1: Get the size of the entire screen of the phone, call get_window_size under the driver () method
  • Step 2: List slide call driver.swipe () method, passing in the initial position and the end position of the slide, there is no need accurately to a certain point, a rough estimate of the starting position of the slide on the line, calculated as a percentage

  List slide code as follows:

# Get the size of the entire screen 
size = driver.get_window_size ()

# Slide upwardly exam 
driver.swipe (start_x size = [ ' width ' ] * 0.5 ,
             start_y=size['height'] * 0.9,
             end_x=size['width'] * 0.5,
             end_y=size['height'] * 0.1,
             duration=200)

  We use animation effects to a slide show process list

  

Second, multi-touch

 MultiAction categories: Providing multi-touch manner, using the enlargement and reduction in the high side of the scene, the following class only add () and perform () method, the principle is the merged synchronization executing a plurality of tracks, just adding to the action of each track add () method can be

1. Zoom

  German map with high zoom, for example, implementation steps as follows:

  • Step 1: Get the entire screen, calling get_window_size under Driver () method
  • Step 2: Define the center of the map
  • Step 3: two zoom points defined initial position
  • Step 4: Define two sliding zooming operation, a single reference single touch slide operation implemented method
  • Step 5: performing multi-touch, the use of add () method adds two sliding action
  • Step 6: performing multi-touch operation calls perform () method

  After following exception is thrown when executing high moral map, zoom-out action before adding the operation click on the screen to solve

   Multi-touch scale code is as follows:

# Get the size of the entire screen 
size = driver.get_window_size ()

# 定义地图的中心点
center_point = (size['width'] * 0.5, size['height'] * 0.5)

# 定义两个外点
out_point_01 = (size['width'] * 0.1, size['height'] * 0.1)

out_point_02 = (size['width'] * 0.9, size['height'] * 0.9)

# # 点击一下屏幕
TouchAction(driver).tap(x=size['width'] * 0.5, y=size['height'] * 0.2).wait(200).perform()

# 设置放大的动作
a1 = TouchAction(driver).press(x=out_point_01[0], y=out_point_01[1]).wait(200).move_to(x=center_point[0], y=center_point[1]).wait(200)

a2 = TouchAction(driver).press(x=out_point_02[0], y=out_point_02[1]).wait(200).move_to(x=center_point[0], y=center_point[1]).wait(200)

# 执行多点触控
m = MultiAction(driver)
m.add(a1, a2)
m.perform()

  我们用一个动画效果来展示高德地图缩放的过程

  

2.多指点击

  有一些场景需要用到多指点击或者长按才能实现预期的结果,此时我们可以使用driver.tap()方法,参考源码的描述,需要注意几点:

  • 最多支持5个指头
  • 只有点击和长按两种动作
  • 当duration=None时为点击,duration不为None时为长按,长按的时间根据传入的duration值决定
  • 第一个参数positions需要传入坐标,并且格式为嵌套元组的列表,第二个参数为duration,单位ms,例如:[(100, 20), (100, 60), (100, 100)], 500

Guess you like

Origin www.cnblogs.com/xiaogongjin/p/11762539.html