BrowserStack中Appium Android swipeGesture不支持问题及滑动方案咨询
我在BrowserStack的Android设备上用Appium测试原生应用时,执行swipeGesture滑动操作抛出了UnsupportedCommandException,但相同代码在本地真机/模拟器上能正常运行。
异常信息
org.openqa.selenium.UnsupportedCommandException: Unknown mobile command "swipeGesture".
Only shell,scrollBackTo,viewportScreenshot,deepLink,startLogsBroadcast,stopLogsBroadcast,
acceptAlert,dismissAlert,batteryInfo,deviceInfo,changePermissions,getPermissions,
performEditorAction,startScreenStreaming,stopScreenStreaming,getNotifications,listSms,
type commands are supported.
我的代码
((JavascriptExecutor) driver).executeScript("mobile: swipeGesture", Map.of( "left", location.x, "top", location.y - swipeBoxHigh, "width", 100, "height", swipeBoxHigh, "direction", "up", "percent", 1, "speed", 1500 ));
异常原因
BrowserStack的Appium服务并未支持所有Appium原生的mobile:命令,swipeGesture属于Appium 2.x+新增的手势命令范畴,但目前BrowserStack仅开放了异常信息中列出的命令集,直接调用未支持的命令就会触发该异常。
替代滑动实现方案
在BrowserStack Android设备上,可通过以下几种方式实现滑动操作:
1. 使用TouchAction类(兼容通用场景)
通过传统的TouchAction模拟滑动,这是BrowserStack完全支持的方式:
import io.appium.java_client.TouchAction; import io.appium.java_client.touch.offset.PointOption; import org.openqa.selenium.Point; // 定义滑动起始点和终点(示例取元素区域的垂直中线) Point startPoint = new Point(location.x + 50, location.y); Point endPoint = new Point(location.x + 50, location.y - swipeBoxHigh); new TouchAction(driver) .press(PointOption.point(startPoint)) .waitAction() .moveTo(PointOption.point(endPoint)) .release() .perform();
2. 使用BrowserStack专属的scrollBackTo命令(针对元素定位滑动)
如果滑动目标是滚动到某个特定元素,可直接用BrowserStack提供的scrollBackTo命令:
// targetElement为需要滚动到的目标元素 ((JavascriptExecutor) driver).executeScript("mobile: scrollBackTo", Map.of( "elementId", ((RemoteWebElement) targetElement).getId() ));
3. 通过shell命令调用adb input swipe(底层模拟)
利用BrowserStack支持的shell命令,直接执行adb的滑动指令:
// 格式:input swipe 起始X 起始Y 结束X 结束Y 持续时间(ms) ((JavascriptExecutor) driver).executeScript("mobile: shell", Map.of( "command", "input swipe " + (location.x + 50) + " " + location.y + " " + (location.x + 50) + " " + (location.y - swipeBoxHigh) + " 1500" ));
内容的提问来源于stack exchange,提问作者figuedmundo

