UiAutomator2.0笔记之By和BySelector API(九)

ByBySelector所实现的功能相同,API也基本上相同,By是对BySelector的简化,官网是这样解释的:

By is a utility class which enables the creation of BySelectors in a concise manner.

Its primary function is to provide static factory methods for constructing BySelectors using a shortened syntax. For example, you would use findObject(By.text(“foo”)) rather than findObject(new BySelector().text(“foo”)) to select UI elements with the text value “foo”.

翻译为:By是BySelector的一个实用类,By的主要功能是提供静态方法并使用简单的语法去构建BySelector进而进行控件的筛选。eg: 
当你寻找一个text为foo的控件时,BySelector的写法为:findObject(new BySelector().text(“foo”))
而By则可以对之进行简化,可以写为:findObject(By.text("foo"))

By、BySelector

API

Example

API

static BySelectorcheckable(boolean isCheckable)

构造一个new BySelector并设置可检查条件。

static BySelectorchecked(boolean isChecked)

构造一个new BySelector并设置检查的条件。

static BySelectorclazz(String packageName, String className)

构造一个new BySelector并设置类名标准。

static BySelectorclazz(Class clazz)

构造一个new BySelector并设置类名标准。

static BySelectorclazz(Pattern className)

构造一个new BySelector并设置类名标准。

static BySelectorclazz(String className)

构造一个new BySelector并设置类名标准。

static BySelectorclickable(boolean isClickable)

构造一个new BySelector并设置可单击的条件。

static BySelectorcopy(BySelector original)

构造一个新的BySelector并复制标准original

static BySelectordepth(int depth)

构造一个新的BySelector并设置深度标准。

static BySelectordesc(String contentDescription)

构造一个新的BySelector并设置内容描述标准。

static BySelectordesc(Pattern contentDescription)

构造一个新的BySelector并设置内容描述标准。

static BySelectordescContains(String substring)

构造一个新的BySelector并设置内容描述标准。

static BySelectordescEndsWith(String substring)

构造一个新的BySelector并设置内容描述标准。

static BySelectordescStartsWith(String substring)

构造一个新的BySelector并设置内容描述标准。

static BySelectorenabled(boolean isEnabled)

构造一个new BySelector并设置启用的条件。

static BySelectorfocusable(boolean isFocusable)

构造一个new BySelector并设置可聚焦标准。

static BySelectorfocused(boolean isFocused)

构造一个新的BySelector并设置重点标准。

static BySelectorhasChild(BySelector childSelector)

构造一个new BySelector并添加子选择器条件。

static BySelectorhasDescendant(BySelector descendantSelector, int maxDepth)

构造一个new BySelector并添加一个后代选择器条件。

static BySelectorhasDescendant(BySelector descendantSelector)

构造一个new BySelector并添加一个后代选择器条件。

static BySelectorlongClickable(boolean isLongClickable)

构造一个new BySelector并设置长可点击标准。

static BySelectorpkg(Pattern applicationPackage)

构造一个new BySelector并设置应用程序包名称条件。

static BySelectorpkg(String applicationPackage)

构造一个new BySelector并设置应用程序包名称条件。

static BySelectorres(String resourceName)

构造一个new BySelector并设置资源名称条件。

static BySelectorres(String resourcePackage, String resourceId)

构造一个new BySelector并设置资源名称条件。

static BySelectorres(Pattern resourceName)

构造一个new BySelector并设置资源ID标准。

static BySelectorscrollable(boolean isScrollable)

构造一个new BySelector并设置可滚动条件。

static BySelectorselected(boolean isSelected)

构造一个new BySelector并设置所选条件。

static BySelectortext(Pattern regex)

构造一个new BySelector并设置文本值标准。

static BySelectortext(String text)

构造一个new BySelector并设置文本值标准。

static BySelectortextContains(String substring)

构造一个new BySelector并设置文本值标准。

static BySelectortextEndsWith(String substring)

构造一个new BySelector并设置文本值标准。

static BySelectortextStartsWith(String substring)

构造一个new BySelector并设置文本值标准。

Example

    //获取UiDevice实例
    UiDevice mDevice=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    //以id筛选控件
    UiObject2 addBtn = mDevice.findObject(By.res("com.star.uiautomator:id/add")).click();
    //以控件描述description筛选控件
    UiObject2 resultBtn = mDevice.findObject(By.res("calculator the result"));

    //多个条件组合筛选控件
    UiObject2 resbutton = mDevice.findObject(By.desc("calculator the result")
                        .clazz(Button.class)
                        .res("com.star.uiautomator:id/calculator")
                        .pkg("com.star.uiautomator")
                        .scrollable(false)
                        .selected(false));  
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值