zoukankan      html  css  js  c++  java
  • 【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39777951

    在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位Android界面上的控件,当时只是一笔带过举了个例子。如该文给自己的承诺,今天特撰写此文以描述UIAutomator各种控件定位的方法,以作为前文的姊妹篇互通有无。

    1. 背景

    为了和前文达成一致,这次的实践对象同样也是使用SDK自带的NotePad应用,同样是尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单选项。以下是UIAutomatorViewer对界面的一个截图.

    但有一个例外的地方是下文的”通过伪xpath方法定位控件“章节实例需要使用到的是NoteEditor这个activity里面的Menu options,因为需要演示通过子控件获得父控件然后得到兄弟控件的功能,UIAutomatorViewer截图如下。

    2. 通过文本信息定位

    通过控件的text属性定位控件应该是最常用的一种方法了,毕竟移动应用的屏幕大小有限,存在text重复的可能性并不大,就算真的有重复,可以添加其他定位方法来缩写误差。

    2.1 UISelector.text方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().text("Add note"));  
    2. assertEquals(addNote.getText(),"Add note");  
    该方法通过直接查找当前界面上所有的控件来比较每个控件的text属性是否如预期值来定位控件,挺好理解的,所以就没有必要细说了。

    2.2. UISelector.textContains方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().textContains("Add"));  
    2. assertEquals(addNote.getText(),"Add note");  
    此方法跟以上方法类似,但是不需要输入控件的全部text信息。

    2.3 UISelector.textStartsWith方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().textStartsWith("Add"));  
    2. assertEquals(addNote.getText(),"Add note");  
    顾名思义,通过判断一个控件的text的开始是否和预期的字串相吻合来获得控件,其实个人觉得这个方法存在的必要性不强,因为它的功能完全可以用上面的方法或者下面的正则表达式的方法取代。况且既然你提供了textStartsWith方法,为什么你不提供个textEndWith的方法呢!

    2.4 UISelector.textMatches方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().textMatches("^Add.*"));  
    2. 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的部分吻合来获得控件。以下面代码为例子:
    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().textMatches("^Add"));  
    2. assertEquals(addNote.getText(),"Add note");  
    正常来说这个正则表达式是没有问题的,它的意思就是想要“获取以Add开头的text的控件,至于Add字串口面是什么值,没有必要去管它”。但是按照我们上面的官方描述,这样子是不行的,你必须要把正则表达式补充完整以使得正而表达式和控件的text完全进行匹配,至于你用什么通配符或者字串就完全按照正则表达式的语法了。
    注意这个限制在UISlector使用所有的正则表达式相关的方法中都有效哦。

    3 通过控件的ClassName定位

    通过这种方法定位控件存在的一个问题是很容易发生重复,所以一般都是先用这种方法去narrow down目标控件,然后再去添加其他如text判断等条件进行控件定位。

    3.1 UISelector.className方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note"));  
    2. assertEquals(addNote.getText(),"Add note");  
    实例中首先通过ClassName找到所有的TextView控件,然后再在这些TextView控件查找text是”Add note“的控件。

    3.2 UISelector.classNameMatches方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$"));  
    2. 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方法:
    [java] view plaincopy
     
    1. save =  new UiObject(new UiSelector().text("Save"));  
    2. assertEquals(save.getText(),"Save");  
    3. delete = save.getFromParent(new UiSelector().text("Delete"));  
    4. assertEquals(delete.getText(),"Delete");  
    UiSelector.fromParent方法(这个例子有点迂回笨拙,但为了演示功能就将就着看吧):
     
    [java] view plaincopy
     
    1. delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));  
    2. assertEquals(delete.getText(),"Delete");  

    4.2 通过UiSelector.childSelector或UiObject.getChild方法

    这种方法是在已知父控件的时候如何快速的查找该父控件下面的子控件。
    UiObject.getChild方法:
    [java] view plaincopy
     
    1. UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));  
    2. save = parentView.getChild(new UiSelector().text("Save"));  
    3. assertEquals(save.getText(),"Save");  
    UiSelector.childSelector方法:
    [java] view plaincopy
     
    1. save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));  
    2. assertEquals(save.getText(),"Save");  

    5. 通过控件ID定位

    在Android API Level18及其以上的版本增加了一个Android控件的属性ResourceId,所以要注意在使用这种方法之前先确保你的目标测试设备和你的UIAutomoator库jar包使用的都是API Level 18以上的版本。例如我自己使用的就是本地sdk中版本19的库:D:DevelopsAndroidSDKplatformsandroid-19uiautomator.jar

    5.1 UiSelector.resourceId方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().resourceId("android:id/title"));  
    2. assertEquals(addNote.getText(),"Add note");  

    5.2 UiSelector.resourceIdMatches方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title"));  
    2. assertEquals(addNote.getText(),"Add note");  
    注意正则表达式的限制和章节2.4描述的一致

    6. 通过contentDescription定位

    在UiAutomator框架和使用了Uiautomator框架的Appium中,控件的属性contentDescription一直是强调开发人员需要添加进去的,因为
    • 有些控件使用其他办法很难或者根本没有办法定位
    • 最重要的是给每个控件的contentDescription设计个唯一值让我们可以非常快速的定位控件,让我们足够敏捷!
    以下的实例并没有真正跑过的,因为Notepad应用上面的控件是没有contentDescription这个属性的,但是如果我们假设Add note这个控件的contentDescription是“AddNoteMenuDesc”的话,代码的相应写法应该就如下了。

    6.1 UiSelector.description方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));  
    2. assertEquals(addNote.getText(),"Add note");  
    [java] view plaincopy
     
    1. </pre><h2>6.2 UiSelector.descriptionContains方法</h2></div><div><pre name="code" class="java">            addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));  
    2.             assertEquals(addNote.getText(),"Add note");  

    6.3 UiSelector.descriptionStartWith方法

    [java] view plaincopy
     
    1. addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));  
    2. assertEquals(addNote.getText(),"Add note");  

    6.4 UiSelector.descriptionMatches方法

    [java] view plaincopy
     
    1. //addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));  
    2. //assertEquals(addNote.getText(),"Add note");  

    7.通过其他方法定位

    除了以上比较常用的方法外,UIAutomator还支持其他一些方法,比如根据控件属性是否可点击可聚焦可长按等来缩小要定位的控件的范围,具体使用方法不一一列举,可以查看以下测试验证代码。
    [java] view plaincopy
     
    1. package majcit.com.UIAutomatorDemo;  
    2.   
    3. import com.android.uiautomator.core.UiDevice;  
    4. import com.android.uiautomator.core.UiObject;  
    5. import com.android.uiautomator.core.UiObjectNotFoundException;  
    6. import com.android.uiautomator.core.UiScrollable;  
    7. import com.android.uiautomator.core.UiSelector;  
    8. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
    9.   
    10. import static org.hamcrest.Matchers.*;  
    11. import static org.hamcrest.MatcherAssert.assertThat;  
    12.   
    13. public class UISelectorFindElementTest extends UiAutomatorTestCase {  
    14.       
    15.      public void testDemo() throws UiObjectNotFoundException {    
    16.             UiDevice device = getUiDevice();  
    17.             device.pressHome();    
    18.             // Start Notepad  
    19.             UiObject appNotes = new UiObject(new UiSelector().text("Notes"));   
    20.             appNotes.click();    
    21.             //Sleep 3 seconds till the app get ready  
    22.             try {    
    23.                 Thread.sleep(3000);    
    24.             } catch (InterruptedException e1) {    
    25.                 // TODO Auto-generated catch block    
    26.                 e1.printStackTrace();    
    27.             }    
    28.               
    29.             //Evoke the system menu option  
    30.             device.pressMenu();  
    31.             UiObject addNote = new UiObject(new UiSelector().text("Add note"));  
    32.             assertEquals(addNote.getText(),"Add note");  
    33.               
    34.             addNote = new UiObject (new UiSelector().checked(false).clickable(true));  
    35.             assertEquals(addNote.getText(),"Add note");  
    36.               
    37.               
    38.             addNote = new UiObject(new UiSelector().className("android.widget.TextView").text("Add note"));  
    39.             assertEquals(addNote.getText(),"Add note");  
    40.               
    41.             addNote = new UiObject(new UiSelector().classNameMatches(".*TextView$"));  
    42.             assertEquals(addNote.getText(),"Add note");  
    43.               
    44.             //addNote = new UiObject(new UiSelector().description("AddNoteMenuDesc));  
    45.             //assertEquals(addNote.getText(),"Add note");  
    46.               
    47.             //addNote = new UiObject(new UiSelector().descriptionContains("AddNote"));  
    48.             //assertEquals(addNote.getText(),"Add note");  
    49.               
    50.             //addNote = new UiObject(new UiSelector().descriptionStartsWith("AddNote"));  
    51.             //assertEquals(addNote.getText(),"Add note");  
    52.               
    53.             //addNote = new UiObject(new UiSelector().descriptionMatches("^AddNote.*$"));  
    54.             //assertEquals(addNote.getText(),"Add note");  
    55.               
    56.             addNote = new UiObject(new UiSelector().focusable(true).text("Add note"));  
    57.             assertEquals(addNote.getText(),"Add note");  
    58.               
    59.             addNote = new UiObject(new UiSelector().focused(false).text("Add note"));  
    60.             assertEquals(addNote.getText(),"Add note");  
    61.               
    62.               
    63.             //TBD  
    64.             //addNote = new UiObject(new UiSelector().fromParent(selector))  
    65.               
    66.             addNote = new UiObject(new UiSelector().index(0).text("Add note"));  
    67.             assertEquals(addNote.getText(),"Add note");  
    68.               
    69.             addNote = new UiObject(new UiSelector().className("android.widget.TextView").enabled(true).instance(0));  
    70.             assertEquals(addNote.getText(),"Add note");  
    71.               
    72.             addNote = new UiObject(new UiSelector().longClickable(false).text("Add note"));  
    73.             assertEquals(addNote.getText(),"Add note");  
    74.               
    75.             addNote = new UiObject(new UiSelector().text("Add note"));  
    76.             assertEquals(addNote.getText(),"Add note");  
    77.               
    78.             addNote = new UiObject(new UiSelector().textContains("Add"));  
    79.             assertEquals(addNote.getText(),"Add note");  
    80.               
    81.             addNote = new UiObject(new UiSelector().textStartsWith("Add"));  
    82.             assertEquals(addNote.getText(),"Add note");  
    83.               
    84.               
    85.             addNote = new UiObject(new UiSelector().textMatches("Add.*"));  
    86.             assertEquals(addNote.getText(),"Add note");  
    87.               
    88.             addNote = new UiObject(new UiSelector().resourceId("android:id/title"));  
    89.             assertEquals(addNote.getText(),"Add note");  
    90.               
    91.             addNote = new UiObject(new UiSelector().resourceIdMatches(".+id/title"));  
    92.             assertEquals(addNote.getText(),"Add note");  
    93.               
    94.             //Go to the editor activity, need to cancel menu options first  
    95.             device.pressMenu();  
    96.             //Find out the new added note entry  
    97.             UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));    
    98.             //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));   
    99.             UiObject note = null;  
    100.             if(noteList.exists()) {  
    101.                 note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true);    
    102.                 //note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true);   
    103.             }  
    104.             else {  
    105.                 note = new UiObject(new UiSelector().text("Note1"));  
    106.             }  
    107.             assertNotNull(note);  
    108.               
    109.             //Go to the NoteEditor activity  
    110.             note.click();  
    111.             device.pressMenu();  
    112.               
    113.             UiObject save = null;  
    114.             UiObject delete = null;  
    115.               
    116.             save =  new UiObject(new UiSelector().text("Save"));  
    117.             assertEquals(save.getText(),"Save");  
    118.             delete = save.getFromParent(new UiSelector().text("Delete"));  
    119.             assertEquals(delete.getText(),"Delete");  
    120.               
    121.             delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));  
    122.             assertEquals(delete.getText(),"Delete");  
    123.               
    124.             save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));  
    125.             assertEquals(save.getText(),"Save");  
    126.               
    127.             UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));  
    128.             save = parentView.getChild(new UiSelector().text("Save"));  
    129.             assertEquals(save.getText(),"Save");  
    130.               
    131.               
    132.                 
    133.         }    
    134.   
    135. }  
  • 相关阅读:
    发送短信验证(防刷新)
    JsRender 学习总结
    JsRender (js模板引擎)
    jQuery中ready与load事件的区别
    web端图片文件直传
    2018面对对象_作业三
    2018面对对象_作业二
    2018面对对象_作业一
    2018寒假作业_4(自我总结)
    2018寒假作业_3(电梯版本二)
  • 原文地址:https://www.cnblogs.com/xiaoluosun/p/4519245.html
Copyright © 2011-2022 走看看