uiautomator2+python自动化测试1-环境准备

前言

uiautomator是Google提供的用来做安卓自动化测试的一个Java库。功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:

  1. 测试脚本只能使用Java语言
  2. 测试脚本必须每次被上传到设备上运行。

我们希望测试能够用Python编写,能够在电脑上运行的时候就控制手机,于是就有了python-uiautomator,后来又有了python-uiautomator2,详情查看github地址https://github.com/openatx/uiautomator2

环境准备
python  3.6
uiautomator2           0.3.3

uiautomator2工作原理

分为两个部分:

PC上的python端:运行脚本,并向系统设备发送http请求
移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

详情可以参考这篇:https://blog.csdn.net/zhuhuahong/article/details/81868671

1.安装uiautomator2

使用pip3安装uiautomator2 ,安装的过程中有点坑,用pip install --pre -U uiautomator2一直安装失败,找了很多教程都是这个。
最后去github上找了下教程https://github.com/openatx/uiautomator2,发现用pip3安装,抱着试试的心态,安装成功了
需cd到python的Scripts目录,再使用pip3安装

E:\python36\Scripts>pip3 install -U uiautomator2

2.adb连上手机

确保手机和电脑能连得上,使用adb devices查看链接状态,adb相关的环境本篇就不多说了,搞android测试的都知道

E:\python36\Scripts>adb devices List of devices attached adb server version (31) doesn't match this client (40); killing... * daemon started successfully emulator-5554 device

安装atx-agent

python -m uiautomator2 init

