BySelector介绍

深度搜索
返回API说明
BySelectordepth(int exactDepth)固定层级深度
BySelectordepth(int min, int max)一定范围的层级深度
BySelectormaxDepth(int max)不能超过指定层级深度
BySelectorminDepth(int min)从指定深度开始向下搜索
常规属性搜索
返回API说明
BySelectorres(String resourceName)通过资源id搜索
BySelectorres(Pattern resourceName)通过资源id的正则匹配搜索
BySelectorres(String resourcePackage, String resourceId)通过资源包和资源id搜索
BySelectortext(Pattern textValue)通过文本的正则匹配搜索
BySelectortext(String textValue)通过文本完全匹配搜索
BySelectortextContains(String substring)通过文本包含配置搜索
BySelectortextEndsWith(String substring)通过文本结尾匹配搜索
BySelectortextStartsWith(String substring)通过文本开头匹配搜索
BySelectorclazz(Pattern className)通过类名正则匹配搜索
BySelectorclazz(String className)通过类名完整匹配搜索
BySelectorclazz(Class clazz)通过类对象搜索
BySelectorclazz(String packageName, String className)通过包名和类名搜索
BySelectordesc(Pattern contentDescription)通过描述的正则匹配搜索
BySelectordesc(String contentDescription)通过描述的完整匹配搜索
BySelectordescContains(String substring)通过描述包含匹配搜索
BySelectordescEndsWith(String substring)通过描述结尾匹配搜索
BySelectordescStartsWith(String substring)通过描述开头匹配搜索
BySelectorpkg(String applicationPackage)通过包名完整匹配搜索
BySelectorpkg(Pattern applicationPackage)通过包名正则匹配搜索
逻辑属性搜索
API
checkable(boolean isCheckable)
checked(boolean isChecked)
enabled(boolean isEnabled)
focusable(boolean isFocusable)
focused(boolean isFocused)
longClickable(boolean isLongClickable)
scrollable(boolean isScrollable)
selected(boolean isSelected)
示例代码:
package mqqzt.myuitest4;

import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.widget.Button;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.regex.Pattern;

/**
 * Created by mqqzt-yang on 2015/11/22.
 */
@RunWith(AndroidJUnit4.class)
public class BySelectorTest {
    UiDevice uiDevice;
    Instrumentation instrumentation;

    @Before
    public void setUp(){
        instrumentation = InstrumentationRegistry.getInstrumentation();
        uiDevice = UiDevice.getInstance(instrumentation);
    }

    @Test
    public void testBySelectBy(){
        UiObject2 uiObject2 = uiDevice.findObject(By.text("Settings"));
        uiObject2.click();
    }

    @Test
    public void testDepth(){
        UiObject2 uiObject2 = uiDevice.findObject(By.depth(7));
        uiObject2.click();

        UiObject2 uiObject3 = uiDevice.findObject(By.text("1").depth(3, 7));
        uiObject3.click();

        UiObject2 uiObject4 = uiDevice.findObject(By.text("1").maxDepth(7));
        uiObject4.click();

        UiObject2 uiObject5 = uiDevice.findObject(By.text("1").minDepth(3));
        uiObject5.click();
    }

    @Test
    public void testNormal(){
        Pattern p = Pattern.compile("[2-9]");
        UiObject2 uiObject2 = uiDevice.findObject(By.text(p).clazz(Button.class));
        uiObject2.click();
    }

    @Test
    public void testBoolean(){
        UiObject2 uiObject2 = uiDevice.findObject(By.checkable(true));
        uiObject2.click();
        sleep(1000);
        uiObject2.click();
    }


    public void sleep(int sleep){
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值