Robotium Android control positioning practices and recommendations (Appium/uiautomator sister article)

Source: Internet
Author: User
Tags appium

I previously wrote a description of how the Appium and Uiautomator frameworks locate controls on the Android interface.

    • Uiautomator ways to locate Android controls practice and advice
    • Appium practice and suggestion on the control location method of various findelement based on Android
Today we change a source more long, today's more popular framework robotium, in practice, see how it is positioning the control.
1. Background to maintain consistency in this series, we continue to use the SDK's own Notepad instance application as our test target application, but this time it's not as if the "Add note" control, which is mostly wrapped around menu option, is positioned. Instead, it is designed to locate multiple different controls on the Notepad. But the main surrounding is still Notepad noteslist this launchable activity, the following first look at the following capture of Uiautomatorviewer we will be involved in the different interface bar.
    • note3 to note9 notes are included in the first page list of the noteslist activity

    • Notelist activity down will also see a note1 to Note2 and Two repeated name note notes

    • Click on the System menu will appear "Add Note" This menu entry for our test certificate


2. The text of the control to locate Robotium in a very many methods are supported directly input text as a parameter to operate, such as Clickontext,clicklongontext, the text to find the control is the quickest and most obvious. Here we use the Clickontext and its overloaded methods to illustrate, the other way to find the text is similar (we can of course also directly call Solo.clickonbutton (String text) To limit the use of the text method to find the button control only). 2.1solo.clickontext (String Text) 2.1.1 Demo sample
Solo.clickontext ("^note$"); Asserttrue (Solo.searchtext ("^note$"));

Find the first textabsolutely equalsNote and click on the control. 2.1.2 Analysis and recommendations the above demo sample has two points that need to be noted to attract attention:
    • Robotium will take the initiative to scroll down until it navigates to the target control: This is why the demo sample does not have to perform the Scrolldown action first to directly find the "note" on the second page.
    • Robotium if more than one control is found that satisfies the condition, the default is to return the first
    • Robotium when the control is positioned according to text, the default is to support the normal form
For example, changing the above example to the following will assert the failure:
Solo.clickontext ("note"); Asserttrue (Solo.searchtext ("^note$"));
Click here will list the top of the diary, in our example is "Note9". Why is it? Since note1 to NOTE9 and note all include the text "note", the references in the above code mean " find text that includes a note string in the control to click”。 So we have to change it to "^note$" to find exactly the text of the control that is absolutely equal to the note. So what if there are repeated controls? Please look down. 2.2 Solo.clickontext (String text, int match) 2.2.1 Demo sample
Solo.clickontext ("^note$", 2); Asserttrue (Solo.searchtext ("^note$"));
2.2.2 Parsing assumes that there are more than one control in text lookup back, then we canThe number of controls required is specified on a 1 -based basis from left to right from the top down order。
2.3 Solo.clickontext (String text, int match, Boolean scroll) 2.3.1 Demo sample
Solo.clickontext ("^note2$", 1, false); Asserttrue (Solo.searchtext ("^note2$"));
2.3.2 parsing the last parameter specifies whether you want to Scrolldown to find the control yourself.
3. ListView child Control positioning It is assumed that the control being positioned is inside a ListView, and in addition to being able to navigate using the text method above, we are able to navigate through a specified control in a few rows of the ListView. 3.1solo.clickinlist (int line):Specify the number of rows to locate3.1.1 Demo sample
Solo.clickinlist (2); Asserttrue (Solo.searchtext ("^note8$"));
3.1.2 Parse click on line 2nd of the first ListView. So what if there are more than one ListView on the interface? Then look at the following method.
3.2 solo.clickinlist (int line, int index): Specify the first few rows of the ListView at the same time to locate the 3.2.1 Demo sample
Solo.clickinlist (2, 0); Asserttrue (Solo.searchtext ("^note8$"));
The first of the 3.2.2 parsing is the number of rows,The second parameter refers to the first few ListView, according to my experience, is based on 1, the interface from left to right from top to bottom in the order of this ListView location。

4. Actionbar controls to locate the new object introduced after Android 3.0, Actionbar can be said to be a quick and easy navigation artifact. It can be used as the title of the activity, highlighting some key actions of the activity (such as "search", "create", "sharing", etc.), as the flexible use of the menu, also can implement similar Tabwidget label function as well as the function of the drop-down navigation, The system can be very good according to different screen configuration to adapt to the appearance of Actionbar. Since the Actionbar function is not implemented in the Notepad, we use the note from Xiaomi to describe the narrative in this section.

