Python-UiAutomator2实现Android自动化测试

本帖转自搜狗测试公众号

【一、前言】

       基于Python-UiAutomator2实现Android自动化测试,小编在Android应用的自动化性能测试中进行了实践。本篇将简单介绍python中使用adb、aapt命令的方法以及Python-UiAutomator2使用,后续文章将对环境搭建以及自动化性能测试进行介绍,希望能给大家一些微薄的帮助。

【二、python中使用adb、aapt命令】

在python中使用adb命令,可以导入os模块

1、简单的adb命令

如:os.system('adb devices)

2、稍微复杂的adb命令

如:os.system('adb shell "dumpsys activity | grep "com.sogou.map.android.***.*""')

3、将adb读出的内容保存(os.system不支持读取,os.popen支持读取)

如:out = os.popen('adb shell "dumpsys activity | grep 

"com.sogou.map.android.***.*""').read()

在python中使用aapt命令

aapt即Android Asset Packaging Tool,可以在Android SDK的platform-tools目录下找到该工具。aapt可以查看、创建、更新ZIP格式的文档,也可将资源文件编译成二进制文件。

1、 列出apk包的内容

aapt l[ist] [-v] [-a]file.{zip,jar,apk}

-v 以table形式列出来

-a 详细列出内容

2、 查看apk一些信息

aapt d[ump] [--values]WHAT file.{apk} [asset [asset ...]]

badging  Print the label and icon for the app declaredin APK

permissions  Print the permissions from the APK.

Resources  Print the resource table from the APK.

Configurations  Print the configurations in the APK.

Xmltree  Print the compiled xmls in the given assets.

Xmlstrings  Print the strings of the given compiled xmlassets.

例如:执行aapt d badging **.apk,可以看到其中包含了应用包名、版本号、permission等信息。

 

 

【三、Python-UiAutomator2使用】

1、UiAutomator

UiAutomator是google为Android平台开发的自动化测试框架,基本上支持所有的Android事件操作,主要是针对UI的自动化测试,支持Android 4.1以及更高的版本。

UiAutomator提供了以下两种工具来支持UI自动化测试:

(1). uiautomatorviewer:用来分析UI控件的图形界面工具,位于SDK目录下的tools文件夹中。

(2). uiautomator:一个java库,提供执行自动化测试的各种API。

是否能够用更脚本化的语言,例如Python,可以所见即所得地修改测试、运行测试?

非常感谢Xiaocong实现并分享(详见参考2),为Python和UiAutomator架了一座桥。

2、Python-UiAutomator2

python-uiautomator2是一个Android UI自动化框架,支持Python编写测试脚本对设备进行自动化。底层基于Google uiautomator2,允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,提高自动化代码编写的效率。原理是在手机上运行了一个http服务器,将uiautomator中的功能开放出来,再将这些http接口,封装成Python库。

支持平台及语言

python-uiautomator2主要分为两个部分,python客户端,移动设备

  • python端:运行脚本,并向移动设备发送HTTP请求

  • 移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

 

整个过程

(1).   在移动设备上安装atx-agent(守护进程),随后atx-agent启动uiautomator2服务(默认7912端口)进行监听;

(2).   在PC上编写测试脚本并执行(相当于发送HTTP请求到移动设备的server端);

(3).   移动设备通过WIFI或USB接收到PC上发来的HTTP请求,执行制定的操作。

python-uiautomator2代码示例

上面代码的作用是启动“搜狗地图”应用,然后点击“身边tab”,代码简洁、高效。

3、python-uiautomator2常用API介绍

3.1 获取机器的信息

d.info

d.window_size()

d.current_app()

d.serial  #获取设备序列号

d.wlan_ip  #获取无线局域网ip

3.2 屏幕相关的操作

开关屏幕,代码如下

d.screen_off()  #打开屏幕

d.screen_on()  #关闭屏幕

d.unlock()  #解锁屏幕

3.3 按键(软/硬)操作

d.press('back')

d.press('home')

还支持如下按键的操作,

home、back、left、right、up、down、center、menu、search、enter、recent(recent apps)、volume_up、volume_down、volume_mute、camera、power

3.4 手势相关的操作,包括短按/长按/滑动/拖拽

点击操作

d.click(x, y)

双击操作

d.double_click(x,y)

长按操作

d.long_click(x, y)

滑动操作

d.swipe(sx, sy, ex, ey)

d.swipe(sx, sy, ex, ey, steps=10)

拖拽操作

d.drag(sx, sy, ex, ey)

3.5 屏幕相关的操作

获取并设置屏幕的旋转方向

orientation = d.orientation

d.set_orientation("l") # or "left"

d.set_orientation("r") # or "right"

d.set_orientation("n") # or "natural"

冻结/解冻旋转功能

d.freeze_rotation()  # 冻结旋转

d.freeze_rotation(False)  # 解冻旋转

屏幕截图

d.screenshot("home.png")

获取屏幕层级(hierachy)XML

xml = d.dump_hierarchy()

打开通知栏或快速设置栏

d.open_notification()

d.open_quick_settings()

【四、参考文献】

参考

https://github.com/openatx/uiautomator2

https://github.com/xiaocong/uiautomator#uiautomator

https://blog.csdn.net/jgw2008/article/details/78286469

https://testerhome.com/topics/11357

猜你喜欢

转载自www.cnblogs.com/Yanss/p/10166512.html