Robotium - 尝试在应用程序中单击主页按钮

时间:2013-12-18 14:50:21

标签: java android junit robotium

我是robotium的新手,我正在尝试编写一个快速而又脏的脚本来运行应用中的所有屏幕。

问题我主要是应用程序中的“主页按钮”。我已经尝试了很多选项,但我似乎无法点击那里除了索引,这不是我想要的。

当我查看带有hierarchyviewer的按钮时,它看起来像这样:

Link

然而,当我尝试例如:

assertTrue(
"Wait for text (id: myapp.R.id.home) failed.",
solo.waitForImageById("myapp.R.id.home", 20000));
solo.clickOnImage((ImageView) solo.findViewById("myapp.R.id.home"));            
solo.waitForActivity("MenuActivity");

waitForImageByID行失败。我尝试了多个选项,如waitForImageButton等,但我似乎无法点击它。我在这里缺少什么?

junit.framework.AssertionFailedError: View with id: '0' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1990)
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1970)
at com.bitbar.recorder.extensions.OtherUtils.a(OtherUtils.java:246)
at com.bitbar.recorder.extensions.OtherUtils.b(OtherUtils.java:241)
at com.bitbar.recorder.extensions.v.a(Waiter.java:71)
at com.bitbar.recorder.extensions.ExtSolo.waitForImageButtonById(ExtSolo.java:4176)
at com.example.android.apis.test.Test.testRecorded(Test.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)

6 个答案:

答案 0 :(得分:4)

使用以下行按操作栏中的主页按钮

solo.clickOnActionBarHomeButton();

答案 1 :(得分:1)

问题是它所引用的id不在你的应用程序中,它在orroids默认的R文件中,尝试android.R.id.home并且应该可以正常工作。值得注意的是,如果您的应用程序使用操作栏sherlock来支持4.0之前的操作栏,那么它将具有不同的ID,您将不得不在测试中处理此问题。

您可以自己查看:http://developer.android.com/reference/android/R.id.html

答案 2 :(得分:1)

当您使用ActionBarSherlock时,您需要检查两个不同的ID,android.R.id.home用于API级别> 11和abs__home用于较低级别(由ActionBarSherlock提供):

    View homeButton = activity.findViewById(android.R.id.home);
    if (homeButton == null) {
        homeButton = activity.findViewById(R.id.abs__home);
    }

答案 3 :(得分:0)

这段代码怎么样:

ArrayList<LinearLayout> ll = solo.getCurrentViews(LinearLayout.class);
//You can change 1 with the ordinal number of LinearLayout you want to click.
solo.clickOnView(ll.get(1));

ArrayList<ImageView> iv = solo.getCurrentViews(ImageView.class);
//You can change 0 with the ordinal number of Image you want to click.
solo.clickOnView(iv.get(0));

我认为如果您确定视图或线性布局或图像视图的正确ID,它应该可以工作。

答案 4 :(得分:0)

Dave C的回答只对我有所帮助。单击该按钮但在加载前一个屏幕之前断言已经开始,因此总是错误的。解决方案是在主线程(Robotium 5.2.1)上运行“home click”:

getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            solo.clickOnActionBarHomeButton();
        }
});   

答案 5 :(得分:0)

从你的问题我可以看到它是一个图像视图。您可以使用以下代码单击任何视图。

View view = solo.getView("View_name_from_hierachy_viewer");
solo.clickOnView(view);
在您的情况下,

View_name_from_hierachy_viewer将是&#34; home&#34;。  如果这不起作用,请告诉我。