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

1

7

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?

Licentiate answered 14/5, 2019 at 19:21 Comment(0)
T
3

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

Tinstone answered 17/5, 2019 at 21:37 Comment(3)
Thanks for the tip. I expected something integrated in Robotium existed. Note: In your code proposal, I will also need to check that the parent is the AlertDialog boxLicentiate
I would expect that this is not necessary, according to the docs: getCurrentViews() Returns an ArrayList of the Views currently displayed in the focused Activity or Dialog.Tinstone
Maybe the doc is not correct. When I call getCurrentViews(), all the views of the AlertDialog box AND the activity under it are returned. Hence this post.Licentiate

© 2022 - 2024 — McMap. All rights reserved.