By和BySelector所实现的功能相同,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"))
API
static BySelector | checkable(boolean isCheckable) 构造一个new |
static BySelector | checked(boolean isChecked) 构造一个new |
static BySelector | clazz(String packageName, String className) 构造一个new |
static BySelector | clazz(Class clazz) 构造一个new |
static BySelector | clazz(Pattern className) 构造一个new |
static BySelector | clazz(String className) 构造一个new |
static BySelector | clickable(boolean isClickable) 构造一个new |
static BySelector | copy(BySelector original) 构造一个新的 |
static BySelector | depth(int depth) 构造一个新的 |
static BySelector | desc(String contentDescription) 构造一个新的 |
static BySelector | desc(Pattern contentDescription) 构造一个新的 |
static BySelector | descContains(String substring) 构造一个新的 |
static BySelector | descEndsWith(String substring) 构造一个新的 |
static BySelector | descStartsWith(String substring) 构造一个新的 |
static BySelector | enabled(boolean isEnabled) 构造一个new |
static BySelector | focusable(boolean isFocusable) 构造一个new |
static BySelector | focused(boolean isFocused) 构造一个新的 |
static BySelector | hasChild(BySelector childSelector) 构造一个new |
static BySelector | hasDescendant(BySelector descendantSelector, int maxDepth) 构造一个new |
static BySelector | hasDescendant(BySelector descendantSelector) 构造一个new |
static BySelector | longClickable(boolean isLongClickable) 构造一个new |
static BySelector | pkg(Pattern applicationPackage) 构造一个new |
static BySelector | pkg(String applicationPackage) 构造一个new |
static BySelector | res(String resourceName) 构造一个new |
static BySelector | res(String resourcePackage, String resourceId) 构造一个new |
static BySelector | res(Pattern resourceName) 构造一个new |
static BySelector | scrollable(boolean isScrollable) 构造一个new |
static BySelector | selected(boolean isSelected) 构造一个new |
static BySelector | text(Pattern regex) 构造一个new |
static BySelector | text(String text) 构造一个new |
static BySelector | textContains(String substring) 构造一个new |
static BySelector | textEndsWith(String substring) 构造一个new |
static BySelector | textStartsWith(String substring) 构造一个new |
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));