admin管理员组

文章数量:1559742

uiautomator2连接逍遥模拟器

【1】安装 python

安装python:

【2】安装 adb

[1]下载adb:

需要翻墙

下载完成后直接解压安装包

[2]配置环境变量

1.打开电脑的设置,选择“高级系统设置”后,再点击“环境变量设置”

2.选择 系统变量中的“PATH”

3.将解压后的adb文件地址,配置在环境变量中。
(如果需要配置Android SDK中的ADB,那么地址就是“{Android SDK安装地址}/android_sdk/platform-tools/”)

4.配置成功后保存,在命令行输入“adb”,输出结果如下,则代表adb已安装成功。

【3】安装uiautomator2

pip安装

pip install uiautomator2

python终端为手机下载atx

python -m uiautomator2 init # 手机端安装ATX

【4】连接手机

# 连接手机
devices = ['127.0.0.1:21503']  # , '127.0.0.1:21513'
devices_object = [u2.connect(device) for device in devices]

【5】安装weditor

# 可能会安装失败
pip install -i https://pypi.douban.com/simple weditor # 豆瓣安装
pip install --pre -U weditor

# 上面如果安装失败的话 直接安装指定版本 0.6.4 / 0.6.3
pip install --upgrade weditor==0.6.4

安装成功后直接在控制台输入:

weditor

接着会自动打开浏览器

如果设备为成功链接请先在终端输入adb指令查看设备是否连接

adb devicers

【6】控制u2元素常用指令

import datetime
import os
import time

class axis():# # 连接手机
    # devices = ['127.0.0.1:21503']  # , '127.0.0.1:21513'
    # devices_object = [u2.connect(device) for device in devices]
    
    def start_app(self, devices_object, app_name):
        # 启动app
        for i, device in enumerate(devices_object):
            device(text=app_name).click()
    
    def show_app(self, devices_object):
        # 展示运行中的app
        for i, device in enumerate(devices_object):
            print(f"人造人{i + 1}号:{device.app_current()}")
    
    def show_all(self, devices_object):
        # 展示所有app
        for i, device in enumerate(devices_object):
            print(f"人造人{i + 1}号:{device.app_list_running()}")
    
    def app_info(self, devices_object):
        # app信息
        for i, device in enumerate(devices_object):
            print(device.info)
    
    def windows_size(self, devices_object):
        # 显示分辨率
        for i, device in enumerate(devices_object):
            print(f"人造人{i + 1}号:屏幕宽:{device.window_size()[1]} 高:{device.window_size()[0]}")
    
    def screen_shot(self, devices_object):
        # 截图
        now_time = datetime.datetime.now().strftime('%m-%d %H-%M-%S')
        folder_path = f'img/{now_time}'
        os.makedirs(folder_path, exist_ok=True)
        for i, device in enumerate(devices_object):
            screen_path = os.path.join(folder_path, f'人造人{i + 1}号.png')
            device.screenshot(screen_path)
            print(f"截图已保存:{screen_path}")
    
    def app_download(self, devices_object, url):
        # 下载app url传入参数为安装包apk:
        for i, device in enumerate(devices_object):
            device.app_install(url)
    
    def click(self, devices_object, x, y):
        # 点击事件 x,y为坐标轴
        for i, device in enumerate(devices_object):
            # screen_width, screen_height = device.window_size()
            # target_x = 0.49
            # target_y = 0.25
            # # 计算实际点击坐标d.click(0.49, 0.25)
            # click_x = int(1074)
            # click_y = int(231)
            device.click(x, y)

【7】adb常用命令

adb devices # 查看当前设备
adb shell pm list packages # 查看手机里面所有包名
adb shell pm list packages -3 # 查看手机里面所有第三方包名
adb shell dumpsys activity activities  # 该命令的功能是获取当前正在被操作的app的activity相关信息
adb shell pm clear com.ablesky.ui.activity # 清除缓存数据

# 启动程序 
# 格式:常规命令 + 包名/activity
# 注意:appActivity返回值本来是com.jingdong.app.mall/.main.MainActivity,需要去掉中间的反斜线!
adb shell am start -n com.jingdong.app.mall/com.jingdong.app.mall.MainFrameActivity 

adb shell pm clear com.jingdong.app.mall # 关闭程序并且清除所有数据,相当于重新安装
adb shell am force-stop com.jingdong.app.mall # 停止运行程序
adb shell "ps | grep com.jingdong.app.mall" # 查看程序进程,判断是否运行
adb shell /system/bin/screencap -p /sdcard/xx.png # 截频并保存文件在手机上为xx.Png
adb pull /sdcard/xx.png D:/xx.png # 将手机xx.png文件保存到D盘文件下
adb push D:/xx.text /adcard/xx.text # 将电脑文件导入手机
adb shell screenrecord /sdcard/demo.mp4 # 录制视频

adb shell input text 123  # 输入内容

adb shell input keyevent 4 # 按键操作
# 4 返回操作
# 1 菜单
# 3 主页
# 21 光标左移
# 22 光标右移
# 67 删除
# 61 tab

adb shell input tap 282 923 # 点击某个坐标
adb shell input swipe 300 1000 300 500 # 下滑500px
adb shell input swipe 288 929 288 929 1000 # 长按1000ms

本文标签: 模拟器逍遥