Search code examples
androidrobotium

Robotium: Click on text in the alertDialog only, not in the underneath activity


I have an activity displaying some text, for example "someText".

From this activity, I open an alert dialog box as follow:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title");
String[] items = {"Hello", "World", "someText"};
builder.setItems(items, new MultiChoiceEventOnClickListener(...);
builder.setCancelable(true);
builder.show();

And here is my Robotium test:

solo.clickOnButton(...); // to open the dialog alert
solo.waitForDialogToOpen();
solo.clickOnText("someText");

The problem is that Robotium finds the text in the activity under the alert dialog. As "someText" can be anywhere in my activity, I cannot use an index.

How can I narrow the search of the text to the alertDialog only? or How can I find the view item in the list of items in the alert dialog?


Solution

  • It should be possible to search for the text manually like that:

    ArrayList<View> views = solo.getCurrentViews();
    for(View v : views) {
       if (!v instanceof TextView) {
          //filter out everything thats not a TextView
          continue;
       }
    
       String text = ((TextView)v).getText().toString();
       if (text.contains("sometext") {
          //We found the view, click and then exit the loop. 
          solo.clickOnView(v);
          break;
       }
    }
    

    Disclaimer: As I currently don't have an Android environment set up on my machine I couldn't verify it