日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[UiAutomator] UiSelector中使用instance与index方法定位控件的区别

發(fā)布時(shí)間:2025/3/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [UiAutomator] UiSelector中使用instance与index方法定位控件的区别 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在使用UiAutomator寫測(cè)試用例時(shí),最常用到的就是控件查找操作。

在UiSelector中,有兩個(gè)定位控件的方法,一個(gè)是instance,一個(gè)是index。那么這兩個(gè)方法有什么區(qū)別呢?

首先,我們看一下官方api說(shuō)明:

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.

也就是說(shuō)instance方法會(huì)將界面上所有相同類型的控件按順序取出來(lái),放到一個(gè)集合里(暫且這么理解吧,不知道放哪里了,囧),然后按照控件在集合的角標(biāo)把想要的控件取出來(lái);而index則是通過(guò)該控件所在層級(jí)的節(jié)點(diǎn)角標(biāo)將對(duì)應(yīng)的控件取出來(lái)。

那么這兩個(gè)方法到底是怎么使用的呢?看下面的例子:

首先我們通過(guò)xml定義一個(gè)布局:

<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截出來(lái)的圖是這樣的:

按照說(shuō)明,使用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

哎,等等,尼瑪!為啥我第一個(gè)方法取到的UiObject是“TestUI”?原來(lái)在我們的TitleBar上也有一個(gè)TextView控件,而它的節(jié)點(diǎn)角標(biāo)也是0(見(jiàn)下圖)。這是不是太坑爹了?先不要埋怨,人家api文檔都說(shuō)的很清楚了,這是一個(gè)不靠譜(unreliable)的方法,其他方法都不好使了才建議去嘗試此方法。

下面把使用instance方法獲取TextView控件的方法寫出來(lái):

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方法就靠譜多了吧。

本次分享到此結(jié)束,歡迎大家與我一起交流。

============================2014-12-25 分割線===================

今天上網(wǎng)看博客,發(fā)現(xiàn)index方法還有一種用法,就是在UiObject.getChild()方法里使用。還是以上面的UI為例。

如果我們想要獲取textview1對(duì)應(yīng)的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對(duì)應(yīng)的TextView控件

?

怎么樣?還是instance好用吧!

?

轉(zhuǎn)載于:https://www.cnblogs.com/lovexiaov/p/uiauto_lovexiaov.html

總結(jié)

以上是生活随笔為你收集整理的[UiAutomator] UiSelector中使用instance与index方法定位控件的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。