Swipe Functions for Native Mobile Applications

Swipe Right:

export async function swipeRight() {
    if (driver.isAndroid) {
        await $('android=new UiScrollable(new UiSelector()).setAsHorizontalList().scrollForward()');
    } else {
        const screenSize = await driver.getWindowRect();
        const startX = screenSize.width * 0.9;
        const endX = screenSize.width * 0.1;
        const y = screenSize.height / 2;

        await driver.execute('mobile: swipe', {
            direction: 'left',
            element: null,
            startX: startX,
            startY: y,
            endX: endX,
            endY: y,
            duration: 1000,
        });
    }
}
  • For Android devices, this function uses the UiScrollable class to scroll forward in a horizontal list. On iOS, it performs a swipe gesture from the right side of the screen to the left, allowing users to navigate to the previous content seamlessly.

Swipe Left:

export async function swipeLeft() {
    if (driver.isAndroid) {
        await $('android=new UiScrollable(new UiSelector()).setAsHorizontalList().scrollBackward()');
    } else {
        const screenSize = await driver.getWindowRect();
        const startX = screenSize.width * 0.9;
        const endX = screenSize.width * 0.1;
        const y = screenSize.height / 2;

        await driver.execute('mobile: swipe', {
            direction: 'right',
            element: null,
            startX: startX,
            startY: y,
            endX: endX,
            endY: y,
            duration: 1000,
        });
    }
}
  • This function for Android uses UiScrollable to scroll backward in a horizontal list. On iOS, it performs a swipe gesture from the left side of the screen to the right, allowing users to navigate to the next content smoothly.