UI:APP录音控件无点击、无长按事件,怎么实现长按录音

在测试的APP,有个录音功能,无点击、无长按事件,怎么实现长按录音。

这个组件是一个特殊的组件,可能是开发调的一个api,类似于手势解锁。把整个组件放在这里实现某部分功能。所以没有办法获取到内部的组件。但我们能获取到它整体的布局结构。

由于你要点击的元素在中心点,可以先获取到这个组件的起点坐标,再获取到组件的宽和高,然后计算出它中心点的坐标。之后调用我们学过的长按操作就能实现了。如果这个位置不在正中心,就需要知道 它偏移的百分比来获取你要点击的坐标值。

python 实现的代码如下:

    def test_case1(self):
        self.driver.find_element(MobileBy.XPATH,'//*[@text="大数据语音转文字测试"]').click()
        self.driver.find_element(MobileBy.ID, 'com.jlb.zhixuezhen.app:id/input_voice').click()
        ele = self.driver.find_element(MobileBy.ID, 'com.jlb.zhixuezhen.app:id/voice_layout')
        location = ele.location
        size = ele.size
        ele_mid_x = location['x']+size['width']/2
        ele_mid_y = location['y']+size['height']/2
        durationtime = 10 * 1000
        self.driver.swipe(ele_mid_x,ele_mid_y,ele_mid_x,ele_mid_y,durationtime)
        sleep(5)

上面用到的 api 是 driver.swipe(),它可以实现滑动操作,起点和终点坐标一致,时间大于2秒就会实现长按的效果。很多api(比如TouchAction)都能实现长按的效果。

1 个赞

谢谢老师,从来没有往你说的这个思路考虑,感觉UI测试又开启了一扇新的大门。 :star_struck: 我按照你说的方式走一遍~~

看报错信息是你那边没有找到这个元素。不同的设备上布局结构也有可能会发生变化。 换一种方式,使用坐标点实现这个功能。

我这边展示出来语音 icon 之后,发现它所在的 y 轴坐标占整个屏幕的 4/5 处, x轴坐标选中间点就可以。demo 如下:

    def test_case1(self):
        self.driver.find_element(MobileBy.XPATH, '//*[contains(@text,"大数据语音转文字测试")]').click()
        self.driver.find_element(MobileBy.ID, 'com.jlb.zhixuezhen.app:id/input_voice').click()
        sleep(2)
        window_size = self.driver.get_window_size()
        width = window_size['width']
        height = window_size['height']

        x = width / 2
        y = height * 0.8
        print(f"x = {x} y={y}")

        durationtime = 10 * 1000
        self.driver.swipe(x, y, x, y, durationtime)

必要的时候需要借助坐标点,如果能识别出来最好使用它的属性来进行定位。

1 个赞

谢谢XIXI老师、sam老师的大力帮助,终于解决的我的录音问题 :smiling_face_with_three_hearts:
int width = driver.manage().window().getSize().getWidth();
int heigth = driver.manage().window().getSize().getHeight();
int x = width / 2;
int y = (int) Math.ceil(heigth * 0.8);

    TouchAction ta = new TouchAction(driver);
    ta.longPress(new LongPressOptions().withPosition(PointOption.point(x, y))).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(15))).release().perform();