简体   繁体   中英

How to compare UiObject in Ui Automator

I have two different (or not -.-) UiObjects and I want to compare them.

I can see if they are of the same class and that sort of thing but it isn't enough.

I thought of using resource id but I can't find a way to access it (I want to do it in run time so ui automator viewer is of no help...).

So I'm actually asking two things:

  1. How should I compare two UiObjects (if not 100% positive, near it)?
  2. Is there a way to access the resource id?

Thanks in advance.

EDIT: My idea is to:

  • open the app
  • identify its layout and widgets
  • detect what interactions are possibles with the different widgets
  • choose an widget and interact with it
  • verify if its a new screen or changes on widgets

Currently, I am able to access the root of the layout tree and, thus, its children (as UiObjects) by choosing the first instance of FrameLayout (true for every screen)

Code Sample:

//I get the first object of the current layout screen by
UiObject obj = new UiObject(new UiSelector().className("android.widget.FrameLayout").instance(0));
/*
    code to get rest of the tree ...
*/
/*
    code interact with a button
*/
// now I want to see if the screen changed (completely new or if a new widget appeared) or if its the same
// for that I need to know if the objects are the same
// To compare objects I need their id

The reason I hadn't written code is because I don't think it really adds much... As the bit that matters is the one I don't know how to do

Well, I figured out this much:

  • When using UI Automator it isn't associated with any Application and so it isn't possible to obtain the Views of the application
  • There is no way to access the id of the View corresponding to the Ui Object (same reason as before).
  • The comparison must be made through heuristics without ever being 100% positive.

In summary, what I wanted is indeed impossible.

View class has the "getId" method, you can use it to compare views. For instance, if your class implements OnClickListener, then you can check which view raised the event like this:

public void onClick (View view) {
    switch(view.getId()) {
        case R.id.my_button_1:
            // What to do for button 1.
            break;
        case R.id.my_button_2:
            // What to do for button 2.
            break;
        default:
            // Unknown view
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM