使用Espresso从列表中选择具有多个条件的项目

时间:2018-06-12 15:09:53

标签: android layout android-espresso

我正在尝试使用Espresso从列表中选择一个项目。

它应该非常简单,但我需要选择一个具有多个条件的项目。

条件是:

task.getLocation!= null; task.getCurrentStatus == Task.ASSIGNED;

因此使用atPosition()将无效。列表中的项目不断变化。

我悲伤的尝试:

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());

我从Dinesh VG的回答中得到了它:

Espresso onData perform click on multiple items

但如果我使用完整的给定示例,它就不起作用。它不接受allOf(),也不接受()。

我也尝试了其他方法,但都没有成功。

堆栈跟踪:

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)

有什么想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

从堆栈跟踪看起来你的匹配器工作正常:)它只是有多个匹配 - Caused by: java.lang.RuntimeException: Multiple data elements matched: . Elements:

您可以编写一个匹配器,从一组结果中选择第一个结果:

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) {
        }
    };
}

然后将其用作以下内容:

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());

对于onData,使用atPosition

可能会更简单
onData(<your matcher>).inAdapterView(<adapter view ID>).atPosition(0).perform(click());