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

时间:2015-01-29 15:19:00

标签: android android-espresso ui-testing

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

  • swipeLeft
  • swipeRight
  • swipeUp
  • swipeDown

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

6 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:8)

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

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

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

答案 2 :(得分: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());
    }
}

我希望这可以帮到你。

答案 3 :(得分:1)

@testsingh 我认为没有开箱即用的方法来做&#34; swipeTop&#34;或者&#34; swipeBottom&#34; 循环可能是最简单的方法(最愚蠢),对我来说是XDDD

// swipeUp 100次,反之亦然swipeDown

function checkNested(obj /*, level1, level2, ... levelN*/) {
  var args = Array.prototype.slice.call(arguments, 1);

  for (var i = 0; i < args.length; i++) {
    if (!obj || !obj.hasOwnProperty(args[i])) {
      return false;
    }
    obj = obj[args[i]];
  }
  return true;
}

var test = {level1:{level2:{level3:'level3'}} };

checkNested(test, 'level1', 'level2', 'level3'); // true
checkNested(test, 'level1', 'level2', 'foo'); // false

答案 4 :(得分: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);
}

我希望这有助于任何绊倒这个的人。

答案 5 :(得分:1)

//向上滑动

for(int i=0;i<=3;i++){
    onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(4, swipeUp()));
}

//向下滑动

for(int i=0;i<=3;i++){
     onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(7, swipeDown()));
}