Francisco
Francisco

Reputation: 21

Is it posible to identify a clicked view and its index number from outside the application?

I've been learning Android for a couple of months, specially different ways to test Android applications. So far I've worked with JUnit and Robotium. I've learned to use Robotium pretty well, even for testing blackbox apps, and I think it's really cool. But while testing blackbox apps with Robotium the only way to click a button is to use its label, coordinates or index. The thing is, if the button is an image button then I can not use its label, and I'm afraid that using coordinates won't allow me to run my tests on different devices due to the different screen sizes, so the views may be placed in different position when they are scaled. So, this leaves me with the index only (unless...there is another way I may not know yet?), but the issue is that there is no way for me to know a view's index, so far the only way I've figured to get a button's index is by writing a test telling Robotium to click on button with index 0, and then 1, and then I can identify the pattern and count by my self until I get the index of the button I want.

But, since this is quite cumbersome (especially if I have several buttons), I am trying to build a tool that allows me to identify a view and its index when it is clicked, for example lets say I am testing the music player and I want to tell Robotium to click on the "play" button, since it is an image button I will send the index number as a parameter to the clickOnImageButton(int index), but I don't know its index, so I would click on it and this tool will print something like "You clicked ImageButton with index 3", if possible as a toast or in the console.

Now, here comes my question, is there any way to do this?, I was trying to create a service an use it to capture click events but then I read this post: Android detecting the touch state from any application, so it seems that for security reasons it is not possible, as I believe that I certainly need to capture click events from other apps and its view elements. I tried to see if logcat provides this info but it doesn't, finally I am trying to use getevent from adb but its output is hard to understand, and it seems that the output of the getevent are coordinates too, is there any way to get the view and its index based on its coordinates?. I been trying to find an explanation on how monkey works too in case there is something that might help there, but all the documentation I find relates to how to use it and not how it works. I wonder if any of you know how Testdroid or any of the record/replay tools manage to capture these clicks and reproduce them on different devices.

Upvotes: 2

Views: 2833

Answers (4)

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22647

But while testing blackbox apps with Robotium the only way to click a button is to use its label, coordinates or index

You can click on a widget that contains a particular string of text, see Solo.clickOnText(String), http://www.jarvana.com/jarvana/view/com/jayway/android/robotium/robotium-solo/1.0.1/robotium-solo-1.0.1-javadoc.jar!/index.html

Upvotes: 1

hari_nars1
hari_nars1

Reputation: 49

Two solutions:

  1. ImageButton imageButton = (ImageButton)activity.findviewbyid(R.id.x); Touchutils.clickview(this,imageButton); where x is id of view, that you can get from the source code.

  2. If you are not able to see the code, then use following one with the solo solo.clickonbutton(int index);, where index is the view number

Upvotes: 0

Kristopher Micinski
Kristopher Micinski

Reputation: 7682

I frequently run up against this when I do black box testing. The thing to keep in mind is that Robotium has a nice clickOnView function that allows you to click on pretty much anything: so the question now becomes how to uniquely identify views (as you can simply get them and then click on them).

I use a pretty hacky (but simple) solution to this when I write code with Robotium: I simply add a method that allows you to click on an arbitrary index by doing a getViews() followed by a clickView() selecting the appropriate view index in the resulting ArrayList.

I'm surprised that this doesn't exist in Robotium already, and I'll make a push request to Renas soon.

Upvotes: 0

Renas
Renas

Reputation: 1919

You can also use Robotium with resource ids. An example is:

ImageButton imageButton = (ImageButton) solo.getView(R.id.x);

solo.clickOnView(imageButton);

Please see the javadoc for more white box test methods:

http://code.google.com/p/robotium/downloads/list`

Upvotes: 2

Related Questions