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的部分源码片段:

package android.support.test.uiautomator;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class BySelector {

    BySelector() {
    }


    public BySelector clazz(String className) {
        checkNotNull(className, "className cannot be null");
        return className.charAt(0) == 46?this.clazz("android.widget", className.substring(1)):this.clazz(Pattern.compile(Pattern.quote(className)));
    }

By类的部分源码片段:

package android.support.test.uiautomator;

import android.support.test.uiautomator.BySelector;
import java.util.regex.Pattern;

public class By {
    private By() {
    }

    public static BySelector clazz(String className) {
        return (new BySelector()).clazz(className);
    }

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链接,并进行点击:

布局文件:

Uiautomator 2.0之BySelector类学习小记_第1张图片

用例代码:

@Test
    public void testCase03(){
        //通过固定层级,查找元素
        UiObject2 ui = mDevice.findObject(By.depth(11));
        Log.i("testCase03", ui.getText());
        ui.click();


        //通过设定层级范围,查找元素
        UiObject2 ui2 = mDevice.findObject(By.clazz(TextView.class).depth(10, 11));
        Log.i("testCase03", ui2.getText());
        ui2.click();


        //通过设定最小层级限制,查找元素
        UiObject2 ui3 = mDevice.findObject(By.clazz(TextView.class).minDepth(10));
        Log.i("testCase03", ui3.getText());
        ui3.click();

    }

运行结果:


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

布局文件:

Uiautomator 2.0之BySelector类学习小记_第2张图片

用例代码:

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

    }


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 通过文本值或者正则表达式,进行查找定位元素,代码如下:

@Test
    public void testCase04(){
        //通过Text值,进行匹配查找
        UiObject2 ui = mDevice.findObject(By.text("8"));
        ui.click();

        //通过正则表达式,进行匹配查找
        Pattern p = Pattern.compile("[8-9]");
        UiObject2 ui2 = mDevice.findObject(By.text(p));
        ui2.click();
    }

3.2 运行结果:

Uiautomator 2.0之BySelector类学习小记_第3张图片


4. 逻辑属性搜索

4.1 代码示例

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

5. 后代搜索

5.1 API介绍

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

5.2 代码示例

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

你可能感兴趣的:(android,测试)