Direct scroll on iOS devices

When creating an automation script in Appium 1.22.3 or later for an iOS device, you can achieve scrolling to a specific element on the screen by using a Perfecto extension to Appium's mobile:scroll command. The extension is an argument called direct that takes on a value of true. For an implementation example, see lines 6 and 7 in the following code snippet.

Restriction: This extension only works when the toVisible argument is set to true.
Copy
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

import java.util.HashMap;

public class ScrollExample {
    public static void performScroll(AppiumDriver driver) {
        // Find element using modern Selenium locator
        WebElement element = driver.findElement(By.xpath("//*[@label='test']"));

        // Cast to RemoteWebElement to get internal element ID
        RemoteWebElement remoteElement = (RemoteWebElement) element;
        String elementId = remoteElement.getId();

        // Build the scroll command parameters
        HashMap<String, Object> scrollParams = new HashMap<>();
        scrollParams.put("elementId", elementId);
        scrollParams.put("toVisible", true);
        scrollParams.put("direct", true);

        // Execute the Perfecto/Appium mobile scroll script command
        driver.executeScript("mobile: scroll", scrollParams);
    }
}