Android: 检查一个按钮是否启用

Android: checking a button is enabled

我在测试我的应用程序时遇到问题。我创建了一个应该失败的浓缩咖啡测试,因为每当我在模拟器中启动我的应用程序时,我都会得到预期的行为。这是我的测试:

 onView(withText("wrong answer")).perform(click());
 onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));

启动测试时,没有任何报告,而 nextQuestionButton 应该 在单击其文本为“错误答案”的单选按钮时启用。

据我了解,您希望它这样工作:

if nextQuestionButton IS enabled, then take following actions:

  • click on 'wrong answer',
  • check if nextQuestionButton changed stated to NOT enabled.

如果是这样,代码应该是这样的:

onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));
onView(withText("wrong answer")).perform(click());
onView(withId(R.id.nextQuestionButton)).check(matches(not(isEnabled())));

Espresso 允许您在测试中使用 Hamcrest 匹配器。

Hamcrest 1.3 Quick Reference.

请同时检查这个(如果您还没有这样做的话):

Espresso 2.1. Espresso Cheat Sheet Master [更新]

根据你的这个片段 post:

When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is "wrong answer".

这意味着你没有设置禁用你的下一个问题按钮,所以 Espresso 通过了这个测试。