Search code examples
javaandroidandroid-espressoautomatorandroid-uiautomator

UI automator / Has somebody faced problems with UI automator? (impossible to click on element)


Prehistory of my question:

I'm studying UI automator (2.1.3) & Espresso tests + using android studio (3.0.1) on simple Mac Air (2014). Previously I have created functional suite for test-run and it works fine for a long period of time. But last week I have noticed strange behavior related to UI automator actions: - I can't perform single click on elements in my current project nor on android elements. For example quite long period I have used click on element by ID in my project, but now these methods are non-functional. Test has passed, but there is no result on screen and in app state, here is simple example:

 UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    String contentOfMyElement = "";
    UiObject myObject = mDevice.findObject(new UiSelector().text(getInstrumentation().getTargetContext()
        .getString((R.id.idOfMyElement))).text(contentOfMyElement)
                    .clickable(true).checkable(false));
    mObject.click();

All objects of such structure suddenly became non-functional, tests are pass through and successful but nothing is going with app state. Such structure has been functional for a long time in my project, but now all tests with it non-functional.

I'm trying to resolve it in a next way:

  • first of all I though that something is wrong with mobile device so I have started several tests (with UI automator actions) on different devices (virtual and physical)
  • clear android studio cache with restart
  • sync gradle (which I haven't changed previously for a long time - 3 month or above) + clear my project + re-build it
  • use previous version of UI automator in gradle dependencies
  • re-install android studio

ALL of these items couldn't resolve issue which I have faced. ADDITIONALLY I have tried above example in another android project and it non-functional too.

As I can see problems somewhere in UI automator or in the latest android studio 3.0.1 All I know for now - UI automator performing mDevice.pressHome(), back etc., but do not perform any actions on specific elements of my app.

Does anybody know how to fix UI automator behavior?


Solution

  • You can debug in android studio, can you get the correct "myObject"? "myObject" will contain the bounds & locations of the element, "click()" action will perform one clicking at center location.

    In the other hand, you are using uiautomator 2.0, you should ues UiObject2 instead of UiObject.

    BySelector selector = By.res("resourceID");
    UiObject2 obj = mDevice.findObject(selector);