![](/img/trans.png)
[英]How to perform clicking on the first item in an ArrayList by Espresso
[英]Android Espresso - Clicking on the first RecyclerView Item with a certain string after swipeUp to ending of list
基本上,Espresso scrollTo 不适用于此视图,因此要断言已创建一个项目,我必须将向上滑动到列表末尾并使用特定字符串检查第一个项目。 该项目是创建该项目的另一个测试的结果。 根据之前运行的测试次数,该项目重复多次是正常的。 现在我无法点击项目,因为它不是完全可见的,或者有多个。 我设法在不同的情况下分别解决了这两个问题,但在这个问题上却没有同时解决。
我使用来自 stackoverflow 的这段代码来避免多个类似的视图异常:
public class FirstViewMatcher extends BaseMatcher {
public static boolean matchedBefore = false;
public FirstViewMatcher() {
matchedBefore = false;
}
@Override
public boolean matches(Object item) {
if (matchedBefore) {
return false;
} else {
matchedBefore = true;
return true;
}
}
@Override
public void describeTo(Description description) {
description.appendText(" first similar view to appear.");
}
@Factory
public static <T> Matcher<View> firstView() {
return new FirstViewMatcher();
}
并在执行单击时使用此代码,以便视图不需要完全可见。
ViewInteraction scrollView = onView(withId(R.id.ptr_layout));
scrollView.perform(swipeUp(), swipeUp());
ViewInteraction testItem= onView(
allOf(withText("Favorite Test"),
FirstViewMatcher.firstView()));
testItem.check(matches(allOf(isEnabled(), isClickable()))).perform(
new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isDisplayingAtLeast(1); // no constraints, they are checked above
}
@Override
public String getDescription() {
return "clicking not fully visible item";
}
@Override
public void perform(UiController uiController, View view) {
view.performClick();
}
}
);
我收到此错误:
E/TestRunner: failed: a_CheckFavoriteDetails(ca.com.android.celero.Public.ACU_E_CheckFavoriteDetails)
----- begin exception -----
E/TestRunner: androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: '(is enabled and is clickable)' doesn't match the selected view.
Expected: (is enabled and is clickable)
Got: "AppCompatTextView{id=2131362475, res-name=favourite_list_item_description, visibility=VISIBLE, width=837, height=50, 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, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@83ce175, tag=null, root-is-layout-requested=false, has-input-connection=false, x=165.0, y=46.0, text=Favorite Test, input-type=0, ime-target=false, has-links=false}"
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1720)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:324)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:306)
at ca.com.android.celero.Public.ACU_E_CheckFavoriteDetails.a_CheckFavoriteDetails(ACU_E_CheckFavoriteDetails.java:166)
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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at androidx.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:134)
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 androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: junit.framework.AssertionFailedError: '(is enabled and is clickable)' doesn't match the selected view.
Expected: (is enabled and is clickable)
Got: "AppCompatTextView{id=2131362475, res-name=favourite_list_item_description, visibility=VISIBLE, width=837, height=50, has-focus=false, has-fo
----- end exception -----
ViewActions.scrollTo
仅适用于ScrollView
并且ViewActions.swipeUp
在RecyclerView
使用时可能会RecyclerView
。 FirstViewMatcher
不是一个好的解决方案,因为它只会在第一次使用时起作用。 您收到错误是因为您希望TextView
可点击,但事实并非如此。
相反,您应该考虑使用RecyclerViewActions对您的RecyclerView
执行测试:
onView(R.id.your_recycler_view)
.perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click())
RecyclerViewActions.actionOnItemAtPosition
将滚动到该位置,然后执行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.