深度搜索
返回 | API | 说明 |
---|
BySelector | depth(int exactDepth) | 固定层级深度 |
BySelector | depth(int min, int max) | 一定范围的层级深度 |
BySelector | maxDepth(int max) | 不能超过指定层级深度 |
BySelector | minDepth(int min) | 从指定深度开始向下搜索 |
常规属性搜索
返回 | API | 说明 |
---|
BySelector | res(String resourceName) | 通过资源id搜索 |
BySelector | res(Pattern resourceName) | 通过资源id的正则匹配搜索 |
BySelector | res(String resourcePackage, String resourceId) | 通过资源包和资源id搜索 |
BySelector | text(Pattern textValue) | 通过文本的正则匹配搜索 |
BySelector | text(String textValue) | 通过文本完全匹配搜索 |
BySelector | textContains(String substring) | 通过文本包含配置搜索 |
BySelector | textEndsWith(String substring) | 通过文本结尾匹配搜索 |
BySelector | textStartsWith(String substring) | 通过文本开头匹配搜索 |
BySelector | clazz(Pattern className) | 通过类名正则匹配搜索 |
BySelector | clazz(String className) | 通过类名完整匹配搜索 |
BySelector | clazz(Class clazz) | 通过类对象搜索 |
BySelector | clazz(String packageName, String className) | 通过包名和类名搜索 |
BySelector | desc(Pattern contentDescription) | 通过描述的正则匹配搜索 |
BySelector | desc(String contentDescription) | 通过描述的完整匹配搜索 |
BySelector | descContains(String substring) | 通过描述包含匹配搜索 |
BySelector | descEndsWith(String substring) | 通过描述结尾匹配搜索 |
BySelector | descStartsWith(String substring) | 通过描述开头匹配搜索 |
BySelector | pkg(String applicationPackage) | 通过包名完整匹配搜索 |
BySelector | pkg(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
@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()
}
}
}