java - 我重写的方法无法正常工作,代码进入方法,但不执行

标签 java android android-espresso android-testing

我为应用程序编写自动测试。在应用程序启动时,它有一个入门(ViewPager),其中有一些页面和最后一页上的按钮。我需要滑动这些页面并按下按钮。这是我的入职类(class)

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)必须切换当前页面,但事实并非如此。当我在调试器中看到它时,测试执行这一行,但页面没有切换。为什么可以呢?谢谢。

最佳答案

... I need to swipe these pages ...

要在仪器测试中滑动,请将其与您的 View 结合使用;

onboardingScreen.perform(swipeRight());

swipeRight() 来自AndroidX Test包 androidx.test.espresso.action.ViewActions.swipeRight

了解有关 AndroidX Test Espresso ViewActions 包的更多信息 here .

根据您的要求设置滑动,看看是否有帮助。

关于java - 我重写的方法无法正常工作,代码进入方法,但不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55260928/

相关文章:

java - 如何从与 Firebase 数据库同步的 ArrayList 中删除条目?

java - 以 Debug模式启动服务器在 eclipse 上不起作用

java - eclipse : key on Android AES encryption and decryption

安卓 Espresso Intent

android - Kotlin 和新的 ActivityTestRule : The @Rule must be public

java - 无法通过记录器解析 com.android.packageinstaller Espresso 生成的代码

java - android异步读取usb

java - 如何在 Swing 中逐渐旋转图像?

android - 绿道onUpdate。如何向旧表添加新列?

android - 由 : java. lang.SecurityException :android. permission.READ_PHONE_STATE 引起