您好,登錄后才能下訂單哦!
在本人之前的一篇文章<<Appium基于安卓的各種FindElement的控件定位方法實(shí)踐和建議>>第二章節(jié)談到Appium可以通過使用UIAutomator的方法去定位Android界面上的控件,當(dāng)時(shí)只是一筆帶過舉了個(gè)例子。如該文給自己的承諾,今天特撰寫此文以描述UIAutomator各種控件定位的方法,以作為前文的姊妹篇互通有無。
為了和前文達(dá)成一致,這次的實(shí)踐對(duì)象同樣也是使用SDK自帶的NotePad應(yīng)用,同樣是嘗試去獲得在NotesList那個(gè)Activity里的Menu Options上面的那個(gè)Add note菜單選項(xiàng)。以下是UIAutomatorViewer對(duì)界面的一個(gè)截圖.
但有一個(gè)例外的地方是下文的”通過偽xpath方法定位控件“章節(jié)實(shí)例需要使用到的是NoteEditor這個(gè)activity里面的Menu options,因?yàn)樾枰菔就ㄟ^子控件獲得父控件然后得到兄弟控件的功能,UIAutomatorViewer截圖如下。
addNote = new UiObject(new UiSelector().text("Add note")); assertEquals(addNote.getText(),"Add note");該方法通過直接查找當(dāng)前界面上所有的控件來比較每個(gè)控件的text屬性是否如預(yù)期值來定位控件,挺好理解的,所以就沒有必要細(xì)說了。
addNote = new UiObject(new UiSelector().textContains("Add")); assertEquals(addNote.getText(),"Add note");此方法跟以上方法類似,但是不需要輸入控件的全部text信息。
addNote = new UiObject(new UiSelector().textStartsWith("Add")); assertEquals(addNote.getText(),"Add note");顧名思義,通過判斷一個(gè)控件的text的開始是否和預(yù)期的字串相吻合來獲得控件,其實(shí)個(gè)人覺得這個(gè)方法存在的必要性不強(qiáng),因?yàn)樗墓δ芡耆梢杂蒙厦娴姆椒ɑ蛘呦旅娴恼齽t表達(dá)式的方法取代。況且既然你提供了textStartsWith方法,為什么你不提供個(gè)textEndWith的方法呢!
addNote = new UiObject(new UiSelector().textMatches("^Add.*")); assertEquals(addNote.getText(),"Add note");這個(gè)方法是通過正則表達(dá)式的方式來比較控件的text來定位控件,這里有意思的是用戶使用的正則表達(dá)式是有限制的,請(qǐng)看該方法的官方描述:”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“。第一句我們不用管它,關(guān)鍵是第二句,翻譯過來就是”目標(biāo)控件的text(的所有內(nèi)容)必須和我們輸入的正則表達(dá)式完全匹配“。什么意思呢?意思就是你不能像往常的正則表達(dá)式那樣通過比較text的部分吻合來獲得控件。以下面代碼為例子:
addNote = new UiObject(new UiSelector().textMatches("^Add")); assertEquals(addNote.getText(),"Add note");正常來說這個(gè)正則表達(dá)式是沒有問題的,它的意思就是想要“獲取以Add開頭的text的控件,至于Add字串口面是什么值,沒有必要去管它”。但是按照我們上面的官方描述,這樣子是不行的,你必須要把正則表達(dá)式補(bǔ)充完整以使得正而表達(dá)式和控件的text完全進(jìn)行匹配,至于你用什么通配符或者字串就完全按照正則表達(dá)式的語法了。
通過這種方法定位控件存在的一個(gè)問題是很容易發(fā)生重復(fù),所以一般都是先用這種方法去narrow down目標(biāo)控件,然后再去添加其他如text判斷等條件進(jìn)行控件定位。
addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note")); assertEquals(addNote.getText(),"Add note");實(shí)例中首先通過ClassName找到所有的TextView控件,然后再在這些TextView控件查找text是”Add note“的控件。
addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$")); assertEquals(addNote.getText(),"Add note");通過正則表達(dá)式判斷className是否和預(yù)期的一致,注意正則表達(dá)式的限制和章節(jié)2.4描述的一致。
save = new UiObject(new UiSelector().text("Save")); assertEquals(save.getText(),"Save"); delete = save.getFromParent(new UiSelector().text("Delete")); assertEquals(delete.getText(),"Delete");UiSelector.fromParent方法(這個(gè)例子有點(diǎn)迂回笨拙,但為了演示功能就將就著看吧):
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");UiSelector.childSelector方法:
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");注意正則表達(dá)式的限制和章節(jié)2.4描述的一致
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 |
天地會(huì)珠海分舵 | http://techgogogo.com | 服務(wù)號(hào):TechGoGoGo 掃描碼:
| 向AI問一下細(xì)節(jié) 免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。 猜你喜歡最新資訊相關(guān)推薦
相關(guān)標(biāo)簽AI
助 手 |