6. 通过contentDescription定位
在UiAutomator框架和使用了Uiautomator框架的Appium中,控件的属性contentDescription一直是强调开发人员需要添加进去的,因为
有些控件使用其他办法很难或者根本没有办法定位
最重要的是给每个控件的contentDescription设计个唯一值让我们可以非常快速的定位控件,让我们足够敏捷!
以下的实例并没有真正跑过的,因为Notepad应用上面的控件是没有contentDescription这个属性的,但是如果我们假设Add note这个控件的contentDescription是“AddNoteMenuDesc”的话,代码的相应写法应该就如下了。
6.1 UiSelector.description方法
addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc)); assertEquals(addNote.getText(),"Add note"); |
6.2 UiSelector.descriptionContains方法
<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.*$")); //assertEquals(addNote.getText(),"Add note"); |
7.通过其他方法定位
除了以上比较常用的方法外,UIAutomator还支持其他一些方法,比如根据控件属性是否可点击可聚焦可长按等来缩小要定位的控件的范围,具体使用方法不一一列举,可以查看以下测试验证代码。
package majcit.com.UIAutomatorDemo; 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 extends UiAutomatorTestCase { public void testDemo() 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"); } } |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。