四、appium实现九宫格滑动和双指缩放操作--TouchAction/MultiAction

1.TouchAction

a.from appium.webDriver.common.touch_action import TouchAction

b.获取一个对象 TouchAction(driver)

c.press(self,el=None,x=None,y=None,pressure=None) 按压传入元素或者坐标,pressure ios only

d.longPress(self,el=None,x=None,y=None,duration=1000) 长按x ms

e.tap(self,el=None,x=None,y=None,count=1)点击

f.move_to(self,el=None,x=None,y=None) 移动

g.wait(self,ms=0) 暂停,单位毫秒

h.release(self) 通过将指针移离屏幕来结束操作

i.perform() 执行,将命令发送到服务器

2.MultiAction

a.from appium.webdriver.common.multi_action import MultiAction

b.add(self,*touch_actions) 添加一个或者多个TouchAction对象,稍后执行

c.multi_action0 = MultiAction(driver)

multi_action0.add(touch_action0,touch_action1)

multi_action0.perform()

实践

1.以微信的支付密码锁为例,用TouchAction实现绘图解锁

获取源坐标和屏幕宽度用的是reno,测试方法是否可行,换用的是findx2

 

 

# user/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "Cc"

from appium import webdriver
import time
import logging
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction

from output_log import output_log


def get_size(my_device):
    """
    获取屏幕尺寸
    :param my_device:
    :return: width和height,元组
    """
    width = my_device.get_window_size()['width']  # get_window_size()返回的是字典
    height = my_device.get_window_size()['height']
    return width, height


def get_point(my_device1, x, y):
    """
    获取起始的坐标点,和终点坐标.这里先假设手机A上起始坐标点是(x,y),屏幕尺寸50,100
    :param my_device1:
    :param x: 原手机横坐标
    :param y: 原手机中坐标
    :return: 目标坐标
    """
    window_size = get_size(my_device1)
    width = window_size[0]
    height = window_size[1]
    x_0 = x*width/1080
    y_0 = y*height/2196
    return x_0, y_0


if __name__ == "__main__":
    desired_caps_a = {'platformName': "Android",
                      'platFormVersion': "9",
                      'deviceName': "df93a63a",
                      'appPackage': "com.tencent.mm",
                      'appActivity': "com.tencent.mm.ui.LauncherUI",
                      'noReset': True,
                      'unicodeKeyboard': True,
                      'resetKeyboard': True
                      }
    device_a = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps_a)
    my_logging = output_log(logging.DEBUG)
    device_a.implicitly_wait(5)
    w, h = get_size(device_a)
    screen_size_msg = "size" + str(w) + "-" + str(h)
    my_logging.debug(screen_size_msg)
    device_a.find_element_by_android_uiautomator('new UiSelector().text("我")').click()
    device_a.find_element_by_android_uiautomator('new UiSelector().text("支付")').click()
    x0, y0 = get_point(device_a, 248, 676)
    x1, y1 = get_point(device_a, 248, 989)
    x2, y2 = get_point(device_a, 248, 1271)
    WebDriverWait(device_a, 5).until(lambda x: x.find_element_by_android_uiautomator('new UiSelector().text("手势密码")'))
    TouchAction(device_a).press(x=x0, y=y0).wait(1000).move_to(x=x1, y=y1).wait(1000).\
        move_to(x=x2, y=y2).release().perform()

2.以微信会话的图片为例,实现双指放大

if __name__ == "__main__":
    desired_caps_a = {'platformName': "Android",
                      'platFormVersion': "9",
                      'deviceName': "df93a63a",
                      'appPackage': "com.tencent.mm",
                      'appActivity': "com.tencent.mm.ui.LauncherUI",
                      'noReset': True,
                      'unicodeKeyboard': True,
                      'resetKeyboard': True
                      }
    device_a = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps_a)
    my_logging = output_log(logging.DEBUG)
    device_a.implicitly_wait(5)
    w, h = get_size(device_a)  # 获取现在测试手机的屏幕尺寸
    screen_size_msg = "size" + str(w) + "-" + str(h)
    my_logging.debug(screen_size_msg)

    # 九宫格操作
    # device_a.find_element_by_android_uiautomator('new UiSelector().text("我")').click()
    # device_a.find_element_by_android_uiautomator('new UiSelector().text("支付")').click()
    # x0, y0 = get_point(device_a, 248, 676)
    # x1, y1 = get_point(device_a, 248, 989)
    # x2, y2 = get_point(device_a, 248, 1271)
    # WebDriverWait(device_a, 5).until(lambda x: x.find_element_by_android_uiautomator('new UiSelector().text("手势密码")'))
    # TouchAction(device_a).press(x=x0, y=y0).wait(1000).move_to(x=x1, y=y1).wait(1000).\
    #     move_to(x=x2, y=y2).release().perform()

    # 双指放大图片
    device_a.find_element_by_android_uiautomator('new UiSelector().text("我的测试群")').click()
    time.sleep(1)
    x_tap0, y_tap0 = get_point(device_a, 266, 537)
    device_a.tap([(x_tap0, y_tap0)])  # 多点点击,可以设置点击的时长,单位ms,最多可五个手指同时点击
    right_x, right_y = get_point(device_a, 543, 1014)
    time.sleep(1)
    fig_a = TouchAction(device_a)
    fig_a.press(x=x_tap0, y=y_tap0).wait(1000).move_to(x=0, y=y_tap0).wait(1000).release()
    fig_b = TouchAction(device_a)
    fig_b.press(x=right_x, y=right_y).wait(1000).move_to(x=w, y=right_y).wait(1000).release()
    figs = MultiAction(device_a)
    figs.add(fig_a, fig_b)
    figs.perform()
    device_a.save_screenshot("multiAction.png")
    device_a.quit()

3.遇到的报错

a.

device_a.tap([x_tap0, y_tap0], 100) 
TypeError: 'float' object is not subscriptable

这里的写法错了。tap([(x0, y0),...]duration=None) 参数1是数组,元素是元组,这里传入的是整型/float,注意duration单位是ms

b.程序运行结束,但是没有双指滑动效果:注意加等待时间

 

posted @ 2020-04-16 20:20  Cc905  阅读(663)  评论(0编辑  收藏  举报