[UiAutomator] UiSelector中使用instance与index方法定位控件的区别
在使用UiAutomator写测试用例时,最常用到的就是控件查找操作。
在UiSelector中,有两个定位控件的方法,一个是instance,一个是index。那么这两个方法有什么区别呢?
首先,我们看一下官方api说明:
instance(int instance):
Set the search criteria to match the widget by its instance number. The instance value must be 0 or greater, where the first instance is 0. For example, to simulate a user click on the third image that is enabled in a UI screen, you could specify a a search criteria where the instance is 2, the className(String) matches the image widget class, and enabled(boolean) is true. The code would look like this: new UiSelector().className("android.widget.ImageView") .enabled(true).instance(2);
index(int index):
Set the search criteria to match the widget by its node index in the layout hierarchy. The index value must be 0 or greater. Using the index can be unreliable and should only be used as a last resort for matching. Instead, consider using the instance(int) method.
也就是说instance方法会将界面上所有相同类型的控件按顺序取出来,放到一个集合里(暂且这么理解吧,不知道放哪里了,囧),然后按照控件在集合的角标把想要的控件取出来;而index则是通过该控件所在层级的节点角标将对应的控件取出来。
那么这两个方法到底是怎么使用的呢?看下面的例子:
首先我们通过xml定义一个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="textview1"android:textSize="22sp" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="button1" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="textview2"android:textSize="22sp" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="button2" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="textview3"android:textSize="22sp" /></LinearLayout>
使用UiAutomatorViewer截出来的图是这样的:
按照说明,使用index方法获取TextView控件是这样的:
UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").index(0)); // textview1 UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").index(2)); // textview2 UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").index(4)); // textview3
哎,等等,尼玛!为啥我第一个方法取到的UiObject是“TestUI”?原来在我们的TitleBar上也有一个TextView控件,而它的节点角标也是0(见下图)。这是不是太坑爹了?先不要埋怨,人家api文档都说的很清楚了,这是一个不靠谱(unreliable)的方法,其他方法都不好使了才建议去尝试此方法。
下面把使用instance方法获取TextView控件的方法写出来:
UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(0)); // TestUI
UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(1)); // textview1 UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(2)); // textview2 UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(3)); // textview3
怎么样,使用instance方法就靠谱多了吧。
本次分享到此结束,欢迎大家与我一起交流。
============================2014-12-25 分割线===================
今天上网看博客,发现index方法还有一种用法,就是在UiObject.getChild()方法里使用。还是以上面的UI为例。
如果我们想要获取textview1对应的TextView控件,首先找到它的父控件LinearLayout,而LinearLayout又是FrameLayout的子控件(如下图)。
所以,获取textview1的代码大概是这样:
UiObject viewObj = new UiObject(new UiSelector().className("android.view.View")); // 获取View控件 UiObject flObj = viewObj.getChild(new UiSelector().index(1)); // 获取FrameLayout控件 UiObject llObj = flObj.getChild(new UiSelector().index(0)); // 获取LinearLayout控件 UiObject tv1Obj = llObj.getChild(new UiSelector().index(0)); // 获取textview1对应的TextView控件
怎么样?还是instance好用吧!
转载于:https://www.cnblogs.com/lovexiaov/p/uiauto_lovexiaov.html
[UiAutomator] UiSelector中使用instance与index方法定位控件的区别相关推荐
- C#中使用Invalidate()与MouseMove方法刷新控件时,控件闪烁问题解决方法
1. 问题出现场景 当我们在使用C# 中的绘图功能时,如果需要动态改变控件UI的状态,比如绘制了一个圆,点击时填充圆颜色为绿色.那么我们必须使用Invalidate()函数进行刷新.运行程序之后会发现 ...
- 查看ocx控件方法_Appium自动化测试入门教程No.8——定位控件
Appium 通过 uiautomatorviewer.bat 工具来查看控件的属性.该工具位于 Android SDK 的 /tools/bin/ 目录下. 常用的Appium定位控件有如下: 一. ...
- VS2017中创建使用 XRM 工具常用登录控件的WPF应用程序,适用CRM9.X版本
VS2017中创建使用 XRM 工具常用登录控件的WPF应用程序,适用CRM9.X版本 前提条件 创建WPF项目和修改版本 添加按钮调用登录控件进行调试 总结 微软dynamics官网之前有个CRM ...
- vue树形结构html,怎么在vue中利用递归组件实现一个树形控件
怎么在vue中利用递归组件实现一个树形控件 发布时间:2021-06-11 17:26:48 来源:亿速云 阅读:81 作者:Leah 本篇文章为大家展示了怎么在vue中利用递归组件实现一个树形控件, ...
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...
- ASP.NET中共有哪几种类型的控件?其中,HTML控件、HTML服务器控件和WEB服务器控件之间有什么区别
[考点] ASP.NET中的控件类型理解. [出现频率] ★★★☆☆ [解答] ASP.NET的控件包括WEB服务器控件.WEB用户控件.WEB自定义控件.HTML服务器控件和HTML控件.HTML控 ...
- C++ 主程序中打开子程序窗口隐藏,操作控件 遍历窗口控件句柄EnumChildWindows();获取其他程序进度条数据 操作选择框 按钮点击
1.线程启动子程序 ShowWindow(hWnd, SW_SHOW)显示 ShowWindow(hwnd, SW_HIDE);隐藏 system_hide(L"Odin3.exe&quo ...
- 项目中比较好用的web打印控件——Lodop
项目中比较好用的web打印控件--Lodop 由于公司业务需求,需要控件打印控件,在网上看到了很多打印插件,觉得Lodop打印插件比较好用,下面介绍操作方法: 1.准备工作,下载控件及官方demo 下 ...
- VB6.0中如何设定COM组件和ActiveX控件的CLSID值
http://ajxfxb.blog.163.com/blog/static/56675086200911181118562/ VB中如何设定COM组件和ActiveX控件的CLSID值 2009-1 ...
最新文章
- 关于MVP模式的一些讨论文章
- 一般物流网站建设有哪些必备版块?
- 矩阵每一行重复_【剑指offer】65 矩阵中的路径
- linux g++ 链接,Linux G++将64位共享库代码链接到静态库
- kubenerte启动_老司机和你深聊Kubenertes 资源分配之 Request 和 Limit 解析
- 标准商业计划书大纲模版
- 随机过程(1)——绪论
- 数据:FIT2CLOUD飞致云连续进入云管理软件市场权威研究报告
- 微商怎么引流被加精准粉?微商有效引流被加方法
- 中国邮递员问题+代码实现(cpp)
- Python文本加密 或 文本文件加密
- python训练营风变编程_扒一扒风变编程的社群运营手段,满满都是套路
- php课程设计感想,设计心得体会
- Spring源码解析二之创建Bean(实例化)
- 华为研究院高级研究员几年心得终得趣谈网络协议文档,附资深架构师讲解
- 数据安全需要做什么?
- 建筑央企工程项目数字化管理整体解决方案
- Ubuntu server关机命令
- 汇编:寄存器的寻址方式
- 周志明架构课--01.原始分布式时代