我为应用程序编写自动测试。在应用程序启动时,它具有一个入职(ViewPager),其中包含一些页面,最后一页上的按钮。我需要滑动这些页面,然后按按钮。这是我的入门课程
public class Onboarding {
ViewInteraction onboardingScreen = onView(
allOf(withId(R.id.vp_onboarder_pager),
childAtPosition(
allOf(withId(R.id.cl_onboarder),
childAtPosition(
withId(android.R.id.content),
0)),
0),
isDisplayed()));
ViewInteraction skipButton = onView(allOf(withId(R.id.btn_finish)));
ViewAction skipOnboardingScreen = new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
ViewPager onboardingPager = (ViewPager) view;
OnboarderAdapter adapter = (OnboarderAdapter) onboardingPager.getAdapter();
int currentPage = 0;
int screenCount = adapter.getCount();
while (screenCount > 0){
onboardingPager.setCurrentItem(currentPage);
screenCount--;
currentPage++;
}
skipButton.perform(click());
}
};
public void skipOnboarding() {
onboardingScreen.perform(skipOnboardingScreen);
}
我以这种方式打电话给我的考试:
@Test
public void onboardingPass() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Onboarding onboarding = new Onboarding();
onboarding.skipOnboarding();
}
执行测试时,onboardingPager.setCurrentItem(currentPage)
必须切换当前页面,但不是。当我在调试器中看到它时,测试执行此行,但页面不会切换。为什么会这样呢?谢谢。
答案 0 :(得分:0)
...我需要刷一下这些页面...
要在仪器测试中滑动,请在视图中使用它;
onboardingScreen.perform(swipeRight());
swipeRight()来自AndroidX Test包androidx.test.espresso.action.ViewActions.swipeRight
详细了解AndroidX Test Espresso ViewActions软件包here。
根据您的要求设置滑动并查看是否有帮助。
答案 1 :(得分:0)
如果您尝试滚动到ViewPager
的最后一页,则应改用ViewPagerActions.scrollToLast。而且我敢肯定,它们都可以简化为:
public void skipOnboarding() {
onView(withId(R.id.vp_onboarder_pager)).perform(ViewPagerActions.scrollToLast());
onView(withId(R.id.btn_finish)).perform(click());
}