【问题标题】:UiDevice object throwing error when we start Instrumentation using startInstrumentation() from main activity当我们使用 main Activity 中的 startInstrumentation() 启动 Instrumentation 时,UiDevice 对象抛出错误
【发布时间】:2018-02-09 10:21:30
【问题描述】:

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

我的 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. 是否有可能通过在没有 PC 连接的情况下启动 apk 来执行 Instrumentation 测试?

(无 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 -----

【问题讨论】:

    标签: android junit4 instrumentation android-uiautomator


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多