使用python编写adb脚本实现自动化测试

您所在的位置:网站首页 python做自动化脚本 使用python编写adb脚本实现自动化测试

使用python编写adb脚本实现自动化测试

2023-09-18 16:23| 来源: 网络整理| 查看: 265

使用python编写adb脚本实现自动化测试 前言一、python与adb二、使用步骤1.基本原理2.方法1:使用os.system()函数实现2.方法2:使用os.popen()函数实现2.方法3:使用subprocess.Popen()函数实现 三、总结滴水穿石,卷卷卷!!!

前言

上一篇文章讲解了adb的基本命令的使用,里面采用window系统的cmd命令提示符来实现adb命令的输入,虽说cmd命令提示符也可以实现adb命令控制,但实际编写脚本不方便,因而进行自动化脚本编写时,我们通常采用python来进行脚本的编写,此处就简单介绍一下使用python编写adb脚本实现自动化测试。

一、python与adb

安装好adb环境、python和pycharm,下面的实验主要使用pycharm软件来进行。

二、使用步骤 1.基本原理

在window系统上实现python对adb命令的调用可以使用三种方法。其中分别用到了os库与subprocess库,借助这两个库实现对adb命令的编辑。下面用运行python脚本实现adb的连接的实验来进行介绍。在进行以下实验时先确保可以通过cmd进行adb命令的连接和使用。

import os import subprocess 2.方法1:使用os.system()函数实现

代码如下(示例):

#此函数可实现adb的连接和进行一次点击事件 import os #连接adb def adb_connect1(IP): os.system(f"adb connect {IP}") # 点击事件 def touch1(x, y): os.system(f"adb shell input tap {x} {y}") if __name__ == "__main__": os.system("adb connect 127.0.0.1:7555") os.system("adb shell input tap 216 310") #adb_connect1('127.0.0.1:7555') #touch1(216,310)

如代码可见,此处通过os.system()函数来进行adb命令的实现:格式为os.system(“adb命令”),无返回值,此处的adb命令为上一篇文章所讲解的adb基本命令,python通过os库实现了对cmd命令行的输入。如此处的os.system(“adb connect 127.0.0.1:7555”)就相当于在window的cmd里执行adb connect 127.0.0.1:7555。同时我们可以利用python来实现某些特定的功能,比如可以建立函数adb_connect(IP),这样要使用的时候只需要调用这个函数便可,os.system(f"adb connect {IP}“)这里面的os.system(f” {IP} “)的f” {IP} "写法可以让函数传递IP变量,便于函数的后续操作与调用。

2.方法2:使用os.popen()函数实现

代码如下(示例):

import os #连接adb def adb_connect2(IP): output_date = os.popen(f"adb connect {IP}") print(output_date.read()) # 点击事件 def touch2(x, y): output_date = os.popen(f"adb shell input tap {x} {y}") print(output_date.read()) if __name__ == "__main__": adb_connect2('127.0.0.1:7555') touch2(216,310)

如代码可见,此处通过os.popen()函数来进行adb命令的实现:os.popen(“adb命令”),os.popen()函数相较于os.system()是具有返回值的,这里把返回值保存至output_date变量,并通过print(output_date.read())输出。目前这是我使用得比较多的一种方法,通过返回值还可以作一些判断。

2.方法3:使用subprocess.Popen()函数实现

代码如下(示例):

import os import subprocess #连接adb def adb_connect3(): p = subprocess.Popen("adb connect 127.0.0.1:7555", shell=True, close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(str(p.communicate())) def touch3(x, y): p = subprocess.Popen(f"adb shell input tap {x} {y}", shell=True, close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(str(p.communicate())) if __name__ == "__main__": adb_connect3() touch3(216,310)

如代码可见,此处通过subprocess.Popen()函数来进行adb命令的实现:subprocess.Popen(“adb命令”,shell=True,等函数各参数…),此函数也有返回值,功能也更高级和全面一些,此处只简单列举了调用cmd实现adb命令的方法,其各参数的具体使用方法有兴趣的可以去了解一下,此处只简单说明window系统通过python的subprocess库实现adb的命令实现,后续深入时可自行继续探究。

三、总结

本文仅仅简单介绍了window系统下使用python编写adb脚本的方法,借助python的语法,便于我们对adb指令的操作和灵活使用,从而实现各种复杂的自动化测试脚本的编写。以下的代码便是对adb的input指令的python脚本写法,调用对应的函数,可实现点击、输入文字、滑动、按键、拖动等操作,通过利用python语法将其进行组合处理便可变化出各种自动化脚本,实现基本的UI自动化测试。

adb命令的input系列指令的python函数代码如下(示例):

import os import time #连接adb def adb_connect1(IP): os.system(f"adb connect {IP}") #adb_shell终端指令输入 def adb_cmd(cmd): output_date = os.popen(f"adb shell {cmd}") print(output_date.read()) #模拟鼠标的点击事件,x,y为点击的坐标 def touch(x, y): adb_cmd(f'input tap {x} {y}') #模拟文字输入事件,text输入字符串 def text(text): adb_cmd(f'input text {text}') #模拟滑动事件,x1,y1为起始坐标,x2,y2为终点坐标,t为滑动的时间 def swipe(x1,y1,x2,y2,t): adb_cmd(f'input swipe {x1} {y1} {x2} {y2} {t}') #模拟按键事件,3代表回主页按键,4代表返回按键 def keyevent(x): adb_cmd(f'input keyevent {x}') #模拟拖动事件,x1,y1为起始坐标,x2,y2为终点坐标,t为拖动的时间 def draganddrop(x1,y1,x2,y2,t): adb_cmd(f'input draganddrop {x1} {y1} {x2} {y2} {t}') if __name__ == "__main__": adb_connect1(IP) touch(x, y) text(text) swipe(x1,y1,x2,y2,t) keyevent(x) draganddrop(x1,y1,x2,y2,t) 滴水穿石,卷卷卷!!!


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3