无法使用UiScrollable和TouchAction在Appium中水平滚动

问题描述 投票:1回答:1

我正在尝试滚动至“进入我的旅行”主页上的“礼品卡”选项,然后单击它。到目前为止,我已经尝试了以下两种方法,但均未成功。我还附上了应用程序主页的屏幕截图,以便于清楚理解。

方法1:使用AndroidUIAutomator滚动到特定元素。

driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()"
                + ".resourceId(\"com.makemytrip:id/rvHomePageIcon\"))"
                + ".scrollIntoView(new UiSelector().textMatches(\"Gift Cards\")"
                + ".instance(0));"));

结果:这不会滚动,但是在应用程序上单击“寄宿家庭”选项。

方法2:

WebElement eleOne = driver.findElement(By.xpath("//*[@text='Flights']"));
WebElement eleTwo = driver.findElement(By.xpath("//*[@text='Gift Cards']"));
TouchAction t = new TouchAction(driver);
t.longPress(longPressOptions().withElement(element(eleOne))
                .withDuration(ofSeconds(8))).moveTo(element(eleTwo))
                    .release().perform();

结果:由于eleTwo当前不在框架中,因此不会引发“找不到此类元素”异常。我试图调整此方法,并输入eleTwo作为元素,该元素在屏幕上可见,只是为了查看滚动是否起作用并且它确实起作用。但是我不知道如何处理屏幕上不可见的元素。

我想滚动顶部的选项列表,然后单击GiftCard,它是顶部小部件菜单上的最后一个选项。

我在Java-Client 7.3.0中使用AppiumDriver。

IntialPositionScrollingTillGiftCard

appium appium-android
1个回答
0
投票

您可以尝试使用uiAutomator2(将scrollable设置为true):

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 scrollHorizontally() {

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

我写了一个详细的答案来滚动使用不同的方法。您可以在这里查看:

How to reach the end of a scroll bar in appium?

© www.soinside.com 2019 - 2024. All rights reserved.