14

有谁知道,如何将 seekBar 设置为特定值或只需单击 Espresso UI 测试上的该视图?

我只是得到一个例外:在带有 id 的视图上执行“单击”时出错...

onView(withId(R.id.FilterPriceMax)).perform(click());
4

5 回答 5

29

您可能想尝试以下代码:

首先你可以匹配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);
            }
        };
    }
于 2014-06-04T12:13:00.123 回答
21

我有一个自定义的 Seekbar,而且我的视图中有多个 Seekbar。我设法用下面的代码做我的测试:

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);
            }
        };
    }
于 2014-07-10T17:32:01.660 回答
12

我使用滑动动作在SeekBar. 这确保调用回调方法 ( SeekBar.OnSeekBarChangeListener. onProgressChanged) 并fromUser设置为 true。它也更符合点击测试。

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 };
    }
}
于 2016-10-03T08:19:56.393 回答
10

如果您想检查进度值

  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;
      }
    };
  }
于 2014-09-30T08:56:09.450 回答
5

如果您不需要特定值,我已经使用它对搜索栏 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")));
于 2017-02-03T18:23:24.927 回答