滑动ViewPager并截取屏幕截图

时间:2017-08-23 14:11:09

标签: android android-espresso

我希望能够测试我的viewpager,它具有动态(预先不知道页面数)的页面集,执行一些断言并能够截取屏幕截图。

由于viewpager具有动态的页面集,我无法提前告诉我swipeLeft()次。

所以,我写了一个自定义约束:

/**
 * Moves to the left by one page.
 */
public static ViewAction scrollLeftUntilEnd() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isDisplayingAtLeast(90);
        }
        @Override
        public String getDescription() {
            return "ViewPager scroll one page to the left";
        }
        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final ViewPager viewPager = (ViewPager) view;
            final int current = viewPager.getCurrentItem();

            final int size = viewPager.getAdapter().getCount();
            for (int i = 0; i < size; i++) {
                viewPager.setCurrentItem(i + 1, true);
                takeScreenshot("ViewPager_Cards");
            }
            uiController.loopMainThreadUntilIdle();
        }
    };
}

我称之为:

onView(withId(R.id.myViewPager)).perform(scrollLeftUntilEnd());

但页面只向左移动一次。

这是因为ViewAction返回的scrollLeftUntilEnd()只能执行一次而不是循环吗?

我无法使用repeatedlyUntil(swipeLeft(), ...);,因为我需要在每个页面截取屏幕截图。

2 个答案:

答案 0 :(得分:0)

可能是因为你有

viewPager.setCurrentItem(current + 1, true);

current永远不会改变。

应该是

viewPager.setCurrentItem(i + 1, true);

代替?

答案 1 :(得分:0)

我能够在Jing Li's方法之后绕过我的问题,这是片段:

  

公共类CountHelper {

private static int count;

public static int getCountFromListUsingTypeSafeMatcher(@IdRes int listViewId) {
    count = 0;

    Matcher matcher = new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View item) {
            count = ((ListView) item).getCount();
            return true;
        }

        @Override
        public void describeTo(Description description) {
        }
    };

    onView(withId(listViewId)).check(matches(matcher));

    int result = count;
    count = 0;
    return result;
}

public static int getCountFromListUsingBoundedMatcher(@IdRes int listViewId) {
    count = 0;

    Matcher<Object> matcher = new BoundedMatcher<Object, String>(String.class) {
        @Override
        protected boolean matchesSafely(String item) {
            count += 1;
            return true;
        }

        @Override
        public void describeTo(Description description) {
        }
    };

    try {
        // do a nonsense operation with no impact
        // because ViewMatchers would only start matching when action is performed on DataInteraction
        onData(matcher).inAdapterView(withId(listViewId)).perform(typeText(""));
    } catch (Exception e) {
    }

    int result = count;
    count = 0;
    return result;
}
     

}

我在课堂上这样称呼它:

final int count = getCountFromListUsingTypeSafeMatcher(R.id.myViewPager);
    for (int i = 1; i < count; i++) {
         onView(R.id.myViewPager).perform(swipeLeft());
         ...
         takeScreenshot("ViewPager_Cards");
    }