本文介绍了Android Espresso listView滚动到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个list_view,我想对其进行测试.
I have a list_view, which I want to test.
list_view项的布局为 RelativeLayout
,适配器为 ItemAdapter
.
list_view item layout is RelativeLayout
, adapter is ItemAdapter
.
此代码可以正常工作(包含 Daft Punk 的项目可见):
This code works fine (item contatining Daft Punk is visible):
@Test
public void listViewTest() {
Espresso.onView(withText("Daft Punk")).perform(click());
}
这两个变体均失败:
(滚动开始,出现在项目上,然后失败,错误恰恰在这个问题上.)
(The scrolling starts, comes to the item and then fails, the error is at the very and of this question.)
@Test
public void listViewTest() {
Espresso.onData(artistWithName("Imagine Dragons")).perform(scrollTo(), click());
}
@Test
public void listViewTest() {
Espresso.onData(artistWithName("Imagine Dragons")).
.inAdapterView(withId(R.id.list_view))
.perform(scrollTo(), click());
}
artistWithName定义:
artistWithName definition:
public static Matcher<Object> artistWithName(String expectedName) {
Checks.checkNotNull(expectedName);
return artistWithName(equalTo(expectedName));
}
public static Matcher<Object> artistWithName(final Matcher<String> itemMatcher) {
Checks.checkNotNull(itemMatcher);
return new BoundedMatcher<Object, ArtistItem>(ArtistItem.class) {
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("ArtistTime with name: ");
itemMatcher.describeTo(description);
}
@Override
protected boolean matchesSafely(ArtistItem artistItem) {
return itemMatcher.matches(artistItem.getName());
}
};
}
错误:
android.support.test.espresso.PerformException: Error performing 'scroll to' on view ' displaying data matching: ArtistTime with name: "Imagine Dragons" within adapter view matching: with id: com.example.iskhakovt.yandextest:id/list_view'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at android.support.test.espresso.DataInteraction.perform(DataInteraction.java:130)
at com.example.iskhakovt.yandextest.MainActivityTest.listViewTest(MainActivityTest.java:52)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView))
Target view: "RelativeLayout{id=-1, visibility=VISIBLE, width=1080, height=420, 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=0.0, y=0.0, child-count=4}"
Further Info: ScrollToAction on a view inside an AdapterView will not work. Use Espresso.onData to load the view.
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
推荐答案
onData()
将为您滚动到该项目,只需删除 scrollTo()
,它应该单击就可以了.
onData()
will scroll to the item for you, just remove scrollTo()
and it should click just fine.
scrollTo()
仅用于与 onView()
这篇关于Android Espresso listView滚动到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!