如何进行Espresso测试,直到页面加载完毕

问题描述 投票:0回答:2

我将登录。之后,显示卡片清单的请求。每次此请求在不同的时间发生,并且使用sleep()并不是很好。只要页面正在加载,如何使测试等待?最好带有示例代码。

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AuthorizationTest {

private IdlingResource mIdlingResource;

@Rule
public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);

@Before
public void SetUp() throws Exception {
        Thread.sleep(4000);
    try {
        onView(allOf(withId(R.id.tiAuthLogin), (isDisplayed()))).check(matches(isDisplayed()));
    } catch (NoMatchingViewException e) {
        pressBack();
    }
}


@Test
public void checkMoneyBackButton() throws Exception {
    onView(withId(R.id.btnAuthLogin)).perform(click());

    Thread.sleep(10000);
    onView(withId(R.id.etSessionKey1)).perform(typeText("1234"));
    closeSoftKeyboard();

    onView(isRoot()).perform(waitFor(40000));


    ViewInteraction viewInteraction = Espresso.onView(withText("**** 0431"));
    viewInteraction.perform(click());

    Thread.sleep(3000);

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

    onView(withId(R.id.statementMoneyBack)).check(matches(isDisplayed()));
}

}

testing automated-tests android-espresso
2个回答
0
投票

您可以创建自定义操作以等待

请检查此链接:

Android Espresso wait for text to appear


-1
投票

我不知道为什么,但是对我不起作用

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AuthorizationTest {


    @Rule
    public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);

    @Before
    public void SetUp() throws Exception {
        Thread.sleep(4000);
    try {
        onView(allOf(withId(R.id.tiAuthLogin), (isDisplayed()))).check(matches(isDisplayed()));
    } catch (NoMatchingViewException e) {
        pressBack();
    }
}

public static ViewAction waitId(final int viewId, final long millis) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + millis;
            final Matcher<View> viewMatcher = withId(viewId);

            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        return;
                    }
                }

                uiController.loopMainThreadForAtLeast(50);
            }
            while (System.currentTimeMillis() < endTime);

            // timeout happens
            throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(new TimeoutException())
                    .build();
        }
    };
}

@Test
public void checkMoneyBackButton() throws Exception {

    onView(withId(R.id.btnAuthLogin)).perform(click());

    Thread.sleep(10000);
    onView(withId(R.id.etSessionKey1)).perform(typeText("1234"));
    closeSoftKeyboard();

    onView(isRoot()).perform(waitId(R.id.swipeRefreshLayout, 45000));


    ViewInteraction viewInteraction = Espresso.onView(withText("**** 0431"));
    viewInteraction.perform(click());

    Thread.sleep(3000);

    onView(withId(R.id.btnTransferToCard)).check(matches(isDisplayed()));
}

}

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