2
votes

I have a simple test project for this problem and I cannot understand how it will fail.

The project under test adds only a button to the layout for the purpose of testing.

<Button 
    android:id="@+id/test_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click" />

Here's the test class:

public class TestMainActivity extends ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;

    public TestMainActivity() {
        super(MainActivity.class);
    }

    public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void test1() {
        solo.clickOnButton(com.example.testrobotium.R.id.test_button);
    }

    public void test2() {
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
}

When executing this test class, the error trace returned is as below, pointing at the call to clickOnButton:

junit.framework.AssertionFailedError: 2131165185 Buttons are not found! at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:417) at com.jayway.android.robotium.solo.Clicker.clickOn(Clicker.java:374) at com.jayway.android.robotium.solo.Solo.clickOnButton(Solo.java:1052) at com.example.testrobotium.test.TestMainActivity.test1(TestMainActivity.java:22) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

Am I missing something in my test class that's causing this problem?

1

1 Answers

5
votes

The issue is robotium does not work how you have used it!

solo.clickOnButton() has two implementations, one that has the text in the button as an argument and another that takes an index. You are passing the ID of the button which happens to be an int value but the int value is not the index.

You want your code to instead be the following:

solo.clickOnView(solo.getView(com.example.testrobotium.R.id.test_button));

If you need more explanation I will be happy to help.