【问题标题】:How to make the Espresso test wait until the page loads如何让 Espresso 测试等到页面加载
【发布时间】:2020-02-18 12:01:06
【问题描述】:

我将登录。然后,请求显示卡片列表。每次这个请求发生在不同的时间并且使用 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


【解决方案1】:

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

请查看此链接:

Android Espresso wait for text to appear

【讨论】:

    【解决方案2】:

    我认为您应该将登录和查看卡列表之间的测试场景分开。 第一种登录场景:

    public class AuthorizationTest {
        @Rule
        public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);
    private Activity activity;
        @Before()
        public void setup() {
            activityTestRule.launchActivity(new Intent());
            activity = activityTestRule.getActivity();
        }
    
        @After()
        public void tearDown() {
            activityTestRule.finishActivity();
        }
    @Test
    public void login() throws Exception {
        onView(withId(R.id.btnAuthLogin)).perform(click());
        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());
    //asssert login success
    onView(withId(R.id.toastloginsuccess)).check(matches(isDisplayed()));
    }
    

    第二个场景直接进入卡片列表活动。

    public class CardListTest {
        @Rule
        public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
    private Activity activity;
        @Before()
        public void setup() {
            activityTestRule.launchActivity(new Intent());
            activity = activityTestRule.getActivity();
        }
    
        @After()
        public void tearDown() {
            activityTestRule.finishActivity();
        }
    @Test
    public void checkMoneyBackButton() throws Exception {
    //assertMainActivity
    onView(withId(R.id.menuTitle)).check(matches(isDisplayed()));
    //Assert what you want
    onView(withId(R.id.btnTransferToCard)).check(matches(isDisplayed()));
    // Do what you want
    }
    

    希望有帮助。

    【讨论】:

      【解决方案3】:

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

      @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()));
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-02
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-23
        • 2015-11-28
        相关资源
        最近更新 更多