I would like to perform long click on specified point with specified time. Unfortunately there is no method like long click in class: UiDevice I probably could write own method, something like this:
private void longClick(int x, int y, long time) {
android.graphics.Point point = new android.graphics.Point(x, y);
android.graphics.Point[] points = new android.graphics.Point[2];
points[0] = point;
points[1] = point;
getUiDevice().swipe(points, time / 5); // according to documentation, each step lasts 5ms
}
or use reflection and invoke longTap:
private void longClick(int x, int y) {
Field mUiAutomationBridgeField = getUiDevice().getClass().getDeclaredField("mUiAutomationBridge");
mUiAutomationBridgeField.setAccessible(true);
Object mUiAutomationBridge = mUiAutomationBridgeField.get(getUiDevice());
Field mInteractionControllerField = mUiAutomationBridge.getClass().getDeclaredField("mInteractionController");
mInteractionControllerField.setAccessible(true);
Object mInteractionController = mInteractionControllerField.get(mUiAutomationBridge);
Method longTap = mInteractionController.getClass().getDeclaredMethod("longTap", int.class, int.class);
longTap.setAccessible(true);
longTap.invoke(mInteractionController, x, y);
}
However it's not satisfied solution, any idea how to do it better way? Why do they miss such method?
getUiDevice().getInstance().swipe(x, y, x, y, 400);
Start point and end point are same. Then you can simulate uiDevice longclick.
I think swipe a UiObject is better.
yourUiObject.swipeRight(int steps);
yourUiObject.swipeLeft(int steps);
yourUiObject.swipeDown(int steps);
yourUiObject.swipeUp(int steps);
Anyone of them works great for me. Notice that the doc says one step takes about 5ms, but I found it's not true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With