gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:42:17 31 4
gpt4 key购买 nike

我正在尝试使用 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 -----

最佳答案

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

关于android - 当我们从主 Activity 使用 startInstrumentation() 启动 Instrumentation 时,UiDevice 对象抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45981820/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com