Espresso:如何进行自定义滑动,例如swipeTop或swipeBottom

问题描述 投票:10回答:5

到目前为止我们可以做到:

  • swipeLeft
  • swipeRight
  • swipeUp
  • 刷下

我们如何滑动To(一直到顶部)或滑动底部(一直到底部)是expresso。如果这些方法已经存在,请举个例子。

android android-espresso ui-testing
5个回答
15
投票

你试过这样的GeneralSwipeAction吗?

private static ViewAction swipeFromTopToBottom() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
            GeneralLocation.BOTTOM_CENTER, Press.FINGER);
}

也许您必须为第二个和/或第三个参数提供自定义实现,正如Anna已经提到的:

new CoordinatesProvider() {
    @Override
    public float[] calculateCoordinates(View view) {
        float[] coordinates =  GeneralLocation.CENTER.calculateCoordinates(view);
        coordinates[1] = 0;
        return coordinates;
    }
}

8
投票

例如,如果在您的应用程序中使用recyclerView,则可以使用以下内容:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())

要么

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeDown())

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);
}

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());
    }
}

我希望这可以帮到你。


1
投票

@testsingh我认为在循环之后没有开箱即用的方法来执行“swipeTop”或“swipeBottom”可能是对我来说最简单的方法(最愚蠢)XDDD

// swipeUp 100次,反之亦然swipeDown

for(int i=0;i<=100;i++){
  onView(withText("label")).perform(swipeUp());
}

1
投票

我知道我迟到了,但经过一段时间的努力,我终于发现了一些可以从上到下,从左到右等等 - 不需要任何资源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
}

我不需要从左到右等,所以我在那里硬编码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);
}

我希望这可以帮助任何绊倒这个的人。

© www.soinside.com 2019 - 2024. All rights reserved.