java Android:检查按钮是否启用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32880107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: checking a button is enabled
提问by サソリ
I am having problems with testing my app. I created an espresso test which is supposed to fail, since whenever I launch my app in the emulator, I get the expected behavior. There is my test:
我在测试我的应用程序时遇到问题。我创建了一个应该失败的浓缩咖啡测试,因为每当我在模拟器中启动我的应用程序时,我都会得到预期的行为。有我的测试:
onView(withText("wrong answer")).perform(click());
onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));
When launching the test, nothing is reported, whereas nextQuestionButton should notbe enabled upon clicking the radioButton whose text is "wrong answer".
当启动测试,没有任何报道,而nextQuestionButton应该不会在点击其文字是“错误的答案”的单选按钮被激活。
回答by piotrek1543
According to what I understand, you want it to work like this:
根据我的理解,你希望它像这样工作:
if
nextQuestionButton
IS enabled, then take following actions:
- click on 'wrong answer',
- check if
nextQuestionButton
changed stated to NOT enabled.
如果
nextQuestionButton
启用了 IS,则执行以下操作:
- 点击“错误答案”,
- 检查是否
nextQuestionButton
更改为未启用。
If this is so, the code should be like this:
如果是这样,代码应该是这样的:
onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));
onView(withText("wrong answer")).perform(click());
onView(withId(R.id.nextQuestionButton)).check(matches(not(isEnabled())));
Espresso allows you to use Hamcrest matchers in tests.
Espresso 允许您在测试中使用 Hamcrest 匹配器。
Please check also this (if you haven't done that already):
请同时检查这个(如果你还没有这样做):
Espresso 2.1. Espresso Cheat Sheet Master[updated]
According to this fragment of your post:
根据你帖子的这个片段:
When launching the test, nothing is reported, whereas
nextQuestionButton
should not be enabled upon clicking theradioButton
whose text is "wrong answer".
启动测试时,不报告任何内容,而
nextQuestionButton
不应在单击radioButton
文本为“错误答案”时启用。
It means that you hadn't set disabled your next question button, so Espresso passes this test.
这意味着你没有设置禁用你的下一个问题按钮,所以 Espresso 通过了这个测试。