当我们从主活动使用startInstrumentation()启动Instrumentation时,UiDevice对象抛出错误

时间:2017-08-31 12:47:55

标签: android junit4 instrumentation uiautomator android-uiautomator

我正在尝试使用startInstrumentation()从Android应用程序访问检测(通过启动应用程序);

我的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的值显示" true"表示它(com.example.per.simpleuiauto)已启动

问题是:一旦Instrumentation启动并调用setUp(),在下面的行获取NullPointerException

mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

完整代码位于

之下
private static UiDevice mDevice;
    @Before
    public void setUp() throws Exception {
        Log.d(TAG, "setUp: called");
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mDevice.pressHome();
    }

我的问题是:

  1. 这可以从没有adb的Android应用程序启动检测测试吗?

  2. 是否有可能通过启动apk而无需pc连接来执行Instrumentation测试?

  3. (没有adb有线或无线)。

    以下是我的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 -----
    

1 个答案:

答案 0 :(得分:0)

如果您的测试尚未从adb开始,则无法获取UiDevice实例。