zoukankan      html  css  js  c++  java
  • Uiautomator 2.0之BySelector类学习小记

    1. BySelector与By静态类

    1.1 BySelector类为指定搜索条件进行匹配UI元素, 通过UiDevice.findObject(BySelector)方式进行使用。

    1.2 By类是一个实用程序类,可以以简洁的方式创建BySelectors对象。主要功能是使用缩短语法,提供静态工厂方法来构造BySelectors对象。例如:你将使用findObject(By.text("foo")),而不是findObject(new Selector().text("foo"))的方式来查找文本值为“foo”的UI元素。

    1.3 通过阅读BySelector类和By类的源代码,可以很清晰的知道两者的关系。 

    BySelector的部分源码片段:

     1 package android.support.test.uiautomator;
     2 
     3 import java.util.Iterator;
     4 import java.util.LinkedList;
     5 import java.util.List;
     6 
     7 public class BySelector {
     8 
     9     BySelector() {
    10     }
    11 
    12 
    13     public BySelector clazz(String className) {
    14         checkNotNull(className, "className cannot be null");
    15         return className.charAt(0) == 46?this.clazz("android.widget", className.substring(1)):this.clazz(Pattern.compile(Pattern.quote(className)));
    16     }

    By类的部分源码片段:

     1 package android.support.test.uiautomator;
     2 
     3 import android.support.test.uiautomator.BySelector;
     4 import java.util.regex.Pattern;
     5 
     6 public class By {
     7     private By() {
     8     }
     9 
    10     public static BySelector clazz(String className) {
    11         return (new BySelector()).clazz(className);
    12     }

    2. 搜索层级深度控制

    2.1. 相关AP介绍

    返回值 API 说明
    BySelector depth(int depth) 通过设定固定层级,进行匹配查找元素。
    BySelector depth (int min, int max) 通过设定层级返回,进行匹配查找元素。
    BySelector minDepth(int min) 通过设定最小层级限制,进行匹配查找元素。
    BySelector maxDepth (int max) 通过设定最大层级限制,进行匹配查找元素。

    2.2 简单示例:

    2.2.1 通过设定固定层级,层级范围,最小层级等方式进行查找元素,e.g.查找如下图的App Permission链接,并进行点击:

    布局文件:

    用例代码:

     1 @Test
     2     public void testCase03(){
     3         //通过固定层级,查找元素
     4         UiObject2 ui = mDevice.findObject(By.depth(11));
     5         Log.i("testCase03", ui.getText());
     6         ui.click();
     7 
     8 
     9         //通过设定层级范围,查找元素
    10         UiObject2 ui2 = mDevice.findObject(By.clazz(TextView.class).depth(10, 11));
    11         Log.i("testCase03", ui2.getText());
    12         ui2.click();
    13 
    14 
    15         //通过设定最小层级限制,查找元素
    16         UiObject2 ui3 = mDevice.findObject(By.clazz(TextView.class).minDepth(10));
    17         Log.i("testCase03", ui3.getText());
    18         ui3.click();
    19 
    20     }

    运行结果:

    2.2.2 通过设定最大层级条件,进行查找元素,e.g. 查找如下图的返回按钮,并且点击:

    布局文件:

    用例代码:

    1    @Test
    2     public void testCase03(){
    3         //通过设定最大层级限制,查找元素
    4         UiObject2 ui4 = mDevice.findObject(By.clazz(ImageButton.class).maxDepth(4));
    5         ui4.click();
    6 
    7     }

    2.2.3 补充说明:

    By类中只提供了固定层级搜索的方法可直接通过By.depth (int exactDepth)的方式进行调用,而层级范围搜索,最小层级搜索限制,最大层级搜索限制等方法,只能通过BySelector对象进行调用,实际应用中可通过By.clazz(TextView.class).minDepth(10)的方式进行调用,即先通过By.clazz(TextView.class)的方式获取到一个BySelector对象,再通过BySelector对象就可以进行调用depth (int min, int max),minDepth(int min),maxDepth (int max)这些方法了。

    3. 常规属性搜索,比如:

    3.1 通过文本值或者正则表达式,进行查找定位元素,代码如下:

     1 @Test
     2     public void testCase04(){
     3         //通过Text值,进行匹配查找
     4         UiObject2 ui = mDevice.findObject(By.text("8"));
     5         ui.click();
     6 
     7         //通过正则表达式,进行匹配查找
     8         Pattern p = Pattern.compile("[8-9]");
     9         UiObject2 ui2 = mDevice.findObject(By.text(p));
    10         ui2.click();
    11     }

    3.2 运行结果:

    4. 逻辑属性搜索

    4.1 代码示例

    1 @Test
    2     public void testCase05(){
    3         UiObject2 ui = mDevice.findObject(By.checked(false));
    4         ui.click();
    5     }

    5. 后代搜索

    5.1 API介绍

    API 说明
    hasChild (BySelector childSelector) 添加一个子类匹配的选择条件
    hasDescendant (BySelector descendantSelector, int maxDepth) 添加一个后代匹配的选择条件,可设定搜索层级
    hasDescendant (BySelector descendantSelector) 添加一个后代匹配的选择条件

    5.2 代码示例

    1 @Test
    2     public void testCase05(){
    3        UiObject2 ui = mDevice.findObject(By.clazz(LinearLayout.class).hasChild(By.text("设置")));
    4         ui.click();
    5     }

     原创:http://blog.csdn.net/swordgirl2011/article/details/50990584

  • 相关阅读:
    Eclipse护眼技巧
    Maven搭建SSM框架(Spring+SpringMVC+MyBatis)
    Spring之各jar包作用
    Maven新建web项目jsp报错
    js金额转大写(万元为单位)
    linux常用指令
    ie8下数组不支持indexOf方法解决方法
    string,stringBuffer,stringBuilder的比较
    input限制输入
    spring boot Mybatis --maven
  • 原文地址:https://www.cnblogs.com/successcai/p/6102200.html
Copyright © 2011-2022 走看看