如何从android应用程序代码运行UiAutomator测试?

问题描述 投票:0回答:1

我使用命令gradlew :app:assembleDebug :app:assembleDebugAndroidTest进行了Android测试并编译了项目。

然后,我安装了两个apk:app-debug.apk和app-debug-androidTest.apk,并使用命令am instrument -w -r -e debug false com.rhino.uiauto.test/android.support.test.runner.AndroidJUnitRunner通过ADB运行测试>

所有作品。但是现在我需要从android应用程序中的代码运行测试,但出现错误Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object reference。应用代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runTesting();
    }

    public void runTesting() {
        try {
            Bundle arguments = new Bundle();
            arguments.putString("class", "com.rhino.uiauto.ApplicationTest");
            startInstrumentation(new ComponentName("com.rhino.uiauto.test", "android.support.test.runner.AndroidJUnitRunner"), null, arguments);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的测试班级代码:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
@LargeTest
public class ApplicationTest {
    private UiDevice mDevice;

    @Before
    public void before() {
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        mDevice = UiDevice.getInstance(instrumentation);
        mDevice.pressHome();
    }

    @Test
    public void test() throws Exception {
        // do something
    }
}

错误消息:

12-02 13:29:19.450 22124-22148/? 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.QueryController.<init>(QueryController.java:95)
        at android.support.test.uiautomator.UiDevice.<init>(UiDevice.java:109)
        at android.support.test.uiautomator.UiDevice.getInstance(UiDevice.java:261)
        at com.rhino.uiauto.ApplicationTest.before(ApplicationTest.java:42)
        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 android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76)
        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 android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
        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:56)
        at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1893)

我不了解错误原因。有人知道吗?可能有什么方法可以在没有ADB且不连接PC的情况下从应用程序中的代码运行测试?可以使用shell命令运行测试吗?

谢谢

我使用gradlew:app:assembleDebug:app:assembleDebugAndroidTest命令进行了Android测试并编译了项目。然后,我安装了两个apk:app-debug.apk和app-debug-androidTest.apk并运行测试...

android android-testing android-uiautomator
1个回答
0
投票

我有同样的问题。到处搜索都没有运气。我尝试将UiDevice.getInstance()移至@Before和@Test,但均无效。

© www.soinside.com 2019 - 2024. All rights reserved.