python中uiautomator_Python+uiautomator2手机UI自动化测试实战 -- 2. 用法介绍

设备连接方法,有两种

1. 通过WiFi,假设设备IP 192.168.5.4和您的PC在同一网络中

import uiautomator2 as u2

d = u2.connect('192.168.5.4') # alias for u2.connect_wifi('192.168.5.4')

print(d.info)

2. 通过USB, 假设设备序列是123456789F(见adb devices)

import uiautomator2 as u2

d = u2.connect('123456789F') # alias for u2.connect_usb('123456789F')

print(d.info)

检查并维持设备端守护进程处于运行状态

d.healthcheck()

连接本地USB设备

需要设备曾经使用 python -m uiautomator2 init初始化过

d = u2.connect_usb("{Your-Device-Serial}")

如何停用UiAutomator的守护程序?

1. 直接打开uiautomator app(init成功后,就会安装上的),点击关闭UIAutomator

2.  d.service("uiautomator").stop()

打开调试开关

>>> d.debug = True

>>> d.info

12:32:47.182 $ curl -X POST -d '{"jsonrpc": "2.0", "id": "b80d3a488580be1f3e9cb3e926175310", "method": "deviceInfo", "params": {}}' 'http://127.0.0.1:54179/jsonrpc/0'

12:32:47.225 Response >>>

{"jsonrpc":"2.0","id":"b80d3a488580be1f3e9cb3e926175310","result":{"currentPackageName":"com.android.mms","displayHeight":1920,"displayRotation":0,"displaySizeDpX":360,"displaySizeDpY":640,"displayWidth":1080,"productName"

:"odin","screenOn":true,"sdkInt":25,"naturalOrientation":true}}

<<< END

安装应用,只能从URL安装

d.app_install(' http://some-domain.com/some.apk ')

安装应用

d.app_start(“ com.example.hello_world ”)# start包名称

停止应用

#相当于`am force-stop`,因此你可能丢失数据

d.app_stop( “ com.example.hello_world ”)

#相当于`pm clear`

d.app_clear( ' com.example.hello_world ')

推送和拉取文件

把文件推送到设备

# push to a folder

d.push("foo.txt", "/sdcard/")

# push and rename

d.push("foo.txt", "/sdcard/bar.txt")

# push fileobj

with open("foo.txt", 'rb') as f:

d.push(f, "/sdcard/")

# push and change file access mode

d.push("foo.sh", "/data/local/tmp/", mode=0o755)

从设备中提取文件

d.pull("/sdcard/tmp.txt", "tmp.txt")

# FileNotFoundError will raise if the file is not found on the device

d.pull("/sdcard/some-file-not-exists.txt", "tmp.txt")

跳过弹窗,禁止弹窗

d.disable_popups() #自动跳过弹出窗口

d.disable_popups(假)#禁用自动跳过弹出窗口

Session

Session represent an app lifestyle. 可用于启动应用,检测应用崩溃

启动应用

sess = d.session(“ com.netease.cloudmusic ”)

Attach to the running app

sess = d.session(“ com.netease.cloudmusic ”,attach = True)

检测应用崩溃

# When app is still running

sess(text="Music").click() # operation goes normal

# If app crash or quit

sess(text="Music").click() # raise SessionBrokenError

# other function calls under session will raise SessionBrokenError too

# check if session is ok.

# Warning: function name may change in the future

sess.running() # True or False

检索设备信息

获取基本信息

d.info

以下是可能的输出:

{

u'displayRotation': 0,

u'displaySizeDpY': 640,

u'displaySizeDpX': 360,

u'currentPackageName': u'com.android.launcher',

u'productName': u'takju',

u'displayWidth': 720,

u'sdkInt': 18,

u'displayHeight': 1184,

u'naturalOrientation': True

}

获取窗口大小

print(d.window_size())

# device upright output example: (1080, 1920)

# device horizontal output example: (1920, 1080)

获取最新的应用信息。对于某些Android设备,输出可能为空

print(d.current_app())

# Output example 1: {'activity': '.Client', 'package': 'com.netease.example', 'pid': 23710}

# Output example 2: {'activity': '.Client', 'package': 'com.netease.example'}

# Output example 3: {'activity': None, 'package': None}

获取设备序列号

print(d.serial)

# output example: 74aAEDR428Z9

关键事件

打开/关闭屏幕

d.screen_on()#打开屏幕

d.screen_off()#关闭屏幕

获取当前屏幕状

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值