『App自动化测试之Appium应用篇』| 元素定位工具uiautomatorviewer从简介、特点、启动到使用的完整过程

1 uiautomatorviewer简介

  • 之前文章说明了Appium Inspector的定位使用方法;
  • uiautomatorviewer是另一种定位工具;
  • uiautomatorviewerandroid-sdk自带的元素定位工具;
  • 它是通过截屏分析XML布局文件方式,来提供控件信息的查看服务。

2 uiautomatorviewer特点

  • uiautomatorviewerAppium Inspector有着明显的区别;
  • Appium Inspector功能相对比较强大;
  • uiautomatorviewer只能用于安卓系统;
  • 另外uiautomatorviewer只能用于元素定位,不支持通过该工具操作APP,也不支持录制功能等。

3 uiautomatorviewer启用

  • uiautomatorviewer位于SDK目录下的tools\目录下;
  • 比如我的是:D:\android-sdk-windows\tools
  • 双击uiautomatorviewer.bat进行启动即可:

4 Work Bar

  • Work Bar即工作栏,工作栏中有4个按钮,如图所示:

  • 从左到右依次为:

图标 名称 说明
1 Open 打开已保存的布局
2 Device Screenshot(uiautomator dump) 获取详细布局
3 Device Screenshot with Cpmpressed Hierarchy (uiautomator dump) 获取简介布局
4 Save 保存布局
  • 其中4保存时将保存两个文件,一个是图片,一个是.uix文件;
  • 可以查看图片显示:
  • 也可以查看XML格式内容:
python 复制代码
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
    <node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
        <node index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
            <node index="0" text="" resource-id="android:id/content" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
                <node index="0" text="" resource-id="com.android.launcher3:id/launcher" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
                    <node index="0" text="" resource-id="com.android.launcher3:id/drag_layer" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
                        <node index="0" text="" resource-id="com.android.launcher3:id/workspace" class="com.android.launcher3.Workspace" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="true" long-clickable="false" password="false" selected="false" bounds="[0,0][768,1184]">
                            <node index="1" text="" resource-id="" class="android.view.ViewGroup" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[8,184][760,976]">
                                <node index="0" text="" resource-id="" class="android.view.ViewGroup" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[8,184][760,976]">
                                    <node index="0" text="QQi" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="QQi" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[8,184][196,382]" />
                                    <node index="1" text="Settings" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Settings" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[384,778][572,976]" /></node>
                                <node index="1" text="" resource-id="" class="android.view.View" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[8,184][220,406]" /></node>
                        </node>
                        <node index="1" text="" resource-id="com.android.launcher3:id/search_drop_target_bar" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,48][768,184]">
                            <node index="0" text="" resource-id="" class="android.appwidget.AppWidgetHostView" package="com.android.launcher3" content-desc="Search" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,48][768,184]">
                                <node index="0" text="" resource-id="" class="android.widget.RelativeLayout" package="com.android.quicksearchbox" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,48][768,184]">
                                    <node index="0" text="" resource-id="com.android.quicksearchbox:id/search_plate" class="android.widget.LinearLayout" package="com.android.quicksearchbox" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,48][768,184]">
                                        <node NAF="true" index="0" text="" resource-id="com.android.quicksearchbox:id/search_icon" class="android.widget.ImageView" package="com.android.quicksearchbox" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[19,68][115,164]" />
                                        <node NAF="true" index="1" text="" resource-id="com.android.quicksearchbox:id/search_widget_text" class="android.widget.TextView" package="com.android.quicksearchbox" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[115,79][740,153]" /></node>
                                </node>
                            </node>
                        </node>
                        <node index="3" text="" resource-id="com.android.launcher3:id/page_indicator" class="android.widget.LinearLayout" package="com.android.launcher3" content-desc="Home screen 2 of 2, Settings" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,992][408,1016]">
                            <node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,992][384,1016]">
                                <node index="1" text="" resource-id="com.android.launcher3:id/inactive" class="android.widget.ImageView" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,992][384,1016]" /></node>
                            <node index="1" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[384,992][408,1016]">
                                <node index="0" text="" resource-id="com.android.launcher3:id/active" class="android.widget.ImageView" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[384,992][408,1016]" /></node>
                        </node>
                        <node index="4" text="" resource-id="com.android.launcher3:id/hotseat" class="android.widget.FrameLayout" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1016][768,1184]">
                            <node index="0" text="" resource-id="com.android.launcher3:id/layout" class="android.view.ViewGroup" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[0,1016][768,1184]">
                                <node index="0" text="" resource-id="" class="android.view.View" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[24,1016][192,1184]" />
                                <node index="1" text="" resource-id="" class="android.view.ViewGroup" package="com.android.launcher3" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[24,1016][768,1184]">
                                    <node index="0" text="Phone" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Phone" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[29,1022][163,1178]" />
                                    <node index="1" text="Contacts" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Contacts" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[173,1022][307,1178]" />
                                    <node index="2" text="" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Apps" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[317,1022][451,1178]" />
                                    <node index="3" text="Messaging" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Messaging" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[461,1022][595,1178]" />
                                    <node index="4" text="Browser" resource-id="" class="android.widget.TextView" package="com.android.launcher3" content-desc="Browser" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[605,1022][739,1178]" /></node>
                            </node>
                        </node>
                    </node>
                </node>
            </node>
        </node>
    </node>
</hierarchy>
  • 其中2会显示全部布局;
  • 其中3只显示部分有用的控件布局。

5 Screenshot

  • screenshot是截图区域,在图中的左侧部分;
  • 主要是显示当前屏幕的布局图片:

6 控件布局

  • 控件布局内容主要在右上方显示,以XML格式展示;

7 控件属性

  • 控件属性显示在右下方,当点击一个控件时,将会在这显示其属性信息:

8 连接设备

  • 直接点击工作栏中的第二个或第三个按钮即可连接到设备;
  • 我们这里点击的是第二个按钮Device Screenshot

9 Error while obtaining UI问题排查

  • 当我们点击第二个按钮Device Screenshot时出现以下界面:
  • 这个说明报错了,没有连接上,那我们看下详细信息,点击【Details】:
python 复制代码
Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!
  • 大概意思是Remote object doesn't exist,就是说已经存在了一个连接,那我们就需要断开已经连接的才行;
  • 我们检查发现使用了Appium-Inspector已经连接了,我们点击【×】断开这个会话连接:
  • 重新再连接就可以使用uiautomatorviewer连接上了。
相关推荐
q5673152318 分钟前
在 Bash 中获取 Python 模块变量列
开发语言·python·bash
是萝卜干呀19 分钟前
Backend - Python 爬取网页数据并保存在Excel文件中
python·excel·table·xlwt·爬取网页数据
代码欢乐豆20 分钟前
数据采集之selenium模拟登录
python·selenium·测试工具
狂奔solar1 小时前
yelp数据集上识别潜在的热门商家
开发语言·python
Tassel_YUE1 小时前
网络自动化04:python实现ACL匹配信息(主机与主机信息)
网络·python·自动化
聪明的墨菲特i1 小时前
Python爬虫学习
爬虫·python·学习
努力的家伙是不讨厌的2 小时前
解析json导出csv或者直接入库
开发语言·python·json
云空2 小时前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
凤枭香3 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
测试杂货铺3 小时前
外包干了2年,快要废了。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展