此代码不会在真实设备中向下滑动,但在模拟器中工作(使用 Appium)



我使用的是Appium For Automation,这段代码用于向下滑动,但当我在真实设备上使用它时,它就不起作用了。如何为实际设备解决此问题。

Dimension dimension = driver.manage().window().getSize();
int start_x = (int) (dimension.width * 0.5);
int start_y = (int) (dimension.height * 0.8);
int end_x = (int) (dimension.width * 0.5);
int end_y = (int) (dimension.height * 0.2);

new TouchAction(driver).press(PointOption.point(start_x, start_y))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(end_x, end_y)).release().perform();

如果你想要一个可靠的解决方案,你需要避免使用基于屏幕尺寸的滑动,即使你知道尺寸,它也不是100%准确的。

使用AndroidUIAutomator定位器,您可以在搜索元素旁边自动向上/向下滚动视图,例如,您只知道文本";这里是":

MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains("Here it is"))";

就我个人而言,我强烈建议不要使用文本,而是检查Android文档中的resourceIdclassName。Appium文档也有例子。

最新更新