【问题标题】:Showing exception when scroll down mobile app using touch action in appium在appium中使用触摸动作向下滚动移动应用程序时显示异常
【发布时间】:2020-03-30 17:47:36
【问题描述】:

我尝试向下滚动移动应用程序以单击屏幕下方的按钮。我已使用以下触摸操作进行滚动,但它给出了异常。还有其他可用的方法吗?任何人都请帮我向下滚动移动应用程序。

显示异常

线程“主”org.openqa.selenium.InvalidElementStateException 中的异常:滑动未成功完成

DesiredCapabilities androidCapabilities = new DesiredCapabilities();
AppiumDriver<MobileElement> appiumDriver;
androidCapabilities.setCapability("automationName",PropertyUtility.getProperty("AndroidAutomationName"));       androidCapabilities.setCapability("platformName",PropertyUtility.getProperty("AndroidPlatformName"));
    androidCapabilities.setCapability("platformVersion", PropertyUtility.getProperty("AndroidPlatformVersion"));
    androidCapabilities.setCapability("deviceName", PropertyUtility.getProperty("AndroidDeviceName"));
    androidCapabilities.setCapability("app",PropertyUtility.getProperty("AndroidAppPath"));
    androidCapabilities.setCapability("noReset", true);
    androidCapabilities.setCapability("newCommandTimeout", "60");
    appiumDriver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), androidCapabilities);

    Dimension dim=appiumDriver.manage().window().getSize();
    System.out.println(dim);
    int x=dim.getWidth()/2;
    int startY=(int) (dim.getHeight()*(.8));
    int endY=(int) (dim.getHeight()*(.2));
TouchAction action=new TouchAction(appiumDriver);
action.press(PointOption.point(x,startY)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000))).moveTo(PointOption.point(x, endY)).release().perform();

配置

appium 1.15 java客户端:7.3.0

【问题讨论】:

  • 你想达到什么目的?
  • 我想向下滚动移动应用程序以点击底部显示的按钮。该按钮仅在滚动页面后显示。

标签: java appium


【解决方案1】:

您可以执行滚动以查找特定元素,如下所示:

public void scrollToElementByText(String text) {
        try {
            ((AndroidDriver)driver).findElementByAndroidUIAutomator("newUiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0))");
        } catch (Exception e) {
            ...
        }
    }

显然,您可以使用任何属性来查找元素(id、可访问性 id 等),然后在该元素上使用 click()。或者您可以简单地向下滚动并检查要显示的元素,如果没有,请继续向下滚动:

private void swipe(int xStart, int yStart, int xStop, int yStop) {
    try {
        new TouchAction(driver)
                .press(point(xStart, yStart))
                .moveTo(point(xStop, yStop)).release().perform();
    } catch (Exception e) {
       ...
    }
}

public void scrollDown() {
    swipe(5,
            (getWindowSize().getHeight()) - 200,
            5,
            100);
}

public void scrollToElement(MobileElement el) {
    while (true) {
        if (!el.isDisplayed()) {
            //element is not displayed, keep scrolling
            scrollDown();
        } else {
            //element found
            break;
        }
    }
}

但要小心,因为您可以使用 last 方法获得无限循环(实现您自己的逻辑来打破该循环)。

【讨论】:

  • 谢谢。我已尝试使用您提供的解决方案,它帮助我向下滚动移动应用程序。
猜你喜欢
  • 2020-11-04
  • 2016-06-13
  • 2019-02-28
  • 1970-01-01
  • 2017-08-22
  • 2013-04-27
  • 1970-01-01
  • 2017-03-23
  • 2020-03-30
相关资源
最近更新 更多