【问题标题】:Android UIAutomator long click on deviceAndroid UIAutomator 长按设备
【发布时间】:2013-04-17 13:32:32
【问题描述】:

我想在指定时间长按指定点。不幸的是,在课堂上没有像长按这样的方法:UiDevice 我可能可以编写自己的方法,如下所示:

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
}

或使用反射并调用 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);
}

但是它不满意的解决方案,知道如何做得更好吗?他们为什么会错过这种方法?

【问题讨论】:

  • 查看以下链接stackoverflow.com/questions/19156959/…>?
  • 请通过以下链接stackoverflow.com/questions/19156959/…>?``

标签: java android testing android-uiautomator


【解决方案1】:
getUiDevice().getInstance().swipe(x, y, x, y, 400);

起点和终点相同。 然后就可以模拟uiDevice长按了。

【讨论】:

    【解决方案2】:

    我认为滑动 UiObject 更好。

    yourUiObject.swipeRight(int steps);
    yourUiObject.swipeLeft(int steps);
    yourUiObject.swipeDown(int steps);
    yourUiObject.swipeUp(int steps);
    

    他们中的任何一个都适合我。 请注意,文档说一步大约需要 5ms,但我发现这不是真的。

    【讨论】:

    • 我没有任何对象。我只想长按 x,y
    • 当我在 Android 4.3 上尝试时,此滑动调用有效。希望 UiObject.longClick() 实际上会长按
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多