Drag & Drop Espresso

人走茶凉 提交于 2019-11-30 03:15:05

问题


Is it possible to perform drag & drop action via Espresso? I need to move one view down (in straight line) in order to accept some conditions in my automation test.


回答1:


You can use GeneralSwipeAction to perform drag & drop.

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

You can customize the location to meet your requirement as well.




回答2:


Thats how I have done it. You have more access to what should happen with your view like that. But accepted answer perform drag&drop too.

  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();
    }


来源:https://stackoverflow.com/questions/35297442/drag-drop-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!