首页 > 解决方案 > 如何执行向上滚动操作,直到找出需要使用 espresso 的元素

问题描述

我是浓缩咖啡的新手,我想滚动到屏幕顶部,直到找不到元素。如何在浓缩咖啡中做到这一点?在这里需要一些帮助。

标签: androidandroid-studioandroid-espresso

解决方案


我想滚动到顶部以单击发送按钮。如何做到这一点?

不需要滚动到顶部,你也可以使用scrollTo,这是ViewAction类已经定义的方法Espresso

 public static ViewAction scrollTo() {
    return actionWithAssertions(new ScrollToAction());
  }

您还必须检查它以使其正常工作。

/**
   * Returns an action that scrolls to the view.<br>
   * <br>
   * View preconditions:
   *
   * <ul>
   *   <li>must be a descendant of ScrollView
   *   <li>must have visibility set to View.VISIBLE
   *       <ul>
   */

那么,你必须做什么?首先知道 idButton或任何东西View,你可以简单地使用:

onView(withId(R.id.yourButtonId))

好的,那么您要做的就是执行 a scrollTo(),然后您可以这样做:

onView(withId(R.id.yourButtonId)).perform(ViewActions.scrollTo())

然后它应该去进行滚动的元素。

最后,如果您想点击它,View您可以在方法上添加操作,perform()如您在此处看到的:

/**
   * Performs the given action(s) on the view selected by the current view matcher. If more than one
   * action is provided, actions are executed in the order provided with precondition checks running
   * prior to each action.
   *
   * @param viewActions one or more actions to execute.
   * @return this interaction for further perform/verification calls.
   */

所以点击就像写一样简单,click()那么最终的代码应该是

onView(withId(R.id.yourButtonId)).perform(ViewActions.scrollTo(), ViewActions.click())


推荐阅读