I have previously written 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 keep this series consistent, we continue to use the SDK's own Notepad instance application as our test target application, but this time it's not just about positioning the "ADD Note" Control around menu option as before, Instead, it is designed to locate multiple different controls on the Notepad. But mainly around the notepad of the noteslist this launchable activity, the following first look at the uiautomatorviewer captured below we will be involved in the different interface bar.
- Noteslist Activity's first page list contains note3 to note9 notes
- Notelist activity will also see a note with note1 to Note2 and two duplicate names for note after pulling down
- Click on the System menu will appear "Add Note" This menu entry for our test verification
2. The text of the control to locate robotium many of the methods are supported by the direct input text as a parameter to operate, such as Clickontext,clicklongontext, through the text to find the control is the most rapid and obvious. Here we use Clickontext and its overloaded methods to illustrate, the other way to find the text is similar (we can of course also 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 Example
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 example has two points that need to be noted:
- Robotium will scroll down automatically until you navigate to the target control: This is why the above example does not need to perform a scrolldown action to directly find the "note" on the second page.
- Robotium if more than one control is found that satisfies the condition, the first one is returned by default
- Robotium when the control is positioned according to text, the default is to support the use of regular expressions
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? Because note1 to note9 and note contain the text "note", the arguments in the above code are in the regular expression meaning "
find the text that contains the note string for 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 repetitive controls like this? Please look down. 2.2 Solo.clickontext (String text, int match) 2.2.1 Example
Solo.clickontext ("^note$", 2); Asserttrue (Solo.searchtext ("^note$"));
2.2.2 Parsing if there are more than one control returned through text, 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 Example
Solo.clickontext ("^note2$", 1, false); Asserttrue (Solo.searchtext ("^note2$"));
2.3.2 parsing the last parameter is specifying whether to automatically scrolldown to find the control.
3. ListView child Control Positioning if the control you want to locate is inside a ListView, in addition to using the text method above, we can also locate a few rows of the ListView by specifying the control. 3.1solo.clickinlist (int line):Specify the number of rows to locate3.1.1 Example
Solo.clickinlist (2); Asserttrue (Solo.searchtext ("^note8$"));
3.1.2 Parse click on line 2nd of the first ListView. So what if there's 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 to locate the 3.2.1 example
Solo.clickinlist (2, 0); Asserttrue (Solo.searchtext ("^note8$"));
3.2.2 parsing where the first parameter 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 where this listview is located。
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 of the key actions of the activity (such as "search", "create", "share", etc.), as a flexible use of the menu, as well as implementing a tabwidget-like label function and a drop-down navigation function. The system can adapt to the appearance of Actionbar according to different screen configuration. Since the Actionbar function is not implemented in Notepad, this section uses the notes from Xiaomi to describe them.
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
4.2 Solo.clickonactionbaritem (int id) parameter integer ID refers to R.id, which is the ID of the R.java in the project engineering.
5. The control is positioned in the order of the above 3.2.2 We have already experienced how to locate the control by specifying the ListView. In fact, many controls can be positioned by specifying the order in which the controls are arranged in the interface, and with the click-related method as an example, we can find the following methods for locating different controls in the order they are arranged
- 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 positioning method as Monkeyrunner through the coordinate point to locate the same limitations, a big one is that when the interface control order has been adjusted to immediately test code maintenance updates. 5.1 Example
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 to get the control in the way and operation the control is implemented in a single method, but 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 use of numerous control intrinsic properties to determine whether a target control is targeted, Robotium only supports the positioning of controls through two control internal properties:
- Resrouceid: Can be a string type (obtained through uiautomatorviewer) or an integer (obtained through a R.java file)
- ClassName: The class of the control (which can be obtained by uiautomatorviewer), but note 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 tired of.
6.1 Solo.getview (string/int id,[int index]) 6.1.1 Example
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 should pay attention to Android activity the following control ResourceID is allowed to repeat, such as Notepad above the ListView in 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 Example
View view = Null;view = Solo.getview (textview.class,1); Solo.clickonview (view);
Click on the 1th TextView type control starting 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 for FQCN (i.e. 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 the two system controls we commonly use do not need to be located, one is the system's menu key, and the other is the GoBack of the system. But note that the menu below the menu Entry still needs to be located, such as the "ADD note" In our example, the menu Entry.
8 Do you have anything else? The above list of robotium in my current most important way to get control, of course, there are some other methods in the solo, but it is not easy to understand is not used now, so it is not stated
- Manipulating controls through coordinate points: easy to understand is to get the coordinate point and click on the screen coordinates.
- Get and manipulate the WebView control: It's not going to work now.
- Is there anything else?
Robotium Android control positioning practices and recommendations (Appium/uiautomator sister)