目前单元测试中的的longClick并不能很好的满足我们要实现长按按钮的需求,无法控制一个大概的长按时间。因此需要一个特殊的操作:
UiObject inputToggle = mUiDevice.findObject(
new UiSelector().text(mContext.getString(R.string.rc_input_voice)));
if (inputToggle.waitForExists(1000)) {
onView(withId(R.id.rc_audio_input_toggle)).perform(click());//点击
onView(withId(R.id.rc_audio_input_toggle)).perform(longClick());//长按
SystemClock.sleep(1000);
try {
UiObject input = mUiDevice.findObject(new UiSelector().resourceId("xxx.xxxx.xxx.test:id/rc_audio_input_toggle"));//获取到控件
Rect bounds = input.getBounds();//找出控件的中心点坐标位置
int x = bounds.centerX();
int y = bounds.centerY();
Log.e(TAG, "需要长按的坐标点" + x + "__" + y);
mUiDevice.swipe(x, y, x, y, 300);
//通过调用swip()方法,起始点和结束点坐标传入一样的值。第五个参数传入大值越大长按的时间就会越长
} catch (UiObjectNotFoundException e) {
Log.e(TAG, "UiObjectNotFoundException: ", e);
}
SystemClock.sleep(3000);
}
网友评论