JLa*_*kin 5 android android-espresso
如何使用Espresso从设计库中单击NavigationView中的menuItem?它可以在整个菜单可见的大型设备上正常工作,但在横向或较小的设备中,我的菜单底部不在屏幕上,我无法想出滚动/滑动到这些menuItem的方法,所以我可以点击它们.
呼叫:
onView(withText(R.string.contact_us)).perform(scrollTo()).check(matches(isDisplayed())).perform(click());
Run Code Online (Sandbox Code Playgroud)
生成以下内容:
引起:java.lang.RuntimeException:不会执行Action,因为目标视图不匹配以下一个或多个约束:(视图具有有效可见性= VISIBLE并且是a的后代:(可从类中分配:class android .widget.ScrollView或可从类中分配:class android.widget.HorizontalScrollView))目标视图:"NavigationMenuItemView {id = -1,visibility = VISIBLE,width = 888,height = 144,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 = 1653.0,text = Contact Us,input-type = 0,ime-target = false,has-links = false}"
NavigationView继承自FrameLayout并正在进行画布移动以进行滚动. https://developer.android.com/reference/android/support/design/widget/NavigationView.html
我尝试使用下面的操作编写一个customViewAction来慢慢滚动屏幕,并检查menuItem是否可见,如果没有继续滚动,但我无法让它工作.
CoordinatesProvider coordinatesProvider = new CoordinatesProvider() {
public float[] calculateCoordinates(View view) {
float[] xy = GeneralLocation.BOTTOM_CENTER.calculateCoordinates(view);
xy[0] += 0.0F * (float)view.getWidth();
xy[1] += -0.083F * (float)view.getHeight();
return xy;
}
};
onView(withId(R.id.navigation_view)).perform(new GeneralSwipeAction(Swipe.SLOW,
coordinatesProvider, GeneralLocation.TOP_CENTER, Press.FINGER));
onView(withText(R.string.contact_us)).check(matches(isDisplayed())).perform(click());
Run Code Online (Sandbox Code Playgroud)
有没有人想出如何在Espresso上测试navigationView?我很确定我的customViewAction背后的逻辑可以工作,但我无法让它工作.
我明白了,我使用了自定义 viewAction
public static ViewAction swipeForTextToBeVisible(final String text, final long millis, final View navigationView) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "swipe for a specific view with text <" + text + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withText(text);
do {
boolean returnAfterOneMoreSwipe = false; //not sure why I have to do one more swipe but it works
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
returnAfterOneMoreSwipe = true;
}
}
CoordinatesProvider coordinatesProvider = new CoordinatesProvider() {
public float[] calculateCoordinates(View view) {
float[] xy = GeneralLocation.BOTTOM_CENTER.calculateCoordinates(view);
xy[0] += 0.0F * (float)view.getWidth();
xy[1] += -0.083F * (float)view.getHeight();
return xy;
}
};
//Below code is c/p from perform in android/support/test/espresso/action/GeneralSwipeAction.class
float[] startCoordinates = coordinatesProvider.calculateCoordinates(navigationView);
float[] endCoordinates = GeneralLocation.TOP_CENTER.calculateCoordinates(navigationView);
float[] precision = Press.FINGER.describePrecision();
Swiper.Status status = Swiper.Status.FAILURE;
for(int tries = 0; tries < 3 && status != Swiper.Status.SUCCESS; ++tries) {
try {
status = Swipe.SLOW.sendSwipe(uiController, startCoordinates, endCoordinates, precision);
} catch (RuntimeException var9) {
throw (new PerformException.Builder()).withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(navigationView)).withCause(var9).build();
}
int duration = ViewConfiguration.getPressedStateDuration();
if(duration > 0) {
uiController.loopMainThreadForAtLeast((long)duration);
}
}
//End c/p from android/support/test/espresso/action/GeneralSwipeAction.class
if (returnAfterOneMoreSwipe) {
return;
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个解决方案,它不是最干净的,因为出于某种原因我必须在它说它可见但它有效后再次滑动。
要使用它,您可以通过以下方式调用它:
onView(isRoot()).perform(CustomViewActions.swipeForTextToBeVisible(getActivity().getString(R.string.contact_us),2000, getActivity().findViewById(R.id.navigation_view)));
Run Code Online (Sandbox Code Playgroud)