1、 SDK Manager需要安装Android Support Repository。
2、 build.gradle配置(Module中的gradle)
defaultConfig {
… …
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// … …
dependencies {
… …
/***********************************/
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test:rules:0.5', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test:runner:0.5', {
exclude group: 'com.android.support', module: 'support-annotations'
})
/***********************************/
}
3、 编写测试类,测试类需要放到“androidTest”下:
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiAutomatorInstrumentationTestRunner;
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 org.junit.runner.RunWith;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
/**
*
*/
@RunWith(AndroidJUnit4.class)
public class ApplicationTest extends UiAutomatorInstrumentationTestRunner {
private UiDevice uiDevice;
public void testDemo() throws UiObjectNotFoundException {
uiDevice = UiDevice.getInstance(getInstrumentation());
UiObject btn1 = uiDevice.findObject(new UiSelector().description(""));
}
}