Python 封装Swipe实现手机屏幕滑动操作

一. 封装Swipe目的

  1. Appium的滑动API:Swipe(int start x,int start y,int end x,int y,duration)
    使用前首先得获取手机屏幕的分辨率,然后根据分辨率计算得出滑动的起始和终点坐标。
    封装后的swipe,只需调用对应的方法即可实现 向上/向下/向左/向右/斜向上 常规滑动屏幕操作
  2. Appium的API,使用均依赖于webdriver的调用,必须先建立session连接才能实现swipe功能
    封装后的swipe,基于ADB command,只需手机与PC建立ADB连接即可实现swipe功能
  3. Appium的API通过建立不同的session 实现同时操作多部手机
    封装后的swipe,直接通过传入手机 serial Number 即可实现同时操作多部手机

综上,封装后的swipe可应用于Appium自动化测试并独立于Appium,且可以实现多部手机同时运作的功能。

二. Source Code

# -*- coding: utf-8 -*-

import os
import sys
import re


def get_screen_size(*args):
    """Get size of window and return the size for swipe function
       args 作为可变参数 表示是否传入设备的ID
    """
    if not args:
        # 如果没有传入指定设备ID,执行以下ADB command,获取设备屏幕分辨率
        size_file = os.popen("adb shell dumpsys window displays").read()
    else:
        # 如果传入指定设备ID,执行以下ADB command,获取设备屏幕分辨率
        uid = args[0]
        size_file = os.popen("adb -s %s shell dumpsys window displays" % uid).read()
    if not size_file:
        # 获取设备分辨率失败
        print("Cannot get the resolution of screen, please check the ADB.")
        sys.exit()
    else:
        # 正则表达式匹配设备分辨率
        size_match = re.search(r"(\d+)x(\d+)", size_file)
        if not size_match:
            # 设备分辨率匹配失败
            print("Failed to match the screen size.")
            sys.exit()
        else:
            # 设备分辨率信息从字符串分割转换为二元元组
            size_screen = re.split(r"x", size_match.group())
            print(size_screen)
            # 字符串元素元组转换为整型元素列表
            size = [int(size_screen[0]), int(size_screen[1])]
            return size


def swipe_up(*args, t=100, n=1):
    """Swipe device screen up in t milliseconds and repeat the operation n times
       t=100 作为命名关键字参数 表示默认的滑动时间为100ms 可自寻设计滑动时间
       n=1 作为命名关键字参数 表示默认的滑动次数为1次 可自寻设计滑动次数
    """
    size = get_screen_size(*args)
    x1 = size[0] * 0.5
    y1 = size[1] * 0.75
    x2 = size[0] * 0.5
    y2 = size[1] * 0.25
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_down(*args, t=100, n=1):
    """Swipe device screen down in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.5
    y1 = size[1] * 0.25
    x2 = size[0] * 0.5
    y2 = size[1] * 0.75
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_left(*args, t=100, n=1):
    """Swipe device screen left in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.95
    y1 = size[1] * 0.5
    x2 = size[0] * 0.05
    y2 = size[1] * 0.5
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_right(*args, t=100, n=1):
    """Swipe device screen right in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.05
    y1 = size[1] * 0.5
    x2 = size[0] * 0.95
    y2 = size[1] * 0.5
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_oblique(*args, t=100, n=1):
    """Swipe device screen oblique in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.05
    y1 = size[1] * 0.75
    x2 = size[0] * 0.95
    y2 = size[1] * 0.25
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容