打开节点查看工具 uiautomatorviewer.bat目录在AndroidSDK\tools\bin
然后gradle导入框架
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'打开app目录下的src/androidTest/java 新建一个CustomTest
@RunWith(AndroidJUnit4.class) public class CustomTest { public Instrumentation mInstrumentation; public UiDevice mUiDevice; @Before public void setInit() { mInstrumentation = InstrumentationRegistry.getInstrumentation(); mUiDevice = UiDevice.getInstance(mInstrumentation); } @Test public void testSum() throws InterruptedException { Thread.sleep(500); //控件的desc mUiDevice.findObject(By.desc("DevCheck")).click(); //控件的res id mUiDevice.findObject(By.res("com.miui.home:id/icon_icon")).click(); //控件的文本内容 mUiDevice.findObject(By.text("DevCheck")).click(); //字符串中包含指定文本的内容 mUiDevice.findObject(By.textContains("排行")).click(); //home键 mUiDevice.pressHome(); mUiDevice.pressHome(); //menu键 mUiDevice.pressMenu(); //滑动操作 参数依次为左上XY 右下XY 最后一个是步长 mUiDevice.swipe(51, 1260, 879, 1260,10); } }节点的信息在uiautomatorviewer查看即可