build.gradle
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
在编译Android工程文件,由于是将其他工程代码移到该工程下编译的,所以编译出了点问题,报Error running app: Default Activity not found错误。
在AndroidManifest.xml文件中
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
加入红色部分
第一个工程
package com.example.myapplication;
import android.app.Instrumentation;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class Testone {
public Instrumentation mInstrumentation;
public UiDevice mUiDevice;
@Before
public void setUp(){
mInstrumentation = InstrumentationRegistry.getInstrumentation();
UiDevice mUIDevice = UiDevice.getInstance(mInstrumentation); //获得device对像
}
@Test
public void testone(){
mUiDevice.findObject(By.text("Photos").clazz("android.widget.TextView")).click();
}
}