We often need to perform scrolling gestures in both Android and iOS devices while automating tests with Appium. Appium provides multiple ways to handle scrolling, but the available options differ slightly depending on the platform:
- Swipe / UiScrollable → These methods are specific to Android.
- TouchAction → Works on both Android and iOS, making it a cross-platform choice.
- Mobile Gestures (
mobile: scroll,mobile: swipe) → iOS-specific commands provided by the XCUITest driver.
Below, I’m sharing example scripts for different scrolling approaches in Appium with Selenium Java. These examples will help you understand how to implement scrolling depending on whether you are testing Android, iOS, or both.
Scrolling with Swipe:
This method works on Android devices and allows us to swipe in any direction by defining the start and end coordinates. The swipe is performed by simulating the movement of a virtual finger (similar to a mouse cursor) across the screen.
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.Dimension;
public class SwipeHelper {
// Generic swipe method
public static void swipe(AndroidDriver<MobileElement> driver, String direction) {
Dimension size = driver.manage().window().getSize();
int startX, endX, startY, endY;
switch (direction.toUpperCase()) {
case "UP":
startX = size.width / 2;
startY = (int) (size.height * 0.8);
endX = startX;
endY = (int) (size.height * 0.2);
driver.swipe(startX, startY, endX, endY, 1000);
break;
case "DOWN":
startX = size.width / 2;
startY = (int) (size.height * 0.2);
endX = startX;
endY = (int) (size.height * 0.8);
driver.swipe(startX, startY, endX, endY, 1000);
break;
case "LEFT":
startY = size.height / 2;
startX = (int) (size.width * 0.8);
endX = (int) (size.width * 0.2);
endY = startY;
driver.swipe(startX, startY, endX, endY, 1000);
break;
case "RIGHT":
startY = size.height / 2;
startX = (int) (size.width * 0.2);
endX = (int) (size.width * 0.8);
endY = startY;
driver.swipe(startX, startY, endX, endY, 1000);
break;
default:
throw new IllegalArgumentException("Invalid swipe direction: " + direction);
}
}
}
Usage for swipe:
AndroidDriver driver = // your driver initialization
// Swipe down
SwipeHelper.swipe(driver, “DOWN”);
// Swipe right
SwipeHelper.swipe(driver, “RIGHT”);
Scrolling with UiScrollable:
UiScrollable is a powerful option in Appium that is specifically designed for Android automation. It allows you to scroll inside scrollable views until the desired element is found. You can use it to scroll forward, backward, to the end, or directly into view of a matching element. We can scroll with UiScrollable using the following code:
MobileElement element = (MobileElement) driver.findElementByAndroidUIAutomator(
“new UiScrollable(new UiSelector().scrollable(true))” +
“.scrollIntoView(new UiSelector().text(\”Your Text Here\”));”
);
Scrolling with TouchAction: TouchAction is a versatile option in Appium that works on both Android and iOS. It simulates real touch gestures such as press, move, and release, making it useful for implementing scroll or swipe actions across different platforms.
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.Dimension;
import java.time.Duration;
public class SwipeHelper {
private static final int DURATION = 800; // swipe duration in ms
// 🔹 Generic swipe method
public static void swipe(AppiumDriver driver, String direction) {
Dimension size = driver.manage().window().getSize();
int startX, endX, startY, endY;
switch (direction.toUpperCase()) {
case "UP":
startX = size.width / 2;
startY = (int) (size.height * 0.8);
endX = startX;
endY = (int) (size.height * 0.2);
break;
case "DOWN":
startX = size.width / 2;
startY = (int) (size.height * 0.2);
endX = startX;
endY = (int) (size.height * 0.8);
break;
case "LEFT":
startY = size.height / 2;
startX = (int) (size.width * 0.8);
endX = (int) (size.width * 0.2);
endY = startY;
break;
case "RIGHT":
startY = size.height / 2;
startX = (int) (size.width * 0.2);
endX = (int) (size.width * 0.8);
endY = startY;
break;
default:
throw new IllegalArgumentException("Invalid direction: " + direction);
}
new TouchAction(driver)
.press(PointOption.point(startX, startY))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(DURATION)))
.moveTo(PointOption.point(endX, endY))
.release()
.perform();
}
// 🔹 Convenience methods
public static void swipeUp(AppiumDriver driver) { swipe(driver, "UP"); }
public static void swipeDown(AppiumDriver driver) { swipe(driver, "DOWN"); }
public static void swipeLeft(AppiumDriver driver) { swipe(driver, "LEFT"); }
public static void swipeRight(AppiumDriver driver){ swipe(driver, "RIGHT"); }
}
Usage for TouchAction:
SwipeHelper.swipeRight(driver);
// Swipe left
SwipeHelper.swipeLeft(driver);
// Swipe up
SwipeHelper.swipe(driver, “UP”);
Scrolling with MobileGestures: MobileGesture commands in Appium are especially useful for implementing scrolling on iOS devices. They provide direct access to native gestures like scroll and swipe through the XCUITest driver. This makes it easier to scroll in a particular direction or until a specific element becomes visible. Here is example script below with Mobile Gestures:
// Scroll down
HashMap params = new HashMap<>();
params.put(“direction”, “down”); // up, down, left, right
driver.executeScript(“mobile: scroll”, params);
// Scroll down until a specific element is visible
HashMap params = new HashMap<>();
params.put(“direction”, “down”);
params.put(“element”, ((RemoteWebElement) element).getId());
driver.executeScript(“mobile: scroll”, params);
… [Trackback]
[…] Read More here: testingtalkslatest.com/2025/09/10/how-to-scroll-in-appium-with-different-options/ […]