一门选修的大作业需要对一个app进行测试,在使用Espresso进行集成测试的时候遇到了SeekBar组件,查了好长时间才找到,特此记录下来。
设置seekbar的值
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);
}
};
}
然后使用下面这句话进行调用
onView(withId(R.id.sb_new_task_progress))
.perform(setProgress(90));
获取seekbar的值
public int getProgress(Matcher<View> matcher) {
final int[] progress = {0};
onView(matcher).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
@Override
public String getDescription() {
return "seekbar";
}
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar)view;
progress[0] = seekBar.getProgress();
}
});
return progress[0];
}
使用下面这句话进行调用
int progress = getProgress(withId(R.id.sb_new_task_progress));
onView(withId(R.id.new_task_progress)).check(matches(withText(progress+"%")));
参考
在这里要感谢这两位大佬的博客
http://www.voidcn.com/article/p-nffftfpi-bws.html
https://blog.csdn.net/qq744746842/article/details/50672689