【发布时间】:2016-04-09 18:48:23
【问题描述】:
是否可以通过 Espresso 执行拖放操作?为了接受自动化测试中的某些条件,我需要向下移动一个视图(沿直线)。
【问题讨论】:
标签: android android-espresso android-instrumentation
是否可以通过 Espresso 执行拖放操作?为了接受自动化测试中的某些条件,我需要向下移动一个视图(沿直线)。
【问题讨论】:
标签: android android-espresso android-instrumentation
您可以使用 GeneralSwipeAction 执行拖放操作。
public static ViewAction swipeUp() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,
GeneralLocation.TOP_CENTER, Press.FINGER);
}
您也可以自定义位置以满足您的要求。
【讨论】:
我就是这样做的。您可以更多地访问这样的视图应该发生的事情。但接受的答案也执行拖放操作。
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();
}
【讨论】: