Execute command if view exists

771 views Asked by At

I'm using robotium to write tests for our android application. I'm looking to execute a command if a certain view or text exists. It would be ideal to check if the view/button exists, but I'm open to checking if the logout text exists as well

My (incorrect) pseudocode would be something like this:

if solo.getView("logoutButton")) //if the logout button exists
    solo.clickOnView(solo.getView("logoutButton")); //click it
end

I'm not familiar with robotium or android and would appreciate any insight.

1

There are 1 answers

6
Simas On BEST ANSWER

What about the good old findViewById:

View view = solo.getCurrentActivity().findViewById(R.id.logout_button);
Assert.assertNotNull(view);
solo.clickOnView(view);

Edit:

import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import com.robotium.solo.Solo;
import junit.framework.Assert;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;

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

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

    public void testView() throws Exception {
        View view = solo.getCurrentActivity().findViewById(R.id.tv);
        Assert.assertNotNull(view);
        solo.clickOnView(view);
    }

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

}