E:\python36\Scripts>python -m uiautomator2 init [D 190716 23:34:53 __main__:28] use cache directory: C:\Users\dell\.uiautomator2 Namespace(serial=None, server=None, subparser='init') init [I 190716 23:34:53 __main__:88] >>> Initial device AdbDevice(serial=emulator-5554) [I 190716 23:34:53 __main__:172] Install minicap, minitouch [Kminitouch |################################| 33.5K / 33.5K [?25h[D 190716 23:34:56 __main__:157] Push https://github.com/openatx/stf-binaries/raw/master/node_modules/minitouch-prebuilt/prebuilt/x86/bin/minitouch -> /data/local/tmp/minitouch:0755 [I 190716 23:34:56 __main__:176] abi:x86 seems to be android emulator, skip install minicap [I 190716 23:34:56 __main__:182] Install com.github.uiautomator, com.github.uiautomator.test [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator') [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test') [Kapp-uiautomator.apk |################################| 1.7M / 1.7M.7M [?25h[D 190716 23:35:05 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator.apk -> /data/local/tmp/app-uiautomator.apk:0644 [D 190716 23:35:05 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk') [Kapp-uiautomator-test.apk |################################| 966.8K / 966.8K [?25h[D 190716 23:35:11 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator-test.apk -> /data/local/tmp/app-uiautomator-test.apk:0644 [D 190716 23:35:11 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk') [I 190716 23:35:18 __main__:193] Install atx-agent [Katx-agent_0.6.0_linux_386.tar.gz |################################| 4.0M / 4.0M.0M [?25h[D 190716 23:35:23 __main__:157] Push https://github.com/openatx/atx-agent/releases/download/0.6.0/atx-agent_0.6.0_linux_386.tar.gz -> /data/local/tmp/atx-agent:0755 [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop') [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '-d') [I 190716 23:35:26 __main__:202] Check install [D 190716 23:35:26 __main__:212] Forward: local:tcp:53461 -> remote:tcp:7912 [D 190716 23:35:28 __main__:214] atx-agent version 0.6.0 Successfully init AdbDevice(serial=emulator-5554)

看到Successfully就是安装成功了(当然总是有几个小伙伴的手机需要换下了!!!)

连接手机

python-uiautomator2连接手机的方式有两种,一种是通过WIFI,另外一种是通过USB

首先查看uiautomator状态是启动状态

1.usb连接手机

先使用adb devices获取手机的唯一识别序列号,也就是devices前面的那一串:emulator-5554

E:\python36\Scripts>adb devices
List of devices attached
emulator-5554   device

测试是否连接成功,先打开你的手机上的任意app,比如淘宝,cmd输入以下信息

E:\python36\Scripts>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import uiautomator2 as u2 >>> d = u2.connect_usb('emulator-5554') >>> d.info {'currentPackageName': 'com.taobao.taobao', 'displayHeight': 1280, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'h60-l01', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True} >>>

看到输出’currentPackageName’: 'com.taobao.taobao’说明连接成功了

2.wifi连接

首先保证手机和电脑在一个局域网,可以先ping 手机ip,看能不能ping的通

>>> import uiautomator2 as u2 >>> d = u2.connect('192.168.1.xx') >>> d.info

(0)

相关推荐

  • 一篇文章教你使用Python自动赚取支付宝积分,网友:发家致富?

    本文介绍如何利用Python+uiautomator2 每日自动赚取支付宝积分. 支付宝的积分有啥用?诱惑诱惑你: 可以兑换视频网站的VIP会员. 可以兑换各种优惠券. 可以在年底活动中兑换苹果手机. ...

  • appium

    文章目录 appium 1.appium可以测试的对象 2.自动化混合APP的条件 3.总结 4.查看webview版本 5.APP面试点 6.代码部分 方式1: 设置搜索webview 方式2: 输 ...

  • 30 行代码实现蚂蚁森林自动偷能量

    给Python开发者加星标,提升Python技能) 来源:CSDN- xindoo 虽然我支付宝加了好多好友,平时有很多能量可以偷,但由于太懒,至今一棵树都没种成,所以心心念念把偷能量这事自动化.之前 ...

  • 记一次 逆向 autoJs 的过程

    一.准备: 一个 用autoJs 写的 Apk: AndroidKiller 工具: adb工具: JAVA SDK: android studio: adb  java  环境变量配置好; andr ...

  • uiautomator2+python自动化测试2-抓元素利器weditor

    前言 android sdk里面自带的uiautomatorviewer.bat可以查看手机app上的元素,但是不太好用,网上找了个大牛写的weditor,试用了下还是蛮不错的 python环境:3. ...

  • selenium+python在mac环境上的搭建

    前言 mac自带了python2.7的环境,所以在mac上安装selenium环境是非常简单的,输入2个指令就能安装好 需要安装的软件: 1.pip 2.selenium2.53.6 3.Firefo ...

  • 《python自动化测试全栈》课程-(7/8/9)3个月

    当他们学Python时候, 我没有报名 --因为我不做自动化测试: 接着他们学selenium, 我没有报名 --因为我不做自动化测试: 然后他们学python接口自动化, 我没有报名 --因为我不是 ...

  • Python集成开发环境有哪些?常见种类!

    Python是非常容易学习的编程语言,对于初学者来说十分容易入门,当我们进行Python学习的时候挑选一个合适的开发工具,可以更好的提高我们的效率.那么Python开发工具有哪些?小编为大家介绍一下. ...

  • 我被学妹白嫖了,python自动化测试经验全部传授,没想到最后竟然......

    先来一张学妹美照: 事情是这样的:学妹刚入门的软件测试,薪水只有5000元左右,想转自动测试或者初入测试岗位,要拿到年薪15-25W,问我应该怎样学习 作为一个入行8年的测试老鸟,这不就到我装逼的时候 ...

  • Python自动化测试框架分类!

    python是一门高级的编程语言,拥有众多框架,而自动化测试是python的应用领域之一,那么你知道Python自动化测试框架有哪些吗?我们一起来看看吧. 1.Unittest 是python内置的标 ...

  • 面试| Python 自动化测试面试经典题目回顾

    过完春节,一共参加了 5 个公司的面试.第一家公司,通过了 2 轮技术面试,但是薪资不够理想.第二个公司,被面试的测试专家虐的不要不要的

  • python自动化测试需要知识

    一.概念: 设计程序自动执行测试用例,从而减少人工消耗在功能测试上的大量时间,提高测试效率的一种测试方法.从自动化测试的概念便可以看出学习自动化测试的重点:设计程序,也就是编写代码. 二.前提准备:掌 ...

  • Python自动化测试怎么学?看完这个!不做无头苍蝇

    前言 很多粉丝私信我,叫我出一期完整且系统的Python自动化测试全栈学习路线.历时半个月,我给大家整理了一套完整的Python自动化测试学习路线,跟着学完,保证能够学会!对自学的朋友能有很大的帮助! ...