如何在android最新版本中以编程方式执行UiAutomator测试用例

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

当我使用adb shell命令执行时,下面的代码工作正常。但是,如果我尝试使用apk,它就无法正常工作。有帮助吗?

MainActivity.class

public void onStart() {

    super.onStart();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bundle bundle = new Bundle();
            bundle.putString("Test", "value");

            try {
                startInstrumentation(new ComponentName("com.example.mypro.uiauto.test", "android.support.test.runner.AndroidJUnitRunner"),null, bundle);

            } catch (Exception e) {

            }
        }
    });


}

ExampleInstrumentedTest.class

@Test
public void pressHome() {

    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    uiDevice.pressHome();
}
android android-instrumentation android-uiautomator
1个回答
0
投票

我去年八月陷入了同样的局面。到目前为止,您无法从APK触发仪表化测试。这是Android guide的链接。因为在这种情况下您也可以访问其他应用程序的UI。这将是整个Android操作系统中的一个安全问题,其中一个应用程序正在打开另一个应用程序并做随机的东西,这是一个威胁。

您可以通过命令提示符/终端或仅通过Android工作室执行检测测试。我共享的链接,有一个标题为“在设备或模拟器上运行UI Automator测试”的列。

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