繁体   English   中英

如何使用Java Appium Client在Android中滚动?

[英]How to scroll in android using java appium client?

我正在尝试使用Appium自动执行本机android应用程序。 我使用相同的Java客户端。 以下是我包含的依赖项。 由于我正在使用Java客户端的版本7,因此不支持滚动和滑动方法。 所以或者我如何滚动到一个特定的元素? 我使用TouchAction类遇到了一些代码片段,但我只想知道TouchAction类之外是否还有其他解决方案? Maven依赖-

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>

<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.0.0</version>
</dependency>

您可以将findElementByAndroidUIAutomator与不同的条件(例如文本,说明)一起使用:

((AndroidDriver<?>) appiumDriver).findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+ text + "\").instance(0))");
public MobileElement scrollElementByTextUsingDescription(String scrollableListContDesc, String uiClassSelector, String text) {
        return driver.findElement(MobileBy.AndroidUIAutomator(
           "new UiScrollable(new UiSelector().description(\"" + scrollableList + "\"))" +
           ".getChildByText(new UiSelector().className(\"" + uiClassSelector + "\"), \"" + text + "\")"));
    }

scrollableListContDesc是automationId / cont-可滚动列表的描述

uiClassSelector是可滚动列表的类名称,例如android.view.View

text是要滚动到的元素的文本。

如果滚动列表中没有cont-description ,则可以使用以下方法:

public MobileElement scrollElementByTextUsingId(String scrollableListId, String uiClassSelector, String text) {
            return driver.findElement(MobileBy.AndroidUIAutomator(
               "new UiScrollable(new UiSelector().resourceId(\"" + scrollableListId + "\"))" +
               ".getChildByText(new UiSelector().className(\"" + uiClassSelector + "\"), \"" + text + "\")"));
        }

scrollableListId是可滚动列表的id / resourceId

UIScrollable / UISelector是TouchAction的一种替代方法,可用于滚动/滑动。 例:

MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceId(\"com.android.vending:id/data_view\")).scrollIntoView("
            + "new UiSelector().textContains(\"HelloWorld\").instance(2))"));

这篇博客文章涵盖了多种解决方案,可以很好地滑动/滚动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM