Android Espresso如何执行触摸事件
我如何用简单的触摸事件来测试视图,例如Android Espresso如何执行触摸事件,android,android-testing,android-espresso,android-touch-event,Android,Android Testing,Android Espresso,Android Touch Event,我如何用简单的触摸事件来测试视图,例如动作向下和动作移动?没有像移动(Gravity.LEFT,40)方法这样的意式咖啡匹配方法 对于某些目的,如ACTION\u DOWN您可以使用类似swipeDown()的滑动方法 检查Espresso.ViewActions参考: 对于某些情况,您需要修改现有方法,如下所示: 还可以尝试将Espresso与其他UI instrumentation框架(如Robotium)混合使用,后者已经有了一些很棒的匹配器和功能,而Espresso没有这些功能,比如to
动作向下
和动作移动
?没有像移动(Gravity.LEFT,40)
方法这样的意式咖啡匹配方法
对于某些目的,如ACTION\u DOWN
您可以使用类似swipeDown()的滑动方法
检查Espresso.ViewActions参考:
对于某些情况,您需要修改现有方法,如下所示:
还可以尝试将Espresso
与其他UI instrumentation框架(如Robotium
)混合使用,后者已经有了一些很棒的匹配器和功能,而Espresso没有这些功能,比如touch()
方法,或者尝试另一个名为uiatomator
的谷歌框架
检查:
希望它能帮助您轻松发送触摸事件。使用此视图操作:
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));