如何在android espresso测试中向下滚动屏幕?我需要验证屏幕上显示的文本

Hil*_*ker 16 java android ui-automation android-espresso

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

 onView(withText("Launch")).check(ViewAssertions.matches(isDisplayed()));
    onView(withText("January 2010")).check(ViewAssertions.matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

出现以下错误,但文本出现在视图中,但需要页面滚动才能手动查看文本.

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'在屏幕上显示给用户'与所选视图不匹配.预期:在屏幕上显示给用户Got:"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}"

pio*_*543 17

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

只需ViewActions.scrollTo()在断言前执行:

   `onView(withText("Launch")).perform(ViewActions.scrollTo()).check(ViewAssertions??.matches(isDisplayed()));`
Run Code Online (Sandbox Code Playgroud)

你需要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>
Run Code Online (Sandbox Code Playgroud)

`


小智 8

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

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp());
Run Code Online (Sandbox Code Playgroud)

对我来说它工作得很好.

巴勃罗


Be_*_*ive 5

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

onView(withText("Launch")).perform(ViewActions.scrollTo()).check(ViewAssertions??.matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)


小智 5

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

onView(withId(R.id.onBottomOfScrollView)).perform(scrollTo(), click());
Run Code Online (Sandbox Code Playgroud)

浓缩咖啡:如何滚动到ScrollView的底部