【问题标题】:Appium scrolling problem on screen with horizontal scrolling水平滚动屏幕上的Appium滚动问题
【发布时间】:2020-01-16 14:03:33
【问题描述】:

这是一个两阶段的问题。

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

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

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

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

【问题讨论】:

    标签: java appium appium-android


    【解决方案1】:

    您可以在现有代码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();
        }
    

    【讨论】:

    • 刚试过这个,它开始向上滚动而不是向下滚动。我可以为此做出调整吗?
    • 它将滚动到元素。如果你的元素是向上的,它会上下寻找元素
    • 元素位于页面底部(屏幕外)。当我运行它时,它无法滚动到元素,因为它以另一种方式滚动。
    • 您是否有多个具有相同 ID 的元素?在这种情况下,它将使它以不同的方式工作,否则它会完美地工作。如果你有多个元素,那么你可以给出元素的索引值
    • 不,我检查了检查员。该页面上只有一个具有该 ID 的元素。
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2019-04-28
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多