在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位Android界面上的控件,当时只是一笔带过举了个例子。如该文给自己的承诺,今天特撰写此文以描述UIAutomator各种控件定位的方法,以作为前文的姊妹篇互通有无。
为了和前文达成一致,这次的实践对象同样也是使用SDK自带的NotePad应用,同样是尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单选项。以下是UIAutomatorViewer对界面的一个截图.
但有一个例外的地方是下文的”通过伪xpath方法定位控件“章节实例需要使用到的是NoteEditor这个activity里面的Menu options,因为需要演示通过子控件获得父控件然后得到兄弟控件的功能,UIAutomatorViewer截图如下。
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().textMatches("^Add")); assertEquals(addNote.getText(),"Add note");
通过这种方法定位控件存在的一个问题是很容易发生重复,所以一般都是先用这种方法去narrow down目标控件,然后再去添加其他如text判断等条件进行控件定位。
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");
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");
UiObject parentView = new UiObject(new UiSelector().className("android.view.View")); save = parentView.getChild(new UiSelector().text("Save")); assertEquals(save.getText(),"Save");
save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save"))); assertEquals(save.getText(),"Save");
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");
addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc)); assertEquals(addNote.getText(),"Add note");
</pre><h3>6.2 UiSelector.descriptionContains方法</h3></div><div><pre name="code" class="java"> 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");
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"); } }
作者 | 自主博客 | 微信 | CSDN |
天地会珠海分舵 | http://techgogogo.com | 服务号:TechGoGoGo 扫描码: | 向AI问一下细节 免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。 猜你喜欢最新资讯相关推荐
开发者交流群:相关标签AI
助 手 |
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>