【问题标题】:Why does my test using sendKeys() not animate in the Android emulator?为什么我使用 sendKeys() 的测试在 Android 模拟器中没有动画?
【发布时间】:2012-11-01 00:14:24
【问题描述】:

当我在 Android 模拟器中运行以下测试时,输入值不会显示在 EditText 中。为什么不呢?我需要更改什么才能在模拟器中看到输入? (测试通过了,所以最后可能没关系。我只是希望能够在模拟器中看到它实际发生。)

public void testOkButtonOnClickWithUserInputNumber() throws Throwable {
    this.sendKeys(Integer.toString(this.testNumber)); // 123

    this.runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            Assert.assertTrue(NumberFilterTest.this.okButton.performClick());
        }
    });

    this.getInstrumentation().waitForIdle(new Runnable() {
        @Override
        public void run() {
            Assert.assertTrue(NumberFilterTest.this.activity.isFinishing());
        }
    });
}

【问题讨论】:

    标签: android unit-testing android-emulator junit


    【解决方案1】:

    查看官方开发指南 - Activity Testing

    1 确保触摸模式已关闭:

    要使用您从测试中发送的关键事件来控制模拟器或设备,您必须关闭触摸模式。如果您不这样做,则忽略关键事件。

     ActivityInstrumentationTestCase2.setActivityTouchMode(false);
    

    2 确保屏幕已解锁:

    如果使用键盘保护模式禁用了模拟器或设备的主屏幕,您可能会发现 UI 测试不起作用。这是因为被测应用无法接收到 sendKeys() 发送的按键事件。

     mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
     mLock = mKeyGuardManager.newKeyguardLock("activity_classname");
     mLock.disableKeyguard();
    

    【讨论】:

    • 还要注意 sendKeys() 不允许在 UI 线程上运行,因此请确保您没有在应用程序的 UI 线程上调用它。
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多