[UiAutomator] the difference between using the instance and index methods in UiSelector to locate controls.

Source: Internet
Author: User

[UiAutomator] the difference between using the instance and index methods in UiSelector to locate controls.

When you use UiAutomator to write test cases, the most common operation is the control query operation.

In UiSelector, there are two methods for locating controls: instance and index. So what are the differences between the two methods?

First, let's take a look at the official api description:

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 cocould specify a search criteria where the instance is 2, the className (String) matches the image widget class, and enabled (boolean) is true. the code wocould 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 shoshould only be used as a last resort for matching. instead, consider using the instance (int) method.

That is to say, the instance method will retrieve all the controls of the same type on the interface in order and put them in a set (for the time being, I don't know where to put them, thanks ), then, the desired control is obtained according to the control badge in the set, and the index is used to obtain the corresponding control through the node badge at the control level.

How are these two methods used? See the following example:

First, we define a layout through 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" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="textview1"        android:textSize="22sp" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button1" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="textview2"        android:textSize="22sp" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button2" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="textview3"        android:textSize="22sp" /></LinearLayout>

The figure captured by using UiAutomatorViewer is as follows:

UiObject obj = new UiObject (new UiSelector (). className ("android. widget. textView "). index (0); // textview1UiObject obj = new UiObject (new UiSelector (). className ("android. widget. textView "). index (2); // textview2UiObject obj = new UiObject (new UiSelector (). className ("android. widget. textView "). index (4); // textview3

Hey, wait, Nima! Why is the UiObject obtained in the first method "TestUI"? Originally, there was a TextView control on our TitleBar, and its node badge was also 0 (SEE ). Is that too bad? Don't complain first. The api documentation clearly states that this is an unreliable method. It is recommended to try this method only when other methods are difficult.

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)); // textview1UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(2));  // textview2UiObject obj = new UiObject(new UiSelector().className("android.widget.TextView").instance(3));  // textview3

How about using the instance method is much more reliable.

This sharing ends. You are welcome to join me.

======================================== 2014-12-25 ==========

After reading a blog today, I found that the index method is also used in the UiObject. getChild () method. Take the preceding UI as an example.

If we want to obtain the TextView control corresponding to textview1, first find its parent control LinearLayout, and LinearLayout is a child control of FrameLayout (for example ).

Therefore, the code for retrieving textview1 is like this:

UiObject viewObj = new UiObject (new UiSelector (). className ("android. view. view "); // obtain the View control UiObject flObj = viewObj. getChild (new UiSelector (). index (1); // obtain the FrameLayout control UiObject lobj = flObj. getChild (new UiSelector (). index (0); // obtain the LinearLayout control UiObject tv1Obj = lobj. getChild (new UiSelector (). index (0); // obtain the TextView control corresponding to textview1

How is it? It's better to use instance!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.