javaseleniumappiumseetest

How to scroll up to a specific date in calendar in appium java


I'm scrolling up in list of year in calendar to select specific date like "1998". So how to scroll up to given year and click on that year?

This given code is scrolling up little bit and then scrolling down again two times. This image is shows latest calendar ui


        MobileElement element = (MobileElement) driver
                .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0))");
        element.click();

Solution

  • You can consider using mobile:shell command to continuously scroll up until the desired year appears on the screen, example code would be something like:

    Dimension windowSize = driver.manage().window().getSize();
    Map<String, Object> args = new HashMap<>();
    args.put("command", "input");
    args.put("args", Lists.newArrayList("swipe", windowSize.width / 2,
            windowSize.height / 2, windowSize.width / 2, windowSize.height));
    while (driver.findElements(By.xpath("//android.widget.TextView[@text='1998']")).size() == 0) {
        driver.executeScript("mobile: shell", args);
    }
    driver.findElement(By.xpath("//android.widget.TextView[@text='1998']")).click();
    

    Demo:

    enter image description here


    Even easier option would be going for SwipeWhileNotFound command available via SeeTest Appium Extension, it will shorten the code to one-liner:

    seetest.swipeWhileNotFound("Up", 0, 2000, "NATIVE", "xpath=//android.widget.TextView[@text='1998']", 0, 1000, 5, true)