0
我正在使用Espresso來測試我的應用程序,我需要在RecyclerView中測試拖放行爲。不幸的是,我在Espresso中看不到任何drag()動作,我錯過了什麼?Espresso中的drag()的等價物?
我正在使用Espresso來測試我的應用程序,我需要在RecyclerView中測試拖放行爲。不幸的是,我在Espresso中看不到任何drag()動作,我錯過了什麼?Espresso中的drag()的等價物?
我還沒有找到任何Espresso
拖動選項。只有swipeUp()
,swipeDown()
,swipeLwft()
,swipeRight()
,這在這種情況下可能沒有用。
此外,在谷歌羣組是一個沒有答案的問題:https://groups.google.com/forum/#!msg/android-test-kit-discuss/X3TDJBUT4Ho/OJAOkTOoxGcJ
雖然,也有Espresso MotionEvents
類sendMovement()
的方法,但我還沒有嘗試過。
使用下面的代碼:
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();
}
也瞭解TouchUtils
(已經從API 24不建議使用):https://developer.android.com/reference/android/test/TouchUtils.html
希望這將有助於
謝謝你,我想實現這個與MotionEvents,目前沒有運氣,但會讓你張貼! –
很好笑,我的回答是有用的 – piotrek1543
我沒有成功與MotionEvents :(使用您的代碼來獲取MotionEvent並將它們應用到UiController以便使用自定義ViewAction中的代碼。需要添加睡眠(1000)在下跌之後,以及我的拖動n跌落被長按觸發。 –