Tuesday, May 20, 2014

Longclick an object

Longclick an object

With the latest Android version of Kitkat the long click in uiautomator over an object does not function as expected. It works similar to a click thereby blocking a part of your automation.
This can be worked around by using the swipe functionality.

For instance, if you want to click on an object using its text attribute, you can follow the code snippet below to perform long click. Make sure the number of steps mentioned in the swipe() is high.

Code Snippet:

public static void longClick(String text) {
  UiObject object = new UiObject(new UiSelector().textContains(text));
  Rect coordinates = object.getBounds();
  mUiDevice.swipe(coordinates .centerX(), coordinates .centerY(), 
    coordinates .centerX(), coordinates .centerY(),
    100);
}

No comments:

Post a Comment