一. 封装Swipe目的

Appium的滑动API:Swipe(int start x,int start y,int end x,int y,duration)

使用前首先得获取手机屏幕的分辨率,然后根据分辨率计算得出滑动的起始和终点坐标。

封装后的swipe,只需调用对应的方法即可实现 向上/向下/向左/向右/斜向上 常规滑动屏幕操作。

Appium的API,使用均依赖于webdriver的调用,必须先建立session连接才能实现swipe功能

封装后的swipe,基于ADB command,只需手机与PC建立ADB连接即可实现swipe功能。

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))

python模拟手机屏幕滑动_Python 封装Swipe实现手机屏幕滑动操作相关推荐

  1. python模拟网页点击_python怎么模拟点击网页按钮

    python怎么模拟点击网页按钮 前提环境: Python3 和 Visual Studio Code安装完毕 . 安装selenium : 在终端输入: pip install selenium, ...

  2. python模拟购物车购物过程_Python 模拟购物车的实例讲解

    1.功能简介 此程序模拟用户登陆商城后购买商品操作.可实现用户登陆.商品购买.历史消费记查询.余额和消费信息更新等功能.首次登陆输入初始账户资金,后续登陆则从文件获取上次消费后的余额,每次购买商品后会 ...

  3. python模拟网页点击_python模拟点击

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python怎么模拟点击网页按钮前提环境:python3 和 visual st ...

  4. python模拟登录163邮箱_python模拟登陆163邮箱并下载邮件内容(第三版代码片段)

    python模拟登陆163邮箱并下载邮件内容(第三版代码片段) 注意: 1 由于163邮箱有众多版本,所以登录请求URL,邮件URL等可能也不太一样,下面是163的简约3.0 2 代码缺乏错误处理能力 ...

  5. python模拟银行存取款_python 模拟银行转账功能过程详解

    首先画出流程图,流程图与现实代码有出入,因为刚开始画流程图的时候,有些东西没考虑进去,后来写着写着就慢慢能想起来并实现了. 另有一点经验推荐给新手朋友,如果说碰到一个项目无从下手的话,就先慢慢去写,写 ...

  6. python模拟登录详细教程_Python模拟登录requests.Session应用详解

    最近由于某些原因,需要用到Python模拟登录网站,但是以前对这块并不了解,而且目标网站的登录方法较为复杂, 所以一下卡在这里了,于是我决定从简单的模拟开始,逐渐深入地研究下这块. 注:本文仅为交流学 ...

  7. python模拟登录163邮箱_python模拟登录网易邮箱-阿里云开发者社区

    python模拟登录网易邮箱 #coding:utf-8 import urllib2,urllib import cookielib from bs4 import BeautifulSoup #设 ...

  8. python控制苹果手机实现自动功能_Python实现macOS中简单的自动操作

    工作中有套系统有一项操作要进行反复的简单操作,基本就是按钮点击.最近在学习python,就想着能不能写个脚本做这件事情. 基本思路就是利用pyautogui库识别屏幕中的按钮并进行一系列点击操作,因为 ...

  9. python写一个自动登录脚本_Python 实现自动登录+点击+滑动验证功能

    需要用到的库有selenium,还需要安装Chrome浏览器驱动,具体如何安装我就不详述了 在这里我模拟了csdn的登录过程 ** 1**.首先打开网页,用户名+密码登录,然后定位用户名输入框,和密码 ...

  10. python 模拟微信浏览器请求_python+requests对app和微信小程序进行接口测试

    对于web端和app端的接口测试来说,他们都是通过请求方法,url和传递的body参数进行接口请求,区别web和app的区别就是header请求的不同.不同的地方在于header中的User-Agen ...

最新文章

  1. xp系统vba服务器,VBA获取操作系统的版本号(支持windows xp,windows 2003 ,win7 ,win10)
  2. 好程序员Web前端分享无法忽视的JavaScript技巧
  3. Shiro <shiro:hasPermission >标签不生效,shiro权限不生效原因
  4. git回滚到任意版本
  5. 2021年武大CS\南大CS\哈工CS\浙软\西交CS\天大佐治亚CS\中科院信网中心面试经验贴
  6. Git上传Github及基本操作
  7. 信息学奥赛一本通(1006:A+B问题)
  8. 真降价还是假环保?华为客服回应手机取消充电器:不清楚
  9. yum安装jdk1.8
  10. C++语言函数重载详解和示例
  11. Keras Datasets 国内下载镜像
  12. 前端设计必备-Font awesome 插件使用菜鸟言语
  13. HTML中Css补充资料
  14. 计算机网络——局域网网络结构以及 VLAN 划分
  15. OSS实现多文件多线程的断点下载(java)
  16. html三因子模型,R语言Fama-French三因子模型实际应用:优化投资组合
  17. Newton-Raphson算法
  18. Mockjs-官网学习总结
  19. 嵌入式AI开发:Maixduino目标识别分类
  20. 和Leo一起做爱线段树的好孩子HDU5238 Calculator

热门文章

  1. Python爬虫爬取中国大学慕课MOOC课程的课程评价信息(讨论信息),采用selenium动态爬取方法
  2. used in key specification without a key length
  3. 彩色图片用opencv批量转成黑底白底
  4. urp教务系统简单利用
  5. 【立创开源】ESP8266制作的1.44寸TFT显示屏太空人天气时钟(st7735)(增加农历显示)(抄作业)
  6. Dao和Repository,你还傻傻分不清吗?
  7. 使用Foxmail管理hotmail邮箱时,只能接收邮件而无法发送邮件的就解决办法
  8. hdu 5442 (后缀数组)
  9. CSDN产品周报第31期|PC端开放账号注销功能
  10. 一个30岁转行程序员的心路历程