UISelector

1, the basic method UiSelector

UiSelector objects can be understood as a condition object, it describes a condition with UiObject can use to get a qualified control object.

All methods are public, and returns the object UiSelector class.

Text method aspects:

  1.text (String text) text

  2.textContains (String text) text that contains

  3.textMatches (String regex) regular text

  4.textStartsWith (String text) text characters start

Method described aspects:

  1.description (String desc) Description

  2.descriptionContains (String desc) description contains

  3.descriptionMatches (String regex) describes a regular

  4.descriptionStartsWith (String desc) described start character

Name of the class method aspects:

  1.childSelector(UiSelector selector) 子类

  2.className(String className) 类名

 Simply, examples of aspects of a method:

  1.index (int index) number

  2.instance (int instantce) Index

 The unique attributes:

  1.checked (boolean val) Select Properties

  2.chickable (boolean val) click on Properties

  3.enabled(boolean val) enabled属性

  4.focusable (boolean val) focal properties

  5.longClickable (boolean val) press properties

  6.scrollable (boolean val) scroll property

  7.selected (boolean val) Select Properties

 Method names aspects of the package:

  1.packageName(String name) 包名

  2.packageNameMatches(String regex) 包名正则

 ID of resources methods:

  1.resourceId (String id) resource ID

  2.resourceIdMatches (String regex) regular resource ID

 2. Positioning via text message

By positioning the text property of the control of the control should be the most common method, after all, limited screen size of mobile applications, there is the possibility of repeating text is not large, even if there really is repeated, you can add other targeting methods abbreviation error.

2.1 UISelector.text method
addNote the UIObject = new new (new new UiSelector () text ( "the Add note").);
AssertEquals (addNote.getText (), "the Add note");
the method by directly find all the controls on the current interface Compare the text property of each control, such as whether the expected value to position control, very good understanding, so there is no need to elaborate.
2.2 UISelector.textContains method.
AddNote UIObject new new = (new new UiSelector () textContains ( "the Add").);
The assertEquals (addNote.getText (), "the Add Note");
This method is similar with the above method, but does not require input control all text information.
2.3 UISelector.textStartsWith method
addNote UIObject new new = (new new UiSelector () textStartsWith ( "the Add").);
The assertEquals (addNote.getText (), "the Add Note");
the name implies, is determined by the start of a text and control whether the expected string match to get control, in fact, personally I feel that there is the necessity of this method is not as strong as it can function using the above method or methods to replace the following regular expression. And now that you provide textStartsWith method, why do not you provide a textEndWith way to do that!
2.4 UISelector.textMatches method
= new new UIObject addNote (. UiSelector new new () textMatches ( "^ the Add *."));
the assertEquals (addNote.getText (), "the Add Note");
This is the regular expression way to compare the text to the control positioning control, here it is interesting that the regular expression is restricted users, see the method's official description: "Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app). The text for the widget must match exactly with the string in your input argument ". We do not ignore the first sentence, the key is the second sentence, translates to "target control text (all content) must be positive and we enter the expression exact match." What does that mean? Meaning that you do not like usual regular expression as part of the agreement obtained by the control comparison of text. The following code example:
addNote UIObject new new = (new new UiSelector () textMatches ( "^ the Add").);
The assertEquals (addNote.getText (), "the Add Note");
Normally this regular expression is no problem it means that you want to "get to the beginning of the text controls Add, as word Add serial face value what is not necessary to go to hell." But according to the official description above us, like this is not enough, you have to take a regular expression so that the full complement regular expressions and text and controls to match exactly, as to what you use wildcard characters or strings completely in accordance with the regular expression style grammar.
Note that all restrictions on the use of regular expressions in UISlector related methods are effective oh.

ClassName 3 by positioning control
by the positioning of a problem with this method is easy to control the presence of duplication, so in this way are generally the first to narrow down the target control, and then to add other conditions such as text judgment positioning control .

3.1 UISelector.className method
addNote UIObject new new = (new new UiSelector () className ( "android.widget.TextView") text ( "Note the Add")..);
The assertEquals (addNote.getText (), "the Add Note");
examples first find all the TextView control by ClassName, then look at these TextView control text is "Add note" controls.
3.2 UISelector.classNameMatches method
addNote UIObject new new = (new new UiSelector () classNameMatches ( "the TextView $ *.").);
The assertEquals (addNote.getText (), "the Add Note");
regular expressions and determines whether the expected className consistent, consistent attention to regular expressions and limitations described in section 2.4.
4. By positioning pseudo-xpath method
provides UISelector class to some lengths to target people based on the control hierarchy in the XML layout of the interface, but has no real UIAutomator provide similar Appium of findElementWithXpath related methods, so I will call it here pseudo-xpath method.

This section is no longer NotesList to use the activity inside the menu options, but NoteEditor this activity inside the Menu options, which have more than one Menu entry.

4.1 by UiSelector.fromParent or UiObject.getFromParent method
this way I find most useful is the test case code currently in operation is one of a control group of the same level of control, which in turn requires the same level of other operating controls when. The following example is to find the same level as their brothers delete control through the parent control save control. Here are a list of instances by UiObject.getFromParent method and UiSelector.fromParent methods, their function is in fact the same.

UiObject.getFromPatrent方法:

= new new UIObject Save (new new UiSelector () text ( "the Save").);
the assertEquals (save.getText (), "the Save");
Delete save.getFromParent = (. UiSelector new new () text ( "the Delete"));
assertEquals (delete.getText (), "the Delete");
UiSelector.fromParent method (in this example a little detour clumsy, but in order to demonstrate the functionality will be on the Look):
. = the Delete the UIObject new new (new new UiSelector () text ( "the Save ") .fromParent (new new UiSelector () text. (" the Delete ")));
the assertEquals (delete.getText ()," the Delete ");
4.2 UiObject.getChild methods or by UiSelector.childSelector
this method is known in the parent when controls how quickly find the parent control following child controls.

