有谁知道,如何将搜索栏设置为特定值,或者只是在Espresso UI测试中点击该视图?
我只是得到一个例外:在带有id ...
的视图上执行“单击”时出错onView(withId(R.id.FilterPriceMax)).perform(click());
答案 0 :(得分:21)
您可能需要尝试以下代码:
首先,您可以将SeekBar
与其班级名称匹配:
onView(withClassName(Matchers.equalTo(SeekBar.class.getName()))).perform(setProgress(progress));
其中setProgress(final int progress)是您定义为的ViewAction:
public static ViewAction setProgress(final int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar) view;
seekBar.setProgress(progress);
}
@Override
public String getDescription() {
return "Set a progress on a SeekBar";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
};
}
答案 1 :(得分:19)
我有一个自定义Seekbar,而且我的View中也有多个Seekbars。我使用以下代码管理我的测试:
onView(withId(R.id.my_seek_bar)).perform(setProgress(10));
和
public static ViewAction setProgress(final int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
((MyCustomSeekBar) view).setSelectedProgress(progress);
//or ((SeekBar) view).setProgress(progress);
}
@Override
public String getDescription() {
return "Set a progress";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(RangeSeekBar.class);
}
};
}
答案 2 :(得分:9)
如果您想检查进度值
public static Matcher<View> withProgress(final int expectedProgress) {
return new BoundedMatcher<View, SeekBar>(SeekBar.class) {
@Override
public void describeTo(Description description) {
description.appendText("expected: ");
description.appendText(""+expectedProgress);
}
@Override
public boolean matchesSafely(SeekBar seekBar) {
return seekBar.getProgress() == expectedProgress;
}
};
}
答案 3 :(得分:7)
我使用滑动操作在SeekBar
上执行实际滑动。这可确保在SeekBar.OnSeekBarChangeListener. onProgressChanged
设置为true的情况下调用回调方法(fromUser
)。它也更符合点击测试。
public static ViewAction scrubSeekBarAction(int progress) {
return actionWithAssertions(new GeneralSwipeAction(
Swipe.SLOW,
new SeekBarThumbCoordinatesProvider(0),
new SeekBarThumbCoordinatesProvider(progress),
Press.PINPOINT));
}
private static class SeekBarThumbCoordinatesProvider implements CoordinatesProvider {
int mProgress;
public SeekBarThumbCoordinatesProvider(int progress) {
mProgress = progress;
}
private static float[] getVisibleLeftTop(View view) {
final int[] xy = new int[2];
view.getLocationOnScreen(xy);
return new float[]{ (float) xy[0], (float) xy[1] };
}
@Override
public float[] calculateCoordinates(View view) {
if (!(view instanceof SeekBar)) {
throw new PerformException.Builder()
.withViewDescription(HumanReadables.describe(view))
.withCause(new RuntimeException(String.format("SeekBar expected"))).build();
}
SeekBar seekBar = (SeekBar) view;
int width = seekBar.getWidth() - seekBar.getPaddingLeft() - seekBar.getPaddingRight();
double progress = mProgress == 0 ? seekBar.getProgress() : mProgress;
int xPosition = (int) (seekBar.getPaddingLeft() + width * progress / seekBar.getMax());
float[] xy = getVisibleLeftTop(seekBar);
return new float[]{ xy[0] + xPosition, xy[1] + 10 };
}
}
答案 4 :(得分:2)
如果您不需要特定值,我已经使用它对seekbar UI组件进行了一些基本测试。它假定'check()'基于0-100百分比。
onView(withId(R.id.yourSeekBar)).perform(new GeneralClickAction(Tap.SINGLE, GeneralLocation.CENTER_LEFT, Press.FINGER));
onView(withId(R.id.yourSeekBarUserFeedback)).check(matches(withText("0")));
onView(withId(R.id.yourSeekBar)).perform(new GeneralClickAction(Tap.SINGLE, GeneralLocation.CENTER, Press.FINGER));
onView(withId(R.id.yourSeekBarUserFeedback)).check(matches(withText("50")));
onView(withId(R.id.yourSeekBar)).perform(new GeneralClickAction(Tap.SINGLE, GeneralLocation.CENTER_RIGHT, Press.FINGER));
onView(withId(R.id.yourSeekBarUserFeedback)).check(matches(withText("100")));