UiDevice对象在我们启动时抛出错误使用startInstrumentation()从主活动中检测
问题描述:
我试图使用startInstrumentation()从Android应用程序(通过启动应用程序)访问工具条;UiDevice对象在我们启动时抛出错误使用startInstrumentation()从主活动中检测
我MainActivty代码如下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle arguments = new Bundle();
arguments.putString("package", "com.example.per.simpleuiauto");
boolean returnValue = startInstrumentation(new ComponentName("com.example.per.simpleuiauto.test", "android.support.test.runner.AndroidJUnitRunner"), null, arguments);
Log.d(TAG, "returnValue: "+returnValue);
}
值的returnValue的显示“真”,这表明它(com.example.per.simpleuiauto)推出
问题是:一旦仪器启动,设置()被调用时,在低于线
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
完整代码得到的NullPointerException低于
private static UiDevice mDevice;
@Before
public void setUp() throws Exception {
Log.d(TAG, "setUp: called");
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressHome();
}
我的问题是:
这是可能的,开始从Android应用仪器测试亚行不?
任何可能执行仪器测试通过启动apk没有电脑连接?
(无adb有线或无线)。
下面是我的亚行的崩溃日志:
15:47:41.788 6578-6578/? I/MonitoringInstrumentation: Instrumentation Started!
08-31 15:47:41.794 6578-6578/com.example.per.simpleuiauto I/MonitoringInstrumentation: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example.per.simpleuiauto.test-2/base.apk", zip file "/data/app/com.example.per.simpleuiauto-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.per.simpleuiauto.test-2/lib/arm, /data/app/com.example.per.simpleuiauto-1/lib/arm, /system/lib, /vendor/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example.per.simpleuiauto.test-2/base.apk", zip file "/data/app/com.example.per.simpleuiauto-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.per.simpleuiauto.test-2/lib/arm, /data/app/com.example.per.simpleuiauto-1/lib/arm, /system/lib, /vendor/lib]]]'
08-31 15:47:41.798 6578-6578/com.example.per.simpleuiauto I/MonitoringInstrumentation: No JSBridge.
08-31 15:47:41.806 6578-6596/com.example.per.simpleuiauto D/InfraTrack: Tracking disabled due to lack of internet permissions
08-31 15:47:41.806 6578-6596/com.example.per.simpleuiauto I/UsageTrackerFacilitator: Usage tracking disabled
08-31 15:47:41.807 6578-6596/com.example.per.simpleuiauto I/TestRequestBuilder: Scanning classpath to find tests in apks [/data/app/com.example.per.simpleuiauto.test-2/base.apk]
08-31 15:47:41.921 6578-6596/com.example.per.simpleuiauto D/TestExecutor: Adding listener android.support.test.internal.runner.listener.LogRunListener
08-31 15:47:41.921 6578-6596/com.example.per.simpleuiauto D/TestExecutor: Adding listener android.support.test.internal.runner.listener.InstrumentationResultPrinter
08-31 15:47:41.921 6578-6596/com.example.per.simpleuiauto D/TestExecutor: Adding listener android.support.test.internal.runner.listener.ActivityFinisherRunListener
08-31 15:47:41.924 6578-6596/com.example.per.simpleuiauto I/TestRunner: run started: 2 tests
08-31 15:47:41.928 6578-6596/com.example.per.simpleuiauto I/TestRunner: started: testAdd(com.example.per.simpleuiauto.CalculatorTester)
08-31 15:47:41.930 6578-6578/com.example.per.simpleuiauto I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
08-31 15:47:41.938 6578-6596/com.example.per.simpleuiauto I/TestRunner: failed: testAdd(com.example.per.simpleuiauto.CalculatorTester)
08-31 15:47:41.938 6578-6596/com.example.per.simpleuiauto I/TestRunner: ----- begin exception -----
08-31 15:47:41.941 6578-6596/com.example.per.simpleuiauto I/TestRunner: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object reference
at android.support.test.uiautomator.UiAutomatorBridge.setOnAccessibilityEventListener(UiAutomatorBridge.java:78)
at android.support.test.uiautomator.QueryController.<init>(QueryController.java:58)
at android.support.test.uiautomator.UiAutomatorBridge.<init>(UiAutomatorBridge.java:66)
at android.support.test.uiautomator.InstrumentationUiAutomatorBridge.<init>(InstrumentationUiAutomatorBridge.java:35)
at android.support.test.uiautomator.UiDevice.<init>(UiDevice.java:103)
at android.support.test.uiautomator.UiDevice.getInstance(UiDevice.java:262)
at com.example.per.simpleuiauto.CalculatorTester.naveen(CalculatorTester.java:32)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2022)
08-31 15:47:41.942 6578-6596/com.example.per.simpleuiauto I/TestRunner: ----- end exception -----
答
如果您的测试尚未从adb
启动,则无法获得的UIDevice实例。