【问题标题】:How to scroll down the screen in the android espresso test? I need to validate the text present on the screen如何在 android espresso 测试中向下滚动屏幕?我需要验证屏幕上的文本
【发布时间】:2015-09-01 07:01:52
【问题描述】:

我正在执行以下测试以验证屏幕上的文本,文本显示在视图上但需要滚动页面才能手动查看文本。

 onView(withText("Launch")).check(ViewAssertions.matches(isDisplayed()));
    onView(withText("January 2010")).check(ViewAssertions.matches(isDisplayed()));

以下错误即将到来,但是文本存在于视图中,但需要手动滚动页面才能看到文本。

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is display on the screen to the user' 与所选视图不匹配。 预期:在屏幕上显示给用户 得到:“TextView{id=2131361941, res-name=project_details_label_tv, visibility=VISIBLE, width=249, height=41, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable =false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection =false, x=4.0, y=24.0, text=Status, input-type=0, ime-target=false, has-links=false}"

【问题讨论】:

标签: java android ui-automation android-espresso


【解决方案1】:

我不知道这个布局是什么样子的,但是要使用

在断言之前执行ViewActions.scrollTo()

onView(withText("Launch"))
         .perform(ViewActions.scrollTo())
         .check(ViewAssertions‌​.matches(isDisplayed()));

您需要将ScrollView 作为结构中的主视图。

如果你已经有LinearLayout,RelativeLayout,你得改成这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

【讨论】:

  • 我不相信你关于 ScrollView 的断言是正确的。视图必须在滚动视图内,但滚动视图不必是布局中的父(或主)视图。
【解决方案2】:

您可以使用 SWIPE 滚动到屏幕底部:

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

对我来说它工作得很好。

巴勃罗

【讨论】:

    【解决方案3】:

    有一个解决方案可以在滚动视图中单击按钮

    onView(withId(R.id.onBottomOfScrollView)).perform(scrollTo(), click());
    

    Espresso: how to scroll to the bottom of ScrollView

    【讨论】:

      【解决方案4】:

      在断言之前执行ViewActions.scrollTo()

      onView(withText("Launch")).perform(ViewActions.scrollTo()).check(ViewAssertions‌​.matches(isDisplayed()));
      

      【讨论】:

      • * 仅“如果您正在使用的视图位于 ScrollView 内”。
      • 这可能会抛出“NoMatchingViewException”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多