Issue
I'm trying to select an item from a list using Espresso.
It should be pretty straightforward, but I need to select an item with multiple conditions.
The conditions are:
task.getLocation != null; task.getCurrentStatus == Task.ASSIGNED;
So using atPosition() won't work. The items in the list are constantly changing.
My sad attempt:
onData(new BoundedMatcher<Object, Task>(Task.class) {
@Override
protected boolean matchesSafely(Task item) {
return (item.getLocation() != null && item.getCurrentStatus() == Task.ASSIGNED);
}
@Override
public void describeTo(Description description) {
}
}).inAdapterView(withId(R.id.listView)).perform(click());
I took it from Dinesh VG's answer here:
Espresso onData perform click on multiple items
But if I use the complete given example, it doesn't work. It does not accept allOf() nor is().
I also tried other approaches as well, pretty unsuccessfully.
The stack trace:
android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'Animations or transitions are enabled on the target device.
For more info check: -------
with id: com.sentilant.driviantasks:id/listView'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:79)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:173)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:114)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:213)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:210)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:230)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:205)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.displayDataMatcher(DataInteraction.java:248)
at android.support.test.espresso.DataInteraction.makeTargetMatcher(DataInteraction.java:150)
at android.support.test.espresso.DataInteraction.perform(DataInteraction.java:135)
at com.sentilant.driviantasks.TestMapMasterUI.testLayoutWithTask(TestMapMasterUI.java:201)
at com.sentilant.driviantasks.TestMapMasterUI.NavigationLayoutTests(TestMapMasterUI.java:73)
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 android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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 android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
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:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2082)
Caused by: java.lang.RuntimeException: Multiple data elements matched: . Elements: [Data: com.sentilant.driviantasks.api.task.Task@bb69b92 (class: com.sentilant.driviantasks.api.task.Task) token: 17, Data: com.sentilant.driviantasks.api.task.Task@61edede (class: com.sentilant.driviantasks.api.task.Task) token: 18]
at android.support.test.espresso.action.AdapterDataLoaderAction.perform(AdapterDataLoaderAction.java:125)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:356)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248)
at android.support.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Any ideas?
Thank you in advance.
Solution
From the stacktrace it looks like your matcher is working correctly :) it's just that there are multiple matches - Caused by: java.lang.RuntimeException: Multiple data elements matched: . Elements:
You could write a matcher that selects the first result out of a set of results:
public static <T> Matcher<T> first(final Matcher<T> matcher) {
return new BaseMatcher<T>() {
private boolean firstSelected = false;
@Override
public boolean matches(final Object item) {
if (!firstSelected && matcher.matches(item)) {
firstSelected = true;
return true;
}
return false;
}
@Override
public void describeTo(final Description description) {
}
};
}
And then use it as the following:
onData(first(new BoundedMatcher<Object, Task>(Task.class) {
@Override
protected boolean matchesSafely(Task item) {
return (item.getLocation() != null && item.getCurrentStatus() == Task.ASSIGNED);
}
@Override
public void describeTo(Description description) {
}
})).inAdapterView(withId(R.id.listView)).perform(click());
For onData
it could be even simpler by using atPosition
:
onData(<your matcher>).inAdapterView(<adapter view ID>).atPosition(0).perform(click());
Answered By - Anatolii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.