拖拽滴咖啡

时间:2016-02-09 16:41:59

标签: android android-espresso android-instrumentation

是否可以执行拖动和放大通过Espresso放下动作?我需要向下移动一个视图(直线)以接受我的自动化测试中的某些条件。

2 个答案:

答案 0 :(得分:11)

您可以使用GeneralSwipeAction执行拖动和放大下降。

public static ViewAction swipeUp() {  
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,  
    GeneralLocation.TOP_CENTER, Press.FINGER);  
}

您也可以自定义位置以满足您的要求。

答案 1 :(得分:2)

多数民众赞成我是如何做到的。您可以更多地访问您的视图应该发生的事情。但接受的答案也会拖延下去。

  public static void drag(Instrumentation inst, float fromX, float toX, float fromY,
                            float toY, int stepCount) {
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis();

        float y = fromY;
        float x = fromX;

        float yStep = (toY - fromY) / stepCount;
        float xStep = (toX - fromX) / stepCount;

        MotionEvent event = MotionEvent.obtain(downTime, eventTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        inst.sendPointerSync(event);
        for (int i = 0; i < stepCount; ++i) {
            y += yStep;
            x += xStep;
            eventTime = SystemClock.uptimeMillis();
            event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
            inst.sendPointerSync(event);
        }

        eventTime = SystemClock.uptimeMillis();
        event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
        inst.sendPointerSync(event);
        inst.waitForIdleSync();
    }