1、首先建立Android工程
2、在工程的build.gradle(Modul:app)中添加依赖
3、注意下gradle的版本和对应appcompat-v 的版本要相对应,不然这里有可能报错
4、写简单的代码:
import android.app.Instrumentation; import android.content.Context; 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.Until; import android.util.Log; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import java.io.IOException; /** * Instrumented test, which will execute on an Android device. * * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { //定义全局变量 public Instrumentation mInstrumentation; public UiDevice mUiDevice; public Context mAppContext; public Context mContext; //初始化全局变量 public static String Tag = "Testing"; public void beforeClassInit(){ mInstrumentation = InstrumentationRegistry.getInstrumentation(); mAppContext = InstrumentationRegistry.getTargetContext(); mContext = InstrumentationRegistry.getContext(); mUiDevice = UiDevice.getInstance(mInstrumentation); } @BeforeClass public static void BeforeClass(){ Log.i(Tag,"运行整个test类之前运行"); } @AfterClass public static void AfterClass(){ Log.i(Tag,"运行完整个test类之后运行"); } @Before public void Before(){ Log.i(Tag,"运行每一个test类之前运行"); beforeClassInit(); } @After public void After(){ Log.i(Tag,"运行完每一个test类之后运行"); } @Test public void test01Demo() throws IOException { String command = "am start -n com.android.settings/.Settings"; mUiDevice.executeShellCommand(command); mUiDevice.findObject(By.text("蓝牙")).click(); mUiDevice.wait(Until.findObject(By.text("接收到的文件")),5000); } }
6、最近看了虫师的博客后发现了一个可以结合Python+Uiautomator2使用的方式,很强大~
https://www.cnblogs.com/fnng/p/8486863.html,可以参考