问题
I have a list_view, which I want to test.
list_view item layout is RelativeLayout
, adapter is ItemAdapter
.
This code works fine (item contatining Daft Punk is visible):
@Test
public void listViewTest() {
Espresso.onView(withText("Daft Punk")).perform(click());
}
Both of this variants fail:
(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 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());
}
};
}
Error:
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)
回答1:
onData()
will scroll to the item for you, just remove scrollTo()
and it should click just fine.
scrollTo()
is used only for items that are inside the ScrollView
combined with onView()
回答2:
To force a ListView to scroll before clicking an item, you can use swipeUp()
or swipeDown()
in multiple calls:
// scroll the list down a little
onData(anything()).atPosition(0).perform(swipeUp());
// scroll the list down again
onData(anything()).atPosition(2).perform(swipeUp());
Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ Help ➔ Find Action ➔ search for "show context action"
or "show intention action"
➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ..."
. You can also assign a keyboard shortcut to "Show Intention Actions". More info here. Another way is to enable "Add unambiguous imports on the fly"
in the Settings.
来源:https://stackoverflow.com/questions/36782506/android-espresso-listview-scroll-to