如何使用简单的触摸事件(例如ACTION_DOWN
和)测试视图ACTION_MOVE
?
问问题
6008 次
2 回答
9
您可以轻松发送触摸事件。使用此视图操作:
public static ViewAction touchDownAndUp(final float x, final float y) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return "Send touch events.";
}
@Override
public void perform(UiController uiController, final View view) {
// Get view absolute position
int[] location = new int[2];
view.getLocationOnScreen(location);
// Offset coordinates by view position
float[] coordinates = new float[] { x + location[0], y + location[1] };
float[] precision = new float[] { 1f, 1f };
// Send down event, pause, and send up
MotionEvent down = MotionEvents.sendDown(uiController, coordinates, precision).down;
uiController.loopMainThreadForAtLeast(200);
MotionEvents.sendUp(uiController, down, coordinates);
}
};
}
并调用它:
onView(withId(R.id.my_view)).perform(touchDownAndUp(x, y));
于 2017-02-20T16:09:07.873 回答
0
没有直接的方法没有像move(Gravity.LEFT, 40)
方法这样的 Espresso 匹配器。
对于某些目的,例如ACTION_DOWN
您可以使用滑动方法,例如swipeDown()
检查 Espresso.ViewActions 参考:https ://developer.android.com/reference/android/support/test/espresso/action/ViewActions.html
对于某些人,您需要修改现有方法,例如:Drag & Drop Espresso
还可以尝试Espresso
与其他 UI 工具框架混合,例如Robotium
已经有一些很棒的匹配器和功能,而 Espresso 没有类似touch()
的方法,或者尝试另一个名为uiatomator
.
检查:http: //qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html
希望它会有所帮助
于 2016-09-11T21:49:22.593 回答