是否可以通过 Espresso 执行拖放操作?我需要向下移动一个视图(直线)以便在我的自动化测试中接受一些条件。
问问题
5305 次
2 回答
12
您可以使用 GeneralSwipeAction 执行拖放操作。
public static ViewAction swipeUp() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,
GeneralLocation.TOP_CENTER, Press.FINGER);
}
您也可以自定义位置以满足您的要求。
于 2016-02-11T05:38:23.667 回答
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();
}
于 2016-02-22T09:07:22.780 回答