Sam
Sam

Reputation: 1

How to test AlertDialog item click in Robotium for Android Studio

I have an AlertDialog as below, I don't know how to test it with Robotium in Android Studio. Can anyone give me a hint for that?

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

            alertDialogBuilder.setTitle("Select");
            final String[] items = {"Take a picture using carmera", "Choose a picture from Album"};
            alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (i == 0) {

... ...

Upvotes: 0

Views: 584

Answers (1)

piotrek1543
piotrek1543

Reputation: 19351

See this answer to a similar question:

This works for me:

solo.clickOnView(solo.getView(android.R.id.button1)); 

where the 'Positive' button is android.R.id.button1, the 'Negative' button is android.R.id.button2 and 'Neutral' is android.R.id.button3.

It means that for your AlertDialog you would need to use the solo.clickOnView(solo.getView(dialogId)) method.

Check out also this answer to a similar question:

lets say you have some code like this

solo.clickOnView(view1);

solo.clickOnView(view2);

and you know the dialog can appear between these two steps of your test, you can place in code that is something like:

if(solo.waitForView(dialogView, 1000, false)){
    solo.clickOnView(dialogDismissButton);
    solo.clickOnView(view2) //retry the step above
}

Upvotes: 2

Related Questions