clickOnButton 在 Robotium 中不起作用

发布于 2024-11-05 19:19:03 字数 491 浏览 5 评论 0原文

我有一个非常简单的测试:单击一个按钮,然后在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄居人 2024-11-12 19:19:03

我也有类似的问题。就我而言,问题出在键盘上!因为键盘已打开,所以 Robotium 看不到按钮:“确定”和“取消”。解决办法:隐藏键盘,然后按按钮。

solo.enterText(0, "Hello!");
        solo.goBack(); //hide the keyboard
        solo.clickOnButton("\u041E\u041A");

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.enterText(0, "Hello!");
        solo.goBack(); //hide the keyboard
        solo.clickOnButton("\u041E\u041A");
梨涡 2024-11-12 19:19:03

我知道这已经很旧了,但我想发帖,因为它让我很头痛。

汤姆,你在评论中提到:

我将以下内容添加到测试项目的 AndroidManifest.xml 中: 然后,我将其添加到我的 AndroidManifest.xml 中application: 仍然没有运气。我应该提交错误报告吗?

解决方案实际上是将: 添加到您正在测试的应用程序而不是实际的测试应用程序。这一项改变对我来说很有效。现在我可以像预期一样使用 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:

I added the following to my AndroidManifest.xml on the test project: <uses-sdk android:targetSdkVersion="10" /> I then added this to the AndroidManifest.xml of my application: <supports-screens android:anyDensity="true"/> Still no luck. Should I file a bug report?

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 use solo.clickButton("button text"); just like intended and not have to use a workaround.

Hope it helps

蔚蓝源自深海 2024-11-12 19:19:03

我也有类似的问题。有一个按钮,上面写着“游戏”,我想在我的测试用例中单击它。经过大量的“独奏”实验。我发现下面的代码可以工作:

solo.waitForDialogToClose(10000);
solo.clickOnButton("Games");

我认为在我的例子中的问题是按钮在初始化并分配 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:

solo.waitForDialogToClose(10000);
solo.clickOnButton("Games");

The problem I think in my case was that the button was getting clicked even before it was initialised and assigned the onClickListener.

我早已燃尽 2024-11-12 19:19:03

我经常发现使用

solo.clickOnText("buttonName"); 

按钮而不是按钮似乎是更好的方法。 Robotium 有时似乎在点击按钮时出现问题。如果您更喜欢使用按钮 ID,那么我建议如下:

solo.waitForIdleSync();
solo.clickOnButton(R.id.whatever);

I often find that using

solo.clickOnText("buttonName"); 

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.waitForIdleSync();
solo.clickOnButton(R.id.whatever);
心如狂蝶 2024-11-12 19:19:03

当您使用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

九厘米的零° 2024-11-12 19:19:03

我有类似的问题。不幸的是,我无法强制独奏正确单击按钮。然而使用 adroid.test.TouchUtils 解决了我的问题:

Button button = solo.getButton( 0 );
TouchUtils.clickView( this, button );

希望它有帮助。

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:

Button button = solo.getButton( 0 );
TouchUtils.clickView( this, button );

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文