java - Appium水平滚动屏幕上的滚动问题

标签 java appium appium-android

这是一个两阶段问题。

第一个问题是通过 Appium 在应用程序上滚动。以前,我们在应用程序的主屏幕上滚动没有问题,但现在主屏幕具有水平滚动的元素,我们内置到断言方法中的垂直滚动功能现在失败了(当搜索要断言的所选元素时,它们只是根本不滚动)。下面是我们用来查找元素的方法的示例:

public WebElement findElementIdByScrolling(String textContains){
    wait(Constants.WaitTime);
    return driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().resourceId(\"" + textContains + "\"));"));

我一直在寻找可行的解决方案,但到目前为止,还没有成功。

第二个问题取决于第一个问题的解决。与其说是一个问题,不如说是一个问题。如何在 Appium 上进行水平滚动?我见过的所有基于元素的滚动都涉及向下滚动屏幕,而不是仅垂直或水平滚动元素。

最佳答案

您可以在现有代码中尝试以下代码 scrollable(true).instance(0))

如果您使用 appium 作为自动化引擎,请添加所需的功能 UiAutomator2。

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

如果您有元素的 id,并且索引为 0(因为页面上有一个元素),请使用以下函数。

public void scrollByID(String Id, int index) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\""+Id+"\").instance("+index+"));")); 

        } catch (Exception e) {
           e.printStackTrace();
        }
    }

使用文本滚动

public void scrollByText(String menuText) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + menuText + "\").instance(0));")); 
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

随屏幕尺寸滚动:

public void scrollToBottom() {

      int  x = driver.manage().window().getSize().width / 2;
      int start_y = (int) (driver.manage().window().getSize().height * 0.2);
      int end_y = (int) (driver.manage().window().getSize().height * 0.8);
        TouchAction dragNDrop = new TouchAction(driver)
                        .press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                        .moveTo(PointOption.point(x, end_y))
                        .release();
        dragNDrop.perform();
    }

关于java - Appium水平滚动屏幕上的滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59771333/

相关文章:

java - 比较 List<String> 时是否有一种干净的方法来忽略大小写?

ios - Jenkins 无法打开 IOS 模拟器

java - 在Android Studio上使用TestNG运行演示程序

java - AppiumDriverLocalService 自动下载 chrome

appium - Appium查找元素的最佳实践

java - 使用 MVC 和 DAO 模式在 JSP 页面中的 HTML 中显示 JDBC 结果集

java - 错误 org.springframework.web.HttpMediaTypeNotSupportedException

java - 在java程序中执行jar文件

python - 在python中获取日期时间错误

android - AppiumOptions : System. TypeLoadException : 'Access is denied: ' OpenQA. Selenium.Remote.DesiredCapability'。'