Drag & Drop Espresso

前端 未结 2 659
轮回少年
轮回少年 2021-01-04 13:50

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.

相关标签:
2条回答
  • 2021-01-04 14:19

    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();
        }
    
    0 讨论(0)
  • 2021-01-04 14:25

    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.

    0 讨论(0)
提交回复
热议问题