<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                在本人之前的一篇文章<<[Appium基于安卓的各種FindElement的控件定位方法實踐和建議](http://blog.csdn.net/zhubaitian/article/details/39754041)>>第二章節談到Appium可以通過使用UIAutomator的方法去定位Android界面上的控件,當時只是一筆帶過舉了個例子。如該文給自己的承諾,今天特撰寫此文以描述UIAutomator各種控件定位的方法,以作為前文的姊妹篇互通有無。 ## 1. 背景 為了和前文達成一致,這次的實踐對象同樣也是使用SDK自帶的NotePad應用,同樣是嘗試去獲得在NotesList那個Activity里的Menu Options上面的那個Add note菜單選項。以下是UIAutomatorViewer對界面的一個截圖. ![](https://box.kancloud.cn/2016-08-12_57ad6e2928ee1.jpg) 但有一個例外的地方是下文的”*通過偽xpath方法定位控件*“章節實例需要使用到的是NoteEditor這個activity里面的Menu options,因為需要演示通過子控件獲得父控件然后得到兄弟控件的功能,UIAutomatorViewer截圖如下。 ![](https://box.kancloud.cn/2016-08-12_57ad6e294d1d7.jpg) ## 2. 通過文本信息定位 通過控件的text屬性定位控件應該是最常用的一種方法了,畢竟移動應用的屏幕大小有限,存在text重復的可能性并不大,就算真的有重復,可以添加其他定位方法來縮寫誤差。 ### 2.1 UISelector.text方法 ~~~ addNote = new UiObject(new UiSelector().text("Add note")); assertEquals(addNote.getText(),"Add note"); ~~~ 該方法通過直接查找當前界面上所有的控件來比較每個控件的text屬性是否如預期值來定位控件,挺好理解的,所以就沒有必要細說了。 ### 2.2. UISelector.textContains方法 ~~~ addNote = new UiObject(new UiSelector().textContains("Add")); assertEquals(addNote.getText(),"Add note"); ~~~ 此方法跟以上方法類似,但是不需要輸入控件的全部text信息。 ### 2.3 UISelector.textStartsWith方法 ~~~ addNote = new UiObject(new UiSelector().textStartsWith("Add")); assertEquals(addNote.getText(),"Add note"); ~~~ 顧名思義,通過判斷一個控件的text的開始是否和預期的字串相吻合來獲得控件,其實個人覺得這個方法存在的必要性不強,因為它的功能完全可以用上面的方法或者下面的正則表達式的方法取代。況且既然你提供了textStartsWith方法,為什么你不提供個textEndWith的方法呢! ### 2.4 UISelector.textMatches方法 ~~~ addNote = new UiObject(new UiSelector().textMatches("^Add.*")); assertEquals(addNote.getText(),"Add note"); ~~~ 這個方法是通過正則表達式的方式來比較控件的text來定位控件,這里有意思的是用戶使用的正則表達式是有限制的,請看該方法的官方描述:”*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*“。第一句我們不用管它,關鍵是第二句,翻譯過來就是”目標控件的text(的所有內容)必須和我們輸入的正則表達式完全匹配“。什么意思呢?意思就是你不能像往常的正則表達式那樣通過比較text的部分吻合來獲得控件。以下面代碼為例子: ~~~ addNote = new UiObject(new UiSelector().textMatches("^Add")); assertEquals(addNote.getText(),"Add note"); ~~~ 正常來說這個正則表達式是沒有問題的,它的意思就是想要“獲取以Add開頭的text的控件,至于Add字串口面是什么值,沒有必要去管它”。但是按照我們上面的官方描述,這樣子是不行的,你必須要把正則表達式補充完整以使得正而表達式和控件的text完全進行匹配,至于你用什么通配符或者字串就完全按照正則表達式的語法了。 注意這個限制在UISlector使用所有的正則表達式相關的方法中都有效哦。 ## 3 通過控件的ClassName定位 通過這種方法定位控件存在的一個問題是很容易發生重復,所以一般都是先用這種方法去narrow down目標控件,然后再去添加其他如text判斷等條件進行控件定位。 ### 3.1 UISelector.className方法 ~~~ addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note")); assertEquals(addNote.getText(),"Add note"); ~~~ 實例中首先通過ClassName找到所有的TextView控件,然后再在這些TextView控件查找text是”Add note“的控件。 ### 3.2 UISelector.classNameMatches方法 ~~~ addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$")); assertEquals(addNote.getText(),"Add note"); ~~~ 通過正則表達式判斷className是否和預期的一致,注意正則表達式的限制和章節2.4描述的一致。 ## 4. 通過偽xpath方法定位 UISelector類提供了一些方法根據控件在界面的XML布局中的層級關系來進行定位,但是UIAutomator又沒有真正的提供類似Appium的findElementWithXpath相關的方法,所以這里我就稱之為偽xpath方法。 這個章節使用到的不再是NotesList那個activity里面的menu options,而是NoteEditor這個activity里面的Menu options,里面不止有一個Menu entry。 ### 4.1 通過UiSelector.fromParent或UiObject.getFromParent方法 這種方法我覺得最有用的情況是測試代碼當前在操作的是同一層級的一組控件中的某一個控件,轉而又需要操作同一層級的另外一個控件的時候。下面的實例就是通過save控件的父控件找到其同一層級的兄弟控件delete。這里分別列出了通過UiObject.getFromParent方法和UiSelector.fromParent方法的實例,事實上他們的功能是一樣的。 UiObject.getFromPatrent方法: ~~~ save = new UiObject(new UiSelector().text("Save")); assertEquals(save.getText(),"Save"); delete = save.getFromParent(new UiSelector().text("Delete")); assertEquals(delete.getText(),"Delete"); ~~~ UiSelector.fromParent方法(這個例子有點迂回笨拙,但為了演示功能就將就著看吧): ~~~ delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete"))); assertEquals(delete.getText(),"Delete"); ~~~ ### 4.2 通過UiSelector.childSelector或UiObject.getChild方法 這種方法是在已知父控件的時候如何快速的查找該父控件下面的子控件。 UiObject.getChild方法: ~~~ 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"); ~~~ ## 5. 通過控件ID定位 在Android API Level18及其以上的版本增加了一個Android控件的屬性ResourceId,所以要注意在使用這種方法之前先確保你的目標測試設備和你的UIAutomoator庫jar包使用的都是API Level 18以上的版本。例如我自己使用的就是本地sdk中版本19的庫:*D:\Develops\AndroidSDK\platforms\android-19\uiautomator.jar* ### 5.1 UiSelector.resourceId方法 ~~~ addNote = new UiObject(new UiSelector().resourceId("android:id/title")); assertEquals(addNote.getText(),"Add note"); ~~~ ### 5.2 UiSelector.resourceIdMatches方法 ~~~ addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title")); assertEquals(addNote.getText(),"Add note"); ~~~ 注意正則表達式的限制和章節2.4描述的一致 ## 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"); ~~~ ~~~ </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.*$")); //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"); } } ~~~ <table cellspacing="0" cellpadding="0" width="539" class=" " style="margin:0px 0px 10px; padding:0px; border-collapse:collapse; width:668px; max-width:100%; word-wrap:break-word!important"><tbody style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important">?</td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">作者</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">自主博客</span></p></td><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">微信</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">CSDN</span></p></td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(227,228,228)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">天地會珠海分舵</span></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://techgogogo.com/">http://techgogogo.com</a></span><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; font-size:11px; letter-spacing:0px; word-wrap:break-word!important"/></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></p></td><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">服務號</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:TechGoGoGo</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">掃描碼</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><img src="image/47cf4f9ec59b0ef1f807a6c33ab5ce5f.jpg" alt="" style="max-width:100%; margin:0px; padding:0px; height:auto!important; word-wrap:break-word!important; width:auto!important; visibility:visible!important"/></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; color:rgb(62,62,62); font-family:'Helvetica Neue',Helvetica,'Hiragino Sans GB','Microsoft YaHei',?¢èí??oú,Arial,sans-serif; font-size:18px; line-height:28.7999992370605px; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://blog.csdn.net/zhubaitian">http://blog.csdn.net/zhubaitian</a></span><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"/></p><div><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"><br/></span></div></td></tr></tbody></table>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看