zoukankan      html  css  js  c++  java
  • uiautomator2

    import uiautomator2 as u2

    # 安装weditor 来找元素
    # pip install --pre weditor
    # python -m weditor 初始化

    # pip install --pre uiautomator2
    # pip install pillow
    # python uiautomator2 init 初始化 ATX

    # d = u2.connect('127.0.0.1:7555')
    d = u2.connect_usb() # 一台机器不带参数
    print(d.device_info)
    d.app_start('pkg_name name')
    d.app_stop('pkgname')
    d.app_install(url='')
    d.pull(src='shouji', dst='dianao') # 文件传输
    d.push(src='diannao', dst='shouji')
    d.disable_popups() # 跳过弹窗
    d.unlock() # 解锁屏幕

    s = d.session(pkg_name='xxxx', attach=True)
    s.implicitly_wait(5)
    s(resourceId='xxx').click()
    s(resourceMatches='.*xxx').click() # id模糊匹配
    s(discription='xxx').click()

    try:
    d(text='12', instance=1).click() # instance下标1的
    except:
    pass

    s().send_keys()
    s().long_click()
    s().clear_text()
    s().left() # 通过左边元素定位自己
    s().swipe('up', 1)
    s().pinch_out() #放大
    s().child() # 子元素
    s().parent() # 父元素
    s().sibling() # 通过兄弟
    s(text='', discription='xx', resourceId='').click()
    s().wait(timeout=3) # 等待元素出现
    s().wait_gone(timeout=2) # 等待元素消失

    s.click(100, 200) # 点击屏幕坐标
    s.press('bakc') # 手机按键
    s.screenshot # 截屏
    s.make_toast(text='tip', duration=2)
    s.screen_on() # 屏幕开
    s.screen_off()
    s.double_click(100, 200, 0.2) # 双击0.2间隔
    s.drag(1, 2, 3, 4) # 从1,2拖到3,4
    s.wait_timeout = 5
    ==============================================================
    一、要求
    python 3.6+
    android 4.4+
     
    二、介绍

    uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作。

    三、地址
     
    四、安装
    1、安装uiautomator2
    pip install --pre uiautomator2
    pip install pillow
     
    2、初始化

    部署相关的守护进程。

    电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

    python -m uiautomator2 init

    安装完成,设备上会多一个uiautomator的应用。

    配置手机设备参数:

    有两种方法,一种是通过WIFI,另一种是通过USB数据线将手机链接电脑。

    WiFi连接更方便一点,需要保持PC和手机使用的一个WIFI,查看手机连接WIFI的IP地址。

    3、测试

    import uiautomator2 as u2

    d = u2.connect('127.0.0.1::6555')

    print(d.info)

    打印结果:

    {'currentPackageName': 'com.android.launcher', 'displayHeight': 1280, 'displayRotation': 1, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'DUK-AL20', 'screenOn': True, 'sdkInt': 23, 'naturalOrientation': False}

    五、元素定位

    1、查看app控件

    我们可以借助Android SDK自的uiautomatorviewer查看元素,这就要求手机必须以USB的方式连接PC,我前面使用的是WIFI连接进行连接的。所以,openatx提供了另外一个工具weditor 来解决这个问题。

    GitHub地址:https://github.com/openatx/weditor

    (1)、安装:

    pip install --pre --upgrade weditor
    2)、使用
    python3 -m weditor
    3)、工具打开
    默认会通过浏览器打开页面:http://atx.open.netease.com/
    (4)工具的操作步骤
    选择android、输入手机或者模拟器的ip+端口,点击connect
    dump hierarchy是用来刷新页面的
    鼠标点击想要的元素,就可以查看他们的控件了
     
     
    2、主要语法
     
    (1)启动app
    d.app_start("com.addcn.android.house591")

    (2)关闭app
    cls.d.app_stop("com.addcn.android.house591")

    (3)ResourceId定位
    cls.d(resourceId="com.addcn.android.house591:id/ad_banner").click()

    (4)Text定位
    d(text="精选").click()

    (5)Description定位
    d(description="..").click()

    (6)ClassName定位
    d(className="android.widget.TextView").click()

    (7)xpath定位
    d.xpath("//*[@content-desc='分享']").click()

    (8)


    3、其他操作
    1)#组默认元素等待超时(秒)
    cls.d.wait_timeout = 20.0 #默认20

    (2)元素拖拽
    (3)开关点击
    • d(A).left(B), selects B on the left side of A.
    • d(A).right(B), selects B on the right side of A.
    • d(A).up(B), selects B above A.
    • d(A).down(B), selects B under A.
    • 例如:
      #选择“Wi-Fi”右侧的“开关” 
      d(text="Wi‑Fi").right(className="android.widget.Switch").click()


    (4)获取/统计某个相同条件的数目
    d(text="Add new").count
    或者
    len(d(text="Add new"))
    得知数目之后,我们可以通过索引去定位
    d(text="Add new")[0]
    d(text="Add new")[1]
    也可以遍历
     
    for view in d(text="Add new"):
        view.info 

    (5)截图
    #截取屏幕截图并保存到计算机上的文件中,要求Android> = 4.2。
    d.screenshot( “ home.jpg ”) # get PIL.Image格式化图像。当然,你需要首先安装pillow  
    image = d.screenshot() # default format =“pillow” image.save( “ home.jpg ”)#或home.png。目前,只有PNG和JPG支持

    #得到OpenCV的格式图像。当然,你需要先安装numpy和cv2
    import cv2
    image = d.screenshot( format = ' opencv') cv2.imwrite( ' home.jpg '图像)#获取原始JPEG数据 imagebin = d.screenshot(格式= '原始') 打开( “ some.jpg ”, “ WB ”).WRITE(imagebin)

    (6)手势操作
    1、单击
    d( text = “ Settings ”).click()

    2、长按
    d( text = “ Settings ”).long_click()


    3、将对象拖向另一个点或另一个UI对象
    #笔记:拖不能用于为Android <4.3。
    #将UI对象拖动到屏幕点(x,y),0.5秒后 
    d( text = “设置”).drag_to(x,y, duration = 0.5)
    #将UI对象拖动到另一个(中心位置) UI对象,在0.25秒 
    d( text = “设置”).drag_to( text = Clock ”, duration = 0.25)

    4、在屏幕上滑动
    # swipe from (sx, sy) to (ex, ey)
    d.swipe(sx, sy, ex, ey)
    # swipe from (sx, sy) to (ex, ey) with 10 steps
    d.swipe(sx, sy, ex, ey, steps=10)


    5、在屏幕上拖拽
    # drag from (sx, sy) to (ex, ey)
    d.drag(sx, sy, ex, ey)
    # drag from (sx, sy) to (ex, ey) with 10 steps
    d.drag(sx, sy, ex, ey, steps=10)




    (7)获取对象信息和状态
    1、
    d(text="Settings").exists 
    #如果存在则为True,否则为假
    or d.exists(text="Settings") # 进一步使用 d(text="Settings").exists(timeout=3) 
    # 等待设置出现在3S,相同.wait(3)

    2、检索特定UI对象的信息
    d(text="Settings").info

    3、获取/设置/清除可编辑字段的文本(例如,EditText小部件)
    d(text = “ Settings ”).get_text()   # get widget text 
    d(text = “ Settings ”).set_text(“ My text ... ”)   #设置文本 
    d(text = “ Settings ”).clear_text( )   #清除文字、


    (8)系统常用按键
    # press home key
    d.press.home()
    # press back key
    d.press.back()
    # the normal way to press back key
    d.press("back")----亲测可用
    # press keycode 0x07('0') with META ALT(0x02) on
    d.press(0x07, 0x02)

    • home                   #手机Home键
    • back                   #手机返回键
    • left                   #对应键盘上的向右键<-
    • right                  #对应键盘上的向右键->
    • up                    #对应键盘上的向上键
    • down                   #对应键盘上的向下键
    • center                  #选中?
    • menu                   #菜单
    • search                  #查找?
    • enter                  #对应键盘上的Enter键
    • delete(or del)                          #对应键盘上的DEL键 用于删除
    • recent(recent apps)                  #任务切换
    • volume_up                #声音向上调整
    • volume_down               #声音向下调整
    • volume_mute               #静音按键
    • camera                  #拍照
    • power                   #电源键



    六、使用经验
    1、使用前初始化
    python -m uiautomator2 init
    2、打开工具
    python3 -m weditor



  • 相关阅读:
    「Vijos 1282」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法照片
    「Vijos 1285」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法药水
    「Vijos 1284」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法阵
    「Vijos 1283」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔杖
    「2018-12-02模拟赛」T3 约束排列 解题报告
    「2018-12-02模拟赛」T2 种树 解题报告
    「2018-12-02模拟赛」T1 最短路 解题报告
    「分块系列」公主的朋友 解题报告
    「分块系列」「洛谷P4168 [Violet]」蒲公英 解题报告
    Java高级架构师(一)第03节:多模块多Web应用合并War包
  • 原文地址:https://www.cnblogs.com/yaohu/p/12588978.html
Copyright © 2011-2022 走看看