2

Pretty self-explanatory. Say I have a login screen and I have no idea what the developer (I'm QA) named the username and password text fields. Given that there are hundreds of such fields throughout our app, I don't want to bug him for every time I need this info.

Is there some way to programmatically list the labels or ids for all elements on an activity screen?

Thanks.

Aaron
  • 2,154
  • 5
  • 29
  • 42

3 Answers3

3

get views

Yes you can use solo.getCurrentViews() for that or any of the more specific methods. See from api:

http://www.jarvana.com/jarvana/view/com/jayway/android/robotium/robotium-solo/3.1/robotium-solo-3.1-javadoc.jar!/com/jayway/android/robotium/solo/Solo.html#getCurrentViews()

edit: found the latest docs: http://robotium.googlecode.com/svn/doc/index.html

getCurrentViews

public ArrayList getCurrentViews() Returns an ArrayList of the View objects currently shown in the focused Activity or Dialog. Returns: an ArrayList of the View objects currently shown in the focused window

iterate views (complete code example)

for( View v : solo.getCurrentViews()){
   // do something with the View object here 
   int viewId = v.getId();
}

http://developer.android.com/reference/android/view/View.html

hcpl
  • 17,382
  • 7
  • 72
  • 73
  • Why do you suggest version, which has 1,5 year? There is robotium 4.1 now: http://code.google.com/p/robotium/downloads/list – maszter May 29 '13 at 20:41
  • Naive question... how do I list them out? I tried this but it's no good: `ArrayList arr = solo.getCurrentViews(); ... System.out.println(arr.toString());` – Aaron May 29 '13 at 20:41
  • @maszter you have a point, I just googled "solo robotium api" to get the api in front of me so I could copy some link in here. Never noticed the version. Can 't find any online published api docs for latest version though. Feel free to edit my answer once you do. – hcpl May 29 '13 at 20:45
  • @AaronS you need to iterate the arraylist. See updated answer. – hcpl May 29 '13 at 20:46
  • @hcpl well, yeah, but what do I do with it? :-) So I do `for (View v : arr)` then what? I need to find the id or a label or SOMETHING to print out to help me be able to interact with the view... – Aaron May 29 '13 at 20:51
  • @hcpl Here's a concrete example of using a friendly label in my code: `CheckBox cb = (CheckBox) solo.getView(R.id.cb_termsandconditions);`. My question is how do I get that friendly id name? Or am I relegated to using integers to click/tap/etc. on elements? – Aaron May 29 '13 at 20:56
  • @AaronS check updated answer, you get a View object so you can fetch the Id or whatever you need from it. Doc is listed. – hcpl May 29 '13 at 20:56
  • int viewId = v.getId(); is useless, as it may be changed between runs – maszter May 29 '13 at 20:57
  • @AaronS I see where you're going, check the answer from on how to convert these IDs to the resource name. – hcpl May 29 '13 at 21:01
2

You can get all R.ids and texts of views from current Activity, for instance this way:

ArrayList<View> views = solo.getCurrentViews();
for (View v : views) {
    if (v.getId() != View.NO_ID) {
        String id = v.getResources().getResourceName(v.getId()).replaceAll(":id/", ".R.id.");
        Log.d("DEBUG", id);
    }
    if (v instanceof TextView) {
        String text = ((TextView)v).getText().toString();
        Log.d("DEBUG", text);
    }
}
maszter
  • 3,680
  • 6
  • 37
  • 53
0

uiautomatorviewer is the best to list all the id's and text which is present in the android screen.

1.Go to your android-sdk/tools directly and you can locate it there.

2.Just open the screen where the element is located and click on snapshot button in uiautomatorviewer. You can view all the ids of the elements in this particular screen.

PS. I will be updating the answer with screenshots later.