如何点击Android Junit Test中的Switch?

时间:2014-01-09 06:10:07

标签: android junit switch-statement android-ui junit3

我使用以下代码检查并取消选中Android的Switch组件。 如何在Android Junit Testing中通过触摸实用程序检查 - 取消选中?

        // wait 2 seconds for the start of the activity
    final FinalizeCalibrationActivity finalActivity = (FinalizeCalibrationActivity) monitorFinalActivity
            .waitForActivityWithTimeout(2000);
    assertNotNull("FinalizeCalibrationActivity is null", finalActivity);

    final Switch swtSignedRdwPortal = (Switch) finalActivity
            .findViewById(R.id.swt_signed_rdw_portal);

    JunitUtils.waitTime(3);
    swtSignedRdwPortal.setChecked(false);

    JunitUtils.waitTime(3);
    swtSignedRdwPortal.setChecked(true);

1 个答案:

答案 0 :(得分:0)

我们无法直接使用Switch视图。 它将抛出失败的断言,如: “机器人仅最原始线程该创建-A-视图层级灿触摸其-意见”。

我用过这样的代码重写

// wait 2 seconds for the start of the activity
    final FinalizeCalibrationActivity finalActivity = (FinalizeCalibrationActivity) monitorFinalActivity
            .waitForActivityWithTimeout(2000);
    assertNotNull("FinalizeCalibrationActivity is null", finalActivity);

    final Switch swtSignedRdwPortal = (Switch) finalActivity
            .findViewById(R.id.swt_signed_rdw_portal);

    JunitUtils.waitTime(3);
    finalActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            swtSignedRdwPortal.setChecked(false);
        }
    });

    JunitUtils.waitTime(3);
    finalActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            swtSignedRdwPortal.setChecked(true);
        }
    });

在Android Junit Test中,这对我来说很好。!!!