Appium AndroidUIAutomator2滚动速度过慢,如何优化提速?
提升UiScrollable滚动速度的优化方案
针对你用UiScrollable滚动速度慢的问题,给你几个实用的优化方法:
增大滚动步长
UiScrollable默认滚动步长较小,你可以通过setScrollY()(垂直滚动)或setScrollX()(水平滚动)设置更大的步长,减少滚动次数。比如:public WebElement scrollToElement() { return getDriver().findElement(AppiumBy.androidUIAutomator( "new UiScrollable(new UiSelector().resourceId(\"tv_today_campaigns\")).setScrollY(500).scrollIntoView(new UiSelector().text(\"Kampanyalar ve Duyurular\"))" )); }步长值可根据设备屏幕尺寸调整,越大滚动幅度越大,但注意不要过大导致跳过目标元素。
使用快速滚动方法
先用flingToEnd()或flingForward()这类快速滑动方法接近目标区域,再精准定位,比持续慢滚动高效得多:public WebElement scrollToElement() { return getDriver().findElement(AppiumBy.androidUIAutomator( "new UiScrollable(new UiSelector().resourceId(\"tv_today_campaigns\")).flingToEnd(5).scrollIntoView(new UiSelector().text(\"Kampanyalar ve Duyurular\"))" )); }flingToEnd(5)表示最多快速滑动5次到容器末尾,之后再执行精准滚动查找元素。缩小滚动容器范围
确认resourceId("tv_today_campaigns")是最小的滚动容器,不要选择整个页面的父容器。滚动范围越小,无效滚动区域越少,速度自然越快。减少不必要的滚动操作
每次调用方法前先判断目标元素是否已可见,只有不可见时再执行滚动逻辑,避免重复滚动:public WebElement scrollToElement() { WebElement target = getDriver().findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"Kampanyalar ve Duyurular\")")); if (target.isDisplayed()) { return target; } return getDriver().findElement(AppiumBy.androidUIAutomator( "new UiScrollable(new UiSelector().resourceId(\"tv_today_campaigns\")).flingToEnd(5).scrollIntoView(new UiSelector().text(\"Kampanyalar ve Duyurular\"))" )); }调整Appium滚动超时设置
初始化Driver时,通过DesiredCapabilities缩短滚动后的等待超时,减少不必要的等待时间:DesiredCapabilities capabilities = new DesiredCapabilities(); // 其他配置... capabilities.setCapability("settings[scrollAcknowledgmentTimeout]", 100); // 单位:毫秒
内容的提问来源于stack exchange,提问作者Onurcan
相关产品推荐
相关产品推荐

