美文网首页
单元测试(6)-实现长按功能

单元测试(6)-实现长按功能

作者: lisx_ | 来源:发表于2020-03-23 16:13 被阅读0次

目前单元测试中的的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);
        }

相关文章

网友评论

      本文标题:单元测试(6)-实现长按功能

      本文链接:https://www.haomeiwen.com/subject/fyywyhtx.html