clickOnButton 在 Robotium 中不起作用
我有一个非常简单的测试:单击一个按钮,然后在 TextView 中显示一条消息,显示单击该按钮的次数。测试看起来像这样:
@Smoke
public void testMessageIsUpdatedWhenButtonIsClicked() {
_solo.clickOnButton(0);
String displayedMessage = ((TextView) _solo.getView(R.id.messageTextView)).getText().toString();
assertEquals("You clicked 1 times.", displayedMessage);
}
现在,当我运行测试时,断言失败,因为 TextView 仍在显示 Activity 启动时的原始文本。似乎我为 Button 创建的 OnClickListener 没有被调用。
还有其他人看到这个问题吗?
有什么办法可以解决这个问题吗?
I have a very simple test: click on a Button, then display a message in a TextView showing the number of times that the button was clicked. The test looks something like this:
@Smoke
public void testMessageIsUpdatedWhenButtonIsClicked() {
_solo.clickOnButton(0);
String displayedMessage = ((TextView) _solo.getView(R.id.messageTextView)).getText().toString();
assertEquals("You clicked 1 times.", displayedMessage);
}
Now, when I run my test, the assert fails because the TextView is still displaying the original text from when the Activity started up. It seems like the OnClickListener I created for the Button isn't getting called.
Has anybody else seen this problem?
Anything a guy can do to get around the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有类似的问题。就我而言,问题出在键盘上!因为键盘已打开,所以 Robotium 看不到按钮:“确定”和“取消”。解决办法:隐藏键盘,然后按按钮。
I was having a similar problem. In my case the problem was the keyboard! Because the keyboard was opened, robotium don't see the buttons: "OK" and "Cancel" . The solution: hide the keyboard and then press the button.
我知道这已经很旧了,但我想发帖,因为它让我很头痛。
汤姆,你在评论中提到:
解决方案实际上是将:
添加到您正在测试的应用程序而不是实际的测试应用程序。这一项改变对我来说很有效。现在我可以像预期一样使用solo.clickButton("button text");
而不必使用解决方法。希望有帮助
I know this is old, but I wanted to post because it has caused me lots of headache.
Tom, you mentioned in your comments:
The solution actually is to add:
<uses-sdk android:targetSdkVersion="10" />
to the app you are testing NOT the actual test app. This one single change works for me. Now I can usesolo.clickButton("button text");
just like intended and not have to use a workaround.Hope it helps
我也有类似的问题。有一个按钮,上面写着“游戏”,我想在我的测试用例中单击它。经过大量的“独奏”实验。我发现下面的代码可以工作:
我认为在我的例子中的问题是按钮在初始化并分配 onClickListener 之前就被点击了。
I was having a similar problem. There was a button with the text "Games" on it, and I wanted to click it in my test case. After a lot of experimentation with the "solo". I found the below code to work:
The problem I think in my case was that the button was getting clicked even before it was initialised and assigned the onClickListener.
我经常发现使用
按钮而不是按钮似乎是更好的方法。 Robotium 有时似乎在点击按钮时出现问题。如果您更喜欢使用按钮 ID,那么我建议如下:
I often find that using
rather than on the button seems to be a better approach. Robotium sometimes seems to have problems with clicking on buttons. If you prefer to use the button ID then I suggest the following:
当您使用solo 单击按钮时。使用基于索引的点击而不是名称或搜索按钮。索引从0开始。
solo.clickOnButton(1);
单独点击与索引配合效果最佳
When you click on a button using solo. Use index based clicks instead of names or searching for a button. Index starts with 0.
solo.clickOnButton(1);
solo clicks works best with indexes
我有类似的问题。不幸的是,我无法强制独奏正确单击按钮。然而使用 adroid.test.TouchUtils 解决了我的问题:
希望它有帮助。
I had a similar problem. Unfortunately I was not able to force solo to click on button properly. However using adroid.test.TouchUtils solved my problems:
Hope it helps.