In Appium, How to Scroll and validate the required set of elements are available in the Android screen using a Java method?

Viewed 2083

In my Android app, I need to scroll down the screen and validate whether the required elements(each individual element) are available or not. How do I achieve this with a Java method?

3 Answers

Use the following code for scrolling up and down using JAVA.

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"**Text you want to Find**\"));");

Thanks.

The good way here is to use native UIAutomator find strategy:

public void isElementPresent(String resourceId) {
    return androidDriver.findElementsByAndroidUIAutomator("new UiScrollable(new UiSelector()
       .scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId("<your app package name>:id/" + resourceId + "\"))")
       .size() > 0; 
}

It will scroll down & up the view to find an element by provided resourceId and return true if element present.

Use the following code to scroll up using java :

public void swipeUp(AppiumDriver<?> appiumDriver, MobileElement fromPosition, MobileElement toPosition) {
        Dimension size = fromPosition.getSize();
        Dimension size1 = toPosition.getSize();
        TouchAction swipe = new TouchAction(appiumDriver)
                .press(ElementOption.element(fromPosition, size.width / 2, size.height - 20))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(4)))
                .moveTo(ElementOption.element(toPosition, size1.width / 2, size1.height / 2 + 30)).release();
        swipe.perform();
        logger.info("Swipe Success");
    }
Related