1.在build.gradle中添加依賴:java
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)android
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
shell命令啓動app
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爲固定格式