python中uiautomator2環境安裝


uiautomator2簡介

什么是uiautomator2?

python-uiautomator2 是一個自動化測試開源工具,僅支持 Android 平台的原生應用測試。

原理:在手機上運行了一個http rpc服務,將uiautomator中的功能集成進來,然后再講這些http接口封裝成Python庫。

uiautomator2的優點

  • 環境搭建便捷
  • UI控件識別有專業工具weditor,可視化好
  • UI自動化編寫采用Python
  • UI自動化腳本運行穩定
  • 幫助文件是中文

官網https://github.com/openatx/uiautomator2

uiautomator2安裝

python環境:python3.7.1

pip install uiautomator2

安裝完成后查看uiautomator2,所有依賴的包如下圖:

uiautomator2對連接的移動設備進行初始化

執行命令:python -m uiautomator2 init    后自動下載下面依賴包

  • 自動下載uiautomator-server谷歌原生的uiautomator
  • atx-agent:uiautomator的守護進程
  • atx-agent增加遠程控制的功能,依賴minicap和minitouch這兩個工具

安裝前提條件:

移動設備是模擬器,設置--->開發者選擇中-->開啟USB調試

移動設備是安卓手機,通過usb線連接且必須開啟USB調試

執行:python -m uiautomator2 init  進行初始化操作

如圖對移動設備初始化完成,會在移動設備中安裝ATX包 

原理:移動設備上安裝atx-agent(守護進程),隨后atx-agent啟動uiautomator2服務(默認7912端口)進行監聽;

           在PC端使用Python編寫測試腳本並執行(相當於發送HTTP請求到移動設備的server端);

           移動設備通過WIFI或USB接收到PC上發來的HTTP請求,執行指定的操作;

查看端口命令:

adb shell   -->查到進程號  輸入命令:  busybox netstat -pt | grep 進程號   查看端口

測試代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 青城子
# datetime: 2021/4/11 22:48 
# ide: PyCharm

import uiautomator2 as u2
import time

d = u2.connect("127.0.0.1:62025")
# 打開debug模式,查看通訊流程
d.debug = True
# 查看簡單的設備信息
print(d.info)

輸出:

C:\Python\python.exe C:/work/imooc/u2_project/test2.py 11:34:01.871 $ curl -X POST -d '{"jsonrpc": "2.0", "id": "32cc7a24de4501c621d05b4c4f28e382", "method": "deviceInfo", "params": {}}' 'http://127.0.0.1:39356/jsonrpc/0'
11:34:02.112 Response (241 ms) >>> {"jsonrpc":"2.0","id":"32cc7a24de4501c621d05b4c4f28e382","result":{"currentPackageName":"com.android.settings","displayHeight":1280,"displayRotation":0,
"displaySizeDpX":480,"displaySizeDpY":853,"displayWidth":720,"productName":"VOG-AL10","screenOn":true,"sdkInt":22,"naturalOrientation":true}} <<< END {'currentPackageName': 'com.android.settings', 'displayHeight': 1280, 'displayRotation': 0, 'displaySizeDpX': 480, 'displaySizeDpY': 853, 'displayWidth': 720, 'productName':
'VOG-AL10', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM