在我的Android应用程序中,我需要向下滚动屏幕并验证所需的元素(每个单独的元素)是否可用。如何使用 Java 方法实现此目的?
使用以下代码使用 JAVA 上下滚动。
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text("**Text you want to Find**"));");
谢谢。
这里的好方法是使用本机UIAutomator查找策略:
public void isElementPresent(String resourceId) {
return androidDriver.findElementsByAndroidUIAutomator("new UiScrollable(new UiSelector()
.scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId("<your app package name>:id/" + resourceId + ""))")
.size() > 0;
}
它将向下滚动和向上滚动视图以按提供的resourceId
查找元素,并返回true
如果元素存在。
使用以下代码使用 java 向上滚动:
public void swipeUp(AppiumDriver<?> appiumDriver, MobileElement fromPosition, MobileElement toPosition) {
Dimension size = fromPosition.getSize();
Dimension size1 = toPosition.getSize();
TouchAction swipe = new TouchAction(appiumDriver)
.press(ElementOption.element(fromPosition, size.width / 2, size.height - 20))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(4)))
.moveTo(ElementOption.element(toPosition, size1.width / 2, size1.height / 2 + 30)).release();
swipe.perform();
logger.info("Swipe Success");
}