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

时间:2015-06-16 12:44:12

标签: java android ui-automation android-espresso

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

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

以下错误即将发生,但文本显示在视图上,但需要页面滚动才能手动查看文本。

  

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

4 个答案:

答案 0 :(得分:16)

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

  

在断言之前只需执行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>

`

答案 1 :(得分:6)

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

robotframework-selenium2library

对我而言,它工作得很好。

巴勃罗

答案 2 :(得分:4)

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

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

Espresso: how to scroll to the bottom of ScrollView

答案 3 :(得分:1)

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

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