tes*_*ngh 14 android ui-testing android-espresso
到目前为止我们可以做到:
我们如何swipeTop(一直到顶部)或swipeBottom(一直到底部)是expresso.如果这些方法已经存在,请举个例子.
Ale*_*cha 19
你试过GeneralSwipeAction这样的吗?
private static ViewAction swipeFromTopToBottom() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
GeneralLocation.BOTTOM_CENTER, Press.FINGER);
}
Run Code Online (Sandbox Code Playgroud)
也许您必须为第二个和/或第三个参数提供自定义实现,正如Anna已经提到的:
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = 0;
return coordinates;
}
}
Run Code Online (Sandbox Code Playgroud)
例如,如果在您的应用程序中使用recyclerView,则可以使用以下内容:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
Run Code Online (Sandbox Code Playgroud)
要么
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeDown())
Run Code Online (Sandbox Code Playgroud)
小智 5
@testsingh我认为没有现成的方法可以在循环后执行“swipeTop”或“swipeBottom”可能是对我有用的最简单的方法(最愚蠢的)XDDD
//向上滑动100次,向下滑动100次
for(int i=0;i<=100;i++){
onView(withText("label")).perform(swipeUp());
}
Run Code Online (Sandbox Code Playgroud)
我知道我参加聚会迟到了,但经过一番折腾,我终于找到了可以从上到下、从左到右等滑动的东西——不需要任何资源 ID。
我需要它的原因是动态填充的视图,其中所有内容都完全不明确。使用下面的方法,我可以一直滚动到底部,甚至可以更改延迟以仅向下滚动一页。
static void swiper(int start, int end, int delay) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
Instrumentation inst = getInstrumentation();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 500, start, 0);
inst.sendPointerSync(event);
eventTime = SystemClock.uptimeMillis() + delay;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 500, end, 0);
inst.sendPointerSync(event);
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 500, end, 0);
inst.sendPointerSync(event);
SystemClock.sleep(2000); //The wait is important to scroll
}
Run Code Online (Sandbox Code Playgroud)
我不需要从左到右等,所以我在那里硬编码了 500(500 是 x 轴)。
并打电话给他们,我做到了,我做到了-
// This swipes all the way to the bottom of the screen
public static void swipeToBottom(){
swiper(1000, 100, 0);
}
// This scrolls down one page at a time
public static void scrollSlowlyDown(){
swiper(775, 100, 100);
}
// This swipes to the top
public static void swipeToTop(){
swiper(100, 1000, 0);
}
// This scrolls up one page at a time
public static void scrollSlowlyUp(){
swiper(100, 775, 100);
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助任何遇到这个问题的人。
小智 4
我认为这可以通过执行滑动来编写复杂的内容来完成ViewActions。
public static ViewAction swipeToTop() {
return new MySwipeAction(Swipe.FAST,
GeneralLocation.CENTER,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = 0;
return coordinates;
}
}, Press.FINGER);
}
public static ViewAction swipeToBottom() {
return new MySwipeAction(Swipe.FAST,
GeneralLocation.CENTER,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = view.getContext().getResources().getDisplayMetrics().heightPixels;
return coordinates;
}
}, Press.FINGER);
}
Run Code Online (Sandbox Code Playgroud)
其中 MySwipeAction 可能是这样的:
public class MySwipeAction implements ViewAction {
public MySwipeAction(Swiper swiper, CoordinatesProvider startCoordProvide, CoordinatesProvider endCoordProvide, PrecisionDescriber precDesc) {
// store here in class variables to use in perform
...
}
@Override public Matcher<View> getConstraints() {...}
@Override public String getDescription() {...}
@Override
public void perform(UiController uiController, View view) {
...
float[] startCoord = startCoordProvide.calculateCoordinates(view);
float[] finalCoord = endCoordProvide.calculateCoordinates(view);
float[] precision = precDesc.describePrecision();
Swiper.Status status = Swiper.Status.FAILURE;
// you could try this for several times until Swiper.Status is achieved or try count is reached
try {
status = m_swiper.sendSwipe(uiController, startCoord, finalCoord, precision);
} catch (RuntimeException re) {
...
}
// ensures that the swipe has been run.
uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration());
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你。
| 归档时间: |
|
| 查看次数: |
10882 次 |
| 最近记录: |