1.在build.gradle中添加依赖:
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
implementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
implementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
2.新建实例:在androidTest文件下新建测试类方法(例:test1.java)
package com.example.myapplication; import android.os.Bundle; import android.support.test.InstrumentationRegistry; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.util.Log; import org.junit.Test; import static android.support.test.InstrumentationRegistry.getArguments; public class test1{ private UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); //获取参数 Bundle bundle = getArguments(); private int displayWidth = uiDevice.getDisplayWidth(); private int displayHeight = uiDevice.getDisplayHeight(); @Test public void test() throws InterruptedException, UiObjectNotFoundException { uiDevice.pressHome(); Log.i("test1", "在等待1111111111111"); uiDevice.swipe(displayWidth / 2, displayHeight / 2, 0, displayHeight / 2, 10); while (true) { UiObject objectA = uiDevice.findObject(new UiSelector().text("华为钱包")); Thread.sleep(1000); if (objectA.exists()) { Thread.sleep(1000); // objectA.click(); // Thread.sleep(1000); uiDevice.pressHome(); ShellRunner.shell("curl -d "123" http://192.168.51.225:8000/ud"); Thread.sleep(1000); } } } }
启动uiautomator测试方法
shell命令启动
am instrument -w -r -e debug false -e class com.example.myapplication.test1#test com.example.myapplication.test/android.support.test.runner.AndroidJUnitRunner
com.example.myapplication.test1#test com.example.myapplication.test
格式为:报名.测试类名#测试方法 报名.test/android.support.test.runner.AndroidJUnitRunner
报名.test/android.support.test.runner.AndroidJUnitRunner为固定格式