UiObject.getChild method:

= New new UIObject parentView UIObject (new new UiSelector () className ( "android.view.View").);
Save = parentView.getChild (new new UiSelector () text ( "the Save").);
The assertEquals (save.getText (), "the Save");
UiSelector.childSelector method:
.. = Save new new UIObject (new new UiSelector () className ( "android.view.View") childSelector (. UiSelector new new () text ( "the Save")));
the assertEquals (Save .getText (), "the Save");
5. by positioning control ID
at and above versions of Android API Level18 increased property ResourceId an Android controls, so be careful before you use this method to ensure that your target test equipment and all your UIAutomoator API library jar package version used above Level 18. For example I use is the local version sdk repository 19: D: \ Develops \ AndroidSDK \ platforms \ android-19 \ uiautomator.jar

5.1 UiSelector.resourceId method
addNote = new UiObject (new new UiSelector () The resourceId (. "Android: ID / title"));
the assertEquals (addNote.getText (), "the Add Note");
5.2 UiSelector.resourceIdMatches method
addNote = new UiObject (new new UiSelector () resourceIdMatches ( "+ ID / title.").);
the assertEquals (addNote.getText (), "the Add Note");
Note regular expression is limited and consistent description of section 2.4
6. contentDescription positioned by
the UiAutomator framework and use Uiautomator framework Appium, attributes contentDescription control has been emphasized developer needs to added to it, because

Some controls are difficult to use other means or no way to locate
the most important thing is to contentDescription each control design unique values so that we can very quickly locate the controls, let's agile enough!
The following example does not really ran, because the Notepad application of the above control is not contentDescription this property, but if we assume that contentDescription Add note this control is "AddNoteMenuDesc", then the corresponding code should be written as follows on the.

6.1 UiSelector.description方法
addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));
assertEquals(addNote.getText(),"Add note");
</pre><h2>6.2 UiSelector.descriptionContains方法</h2></div><div><pre name="code" class="java"> addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));
assertEquals(addNote.getText(),"Add note");
6.3 UiSelector.descriptionStartWith方法
addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));
assertEquals(addNote.getText(),"Add note");
6.4 UiSelector.descriptionMatches方法
//addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));7. The positioning by other methods
Note //assertEquals(addNote.getText(),"Add ");

In addition to these commonly used methods, UIAutomator also supports other methods, such as control according to the focusing properties can be clicked may be press like narrow control range to target, not enumerated the specific use, can be viewed to verify the following test code.
majcit.com.UIAutomatorDemo Package;

Import com.android.uiautomator.core.UiDevice;
Import com.android.uiautomator.core.UiObject;
Import com.android.uiautomator.core.UiObjectNotFoundException;
Import com.android.uiautomator.core.UiScrollable ;
Import com.android.uiautomator.core.UiSelector;
Import com.android.uiautomator.testrunner.UiAutomatorTestCase;

Import static org.hamcrest.Matchers *;.
Import static org.hamcrest.MatcherAssert.assertThat;

public class UISelectorFindElementTest the extends UiAutomatorTestCase {

public testDemo void () throws UiObjectNotFoundException {
UIDevice Device getUiDevice = ();
device.pressHome();
// Start Notepad
UiObject appNotes = new UiObject(new UiSelector().text("Notes"));
appNotes.click();
//Sleep 3 seconds till the app get ready
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

//Evoke the system menu option
device.pressMenu();
UiObject addNote = new UiObject(new UiSelector().text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject (new UiSelector().checked(false).clickable(true));
assertEquals(addNote.getText(),"Add note");


addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$"));
assertEquals(addNote.getText(),"Add note");

//addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));
//assertEquals(addNote.getText(),"Add note");

//addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));
//assertEquals(addNote.getText(),"Add note");

//addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));
//assertEquals(addNote.getText(),"Add note");

//addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));
//assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().focusable(true).text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().focused(false).text("Add note"));
assertEquals(addNote.getText(),"Add note");


//TBD
//addNote = new UiObject(new UiSelector().fromParent(selector))

addNote = new UiObject(new UiSelector().index(0).text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().className("android.widget.TextView").enabled(true).instance(0));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().longClickable(false).text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().text("Add note"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().textContains("Add"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().textStartsWith("Add"));
assertEquals(addNote.getText(),"Add note");


addNote = new UiObject(new UiSelector().textMatches("Add.*"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().resourceId("android:id/title"));
assertEquals(addNote.getText(),"Add note");

addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title"));
assertEquals(addNote.getText(),"Add note");

//Go to the editor activity, need to cancel menu options first
device.pressMenu();
//Find out the new added note entry
UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));
//UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));
UiObject note = null;
if(noteList.exists()) {
note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true);
//note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true);
}
else {
note = new UiObject(new UiSelector().text("Note1"));
}
assertNotNull(note);

//Go to the NoteEditor activity
note.click();
device.pressMenu();

UiObject save = null;
UiObject delete = null;

save = new UiObject(new UiSelector().text("Save"));
assertEquals(save.getText(),"Save");
delete = save.getFromParent(new UiSelector().text("Delete"));
assertEquals(delete.getText(),"Delete");

delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));
assertEquals(delete.getText(),"Delete");

save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));
assertEquals(save.getText(),"Save");

UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));
save = parentView.getChild(new UiSelector().text("Save"));
assertEquals(save.getText(),"Save");


}
}

Guess you like

Origin www.cnblogs.com/ndd2016/p/11999975.html