Drag & Drop Espresso

Can I perform a drag and drop operation through Espresso? I need to move one view down (in a straight line) to accept some conditions in my automation test.

+7
android android-espresso android-instrumentation
source share
2 answers

You can use the GeneralSwipeAction command to perform a drag and drop.

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

You can also customize your location and fulfill your requirements.

+8
source share

Here is how I did it. You have more access to what should happen to your presentation. But the accepted answer does 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(); } 
+3
source share

All Articles