Java 如何在android espresso测试中向下滚动屏幕?我需要验证屏幕上的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30867942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll down the screen in the android espresso test? I need to validate the text present on the screen
提问by HillHacker
I am performing the below test to validate the text on screen , The text is present on the view but need to page scroll to see the text manually.
我正在执行以下测试以验证屏幕上的文本,文本存在于视图中,但需要滚动页面才能手动查看文本。
onView(withText("Launch")).check(ViewAssertions.matches(isDisplayed()));
onView(withText("January 2010")).check(ViewAssertions.matches(isDisplayed()));
Following error is coming, However the text is present on the view but need to page scroll to see the text manually.
出现以下错误,但是文本存在于视图中,但需要滚动页面才能手动查看文本。
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view. Expected: is displayed on the screen to the user 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}"
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}"
回答by Be_Negative
Just perform a ViewActions.scrollTo()
before your assertion:
只需ViewActions.scrollTo()
在断言之前执行:
onView(withText("Launch")).perform(ViewActions.scrollTo()).check(ViewAssertions??.matches(isDisplayed()));
回答by piotrek1543
I don't know how this layout looks like, but to use
我不知道这个布局看起来如何,但要使用
Just perform a ViewActions.scrollTo()
before your assertion:
只需ViewActions.scrollTo()
在断言之前执行:
onView(withText("Launch"))
.perform(ViewActions.scrollTo())
.check(ViewAssertions??.matches(isDisplayed()));
you need to have ScrollView
as your main view in structure.
您需要将ScrollView
结构作为主要视图。
If you have already LinearLayout, RelativeLayout, you have to change it to like this:
如果你已经有了 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>
回答by Francisco Javier Gutierrez-Mat
There is a solution to click in a button in a scroll view
有一种在滚动视图中单击按钮的解决方案
onView(withId(R.id.onBottomOfScrollView)).perform(scrollTo(), click());
回答by Pablo
You can use SWIPE to scroll to the bottom of the screen:
您可以使用 SWIPE 滚动到屏幕底部:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp());
For me its working nice.
对我来说它的工作很好。
Pablo
巴勃罗