4.1 Solo.clickonactionbarhomebutton () The purpose of this method is to click the home or up icon in the upper left corner of the Actionbar to navigate to the previous page
The 4.2 solo.clickonactionbaritem (int id) parameter integer ID refers to the ID of r.id, which is the R.java inside the project.

5. The control is positioned in the order of the above 3.2.2 We have experienced how to locate the control by specifying the first few ListView. In fact, very many controls can be positioned by specifying the order in which the controls are arranged in the interface, and in the case of the click-related method, we are able to find the following methods for positioning the different controls in the Order of arrangement
    • Solo.clickonbutton (int index)
    • Solo.clickoncheckbox (int index);
    • Solo.clickonedittext (int index)
    • Solo.clickonimage (int index)
    • Solo.clickonimagebutton (int index)
    • Solo.clickonradiobutton (int index)
This way of positioning as monkeyrunner through the coordinates of the location of the same way there are limitations, very large one is when the interface control order is adjusted to immediately test code maintenance updates. 5.1 Demo Examples
Solo.clicklongontextandpress ("Note9", 2); Solo.clickonbutton (0);
5.2 Analysis of the above code is done
    • Long press Note9 that note

    • In the pop-up menu, select the 2nd menu entry from 0, which is "Edit Title"
    • Click on the No. 0 button from left to right from top to bottom from 0, which is "OK" button


6. Navigate through the internal properties of the control the way and operation of getting the control is implemented in a single method, in fact Robotium also supports getting the control first and then slowly working on the control. Robotium's solo class has some of the overloaded methods of GetView that are dedicated to this thing. ButUnlike Uiautoamtor and Appium, which support the inference of a target control with a multitude of control intrinsic properties, Robotium only supports locating controls through two control internal properties:
    • Resrouceid: Can be string type (obtained through Uiautomatorviewer) can also be integer (obtained via R.java file)
    • ClassName: The class of the control (which can be obtained through uiautomatorviewer), just notice that it is not a string but a real class

Here we use the GetView as an example to illustrate how to get the control through the control's internal properties to make a point about it. Of course, in addition to Getview,robotium also support other methods such as getviews,getcurrentviews, but the principle is consistent, not exhausted.

6.1 Solo.getview (string/int id,[int index]) 6.1.1 Demo sample
View view = Null;view = Solo.getview ("Android:id/text1", 1); Solo.clickonview (view);
6.1.2 Analysis of what this code does is to get the 1th ResourceID from 0 to "Android:id/text1".
Why do I need to fill out index here? In fact, we have to pay attention to the Android activity the following control ResourceID is agreed repeatedly, for example Notepad above the ListView inside the ResourceID of each note is actually " Android:id/text1 ". So in this case we have to add index to distinguish between what we need is the first few note. For example, the following code will be the first note inside the ListView
View view = Null;view = Solo.getview ("Android:id/text1"); Solo.clickonview (view);
6.2 Solo.getview (class<t> viewclass, int index) 6.2.1 Demo sample
View view = Null;view = Solo.getview (textview.class,1); Solo.clickonview (view);
Click the 1th textview type control from 0, which is the note4 in. According to the order from left to right, <span style= "font-family:arial, Helvetica, Sans-serif;" > The No. 0 One here is the textview:</span> of the ListView title called Notes.
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
6.2.2 Parsing note the class names here Viewclass and Uiautomator ( New Uiselector (). ClassName (String className)) and Appium ( appiumdriver.findelementbyclassname (String className)The format and type of the class to be filled out by classname is different, take TextView as an example:
    • Robotium :class Type | | No need to FQCN (that is, no need to write andoid.widget.Textview, which is necessary in Uiautomator and Appium)
    • Uiautomator: String Type | | Fqcn
    • Appium :String Type | | Fqcn
7 controls without positioning we often use two system controls that do not need to be located, one is the system's menu key, and the other is the GoBack of the system. However, note that the menu below the Entry or need to be located, for example, our sample "Add Note" this menu Entry.
8 Do you have anything else? The above list of robotium in the most important to my current method of acquiring controls, of course, solo inside some other methods, but not easy to understand that is not used today, so do not state
    • Manipulating controls through coordinate points: easy to understand is to get the coordinate point and then click on the screen coordinates.
    • Get and manipulate the WebView control: It's not available now, and when it's practical, it's time to dig deeper.
    • Is there anything else?

Item

Description

Warning

Author

Heaven Zhuhai Branch Rudder

Reprint please specify the source!

Many other wonderful articles please check my blog!

Blog Address

Http://blog.csdn.net/zhubaitian



Robotium Android control positioning practices and recommendations (Appium/uiautomator sister 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.