【问题标题】:How to reach the end of a scroll bar in appium?如何到达appium中滚动条的末尾?
【发布时间】:2019-04-10 13:53:24
【问题描述】:

我正在尝试自动滚动水平条,其中条的元素是动态的并且是从 API 获取的。

有没有办法在 appium 中实现自动化?

【问题讨论】:

    标签: java selenium selenium-webdriver appium appium-android


    【解决方案1】:

    如果页面底部有任何元素或文本,则可以使用 UiAutomator2。

    如果您使用的是 appium,请添加所需的功能“UiAutomator2”。

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

    如果你有元素的 id,现在使用下面的函数

     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();
            }
        }
    

    如果您不知道 botton 中的任何元素,那么您必须使用屏幕尺寸

    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();
        }
    

    【讨论】:

    • 谢谢穆扎米尔。不幸的是,我不知道最后的元素,它是从 API 和动态中获取的。我已按照您的建议添加了所需的功能。
    • @lionxlamb 我已根据您的要求编辑了帖子。看看最后一个函数。
    • @Muzzamil 我是否正确阅读了您的代码,您假设向下滚动 8 * driver.manage().window().getSize().height 将是底部?我不认为这总是正确的。我会将滚动循环并在 Y 位置不再变化时停止滚动。
    • @AndiCover 8 将是屏幕高度的 80%,因此它几乎是底部。滚动将发生在高度的 20% 到 80% 之间。如果我们将滚动/函数放在一个循环中,那么在每次迭代中,它将获取屏幕宽度和高度并计算 x、start_y 和 end_y,因此如果我们在循环中调用函数,它将是动态的。
    • @Muzzamil,感谢您的澄清。我以为 driver.manage().window().getSize().height 返回设备屏幕的高度,而不是实际设备。
    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 2013-04-30
    • 2022-01-10
    相关资源
    最近更新 更多