Automating Gestures

Reading Time : 42 min read
Automating gestures in mobile app testing

How to use touch actions in Appium

Up until now we have looked into basic Appium automation, such as finding and clicking on a button or typing text into a text field. However, “real world” mobile applications are more sophisticated and contain many complex UI elements that require user interactions such as double tap, long press, swipe left/right, pull up/down and even multi-touch actions.

Appium supports the following gestures:

  • Tap on an element.
  • Tap on x, y coordinates.
  • Press an element for a particular duration.
  • Press x, y coordinates for a particular duration.
  • Horizontal swipe: Using start and end percentage of the screen height and width.
  • Vertical swipe: Using start and end percentage of the screen height and width.
  • Drag(Swipe) one element to another element.
  • Multitouch for an element.

Appium supports these gestures using the TouchActions class.

TouchAction touchAction = new TouchAction(driver);

Some of the supported methods are:

Method NamePurpose
Press(PointOption pressOptions)Press action on the screen.
LongPress(LongPressOptions longPressOptions)Press and hold the at the center of an element until the context menu event has fired.
Tap(PointOption tapOptions)Tap on a position.
MoveTo(PointOption moveToOptions)Moves current touch to a new position.
cancel()
Moves current touch to a new position.
perform()Perform this chain of actions on the performsTouchActions.

Official Appium API docs: Click Here.!

Before exploring each mentioned action we need to understand the significance of perform() as it plays a vital role. The Appium client simply records all the instructions and actions on the client side and stores the intermediate values in a local data structure. The perform() method is used to send all actions to the appium server – ss soon as perform() is called, the intermediate actions and instructions are converted to JSON and sent to the appium server, and then actual action is being performed. So for any gesture code the last method called would be perform()

Note: This is a common omission during development, forgetting to call perform() and wondering why your test isn’t working!

Appium fully supports native applications. So if the application is designed natively for the platform (iOS or Android), then you can easily find the unique selectors for automation, but there are case when you use cross-platform development technologies such as react native, ionic or xamarin framework. In this instance, sometime elements are not present for a particular screen or even a whole application.

For example, most of games are coded using the Unity3D platform rather than native coding, so there would not be a single element that can be located by any tool or even by the appium inspector. However we are not talking about game automation right now.

The main takeaway here is that if you are not able to get the selector for any element for any reason then only one survival option remains. Which is to get the x, y coordinates for that element.

NOTE: Please remember that you can only click on that element using appium.

Now the question is how can you get the x,y coordinate?

  • It depends…
  • Because you can get the Pointer location in Android but you can not get it in iOS devices.

Getting the pointer location in Android:

  1. Move to Settings > Developer options
  2. Enable the Pointer location.
  3. Now move to any application for which you need the coordinates of a particular location. Tap on the location and you will get the coordinations for that place at top of the screen.
Appium eBook
Figure-1: Enable the Pointer location.
Appium eBook
Figure-2: Get coordinates.

Getting the pointer location in iOS:

iOS does not support the pointer location and there aren’t even any third party apps or tools which come to the rescue. Therefore you need to calculate it using screen resolution and a little bit of prediction. In case you don’t get success at first, you can use trial and error to get the needed location.

Now let’s look into each gesture one by one:

Appium gesture builder
Build a touch gesture, watch it run, copy the test
Pick the platform, the gesture and the screen, then set how far and how fast the finger moves. The phone replays it, and the script below is written with the W3C Actions API and the platform’s own mobile: commands, the two ways Appium 2 expects gestures to be driven.
Platform
Sizes are in physical pixels, the unit Pointer location shows on the device.Sizes are in points. The driver reports the logical size, not the pixel panel.
Gesture
Pointer down, a few milliseconds, pointer up. The gesture every other one is built from.Two taps inside the system double-tap window. The gap is what the platform measures.Pointer down, hold, pointer up. The hold has to clear the platform’s long-press threshold.Press, move across the screen over a set time, release. Fast reads as a fling, slow as a drag.Press and hold to pick up, move to the target, release. The hold before moving is what makes it a drag.Two pointers moving at the same time. Needs two sequences performed together, never one after the other.
Screen
Pixel 8 class. The size most Android test farms default to.Entry-level handset. The one that exposes hard-coded coordinates.iPhone 15 Pro class, reported in points: the panel is 1179 × 2556 px.iPhone SE. Short screen, so bottom-anchored taps land on the home indicator.
Target the gesture by
Elements are the reliable choice. Fall back to coordinates for Unity, canvas or web views where nothing has a locator.
Finger moves
Finger up scrolls the content down. Travels from 80% to 20% of the height, x fixed at 50%.Finger down scrolls the content up, or pulls to refresh at the top. 20% to 80% of the height.Finger left reveals what sits to the right: the next card, the next tab. 80% to 20% of the width.Finger right goes back or reveals the previous page. 20% to 80% of the width.
Zoom
Fingers start 10% of the width apart and end 60% apart, around the centre.Fingers start 60% of the width apart and end 10% apart, around the centre.
Press durationGap between tapsHold durationMove durationMove durationMove duration
Keep it under 100 ms. Longer and Android may treat it as a long press.Android accepts up to about 300 ms between taps; iOS is similar. 100 to 200 ms is safe.Android’s default long-press timeout is 400 ms, iOS around 500 ms. 1,000 ms clears both with margin.Under 300 ms flings and keeps scrolling after release. 500 to 800 ms gives a controlled, measurable scroll.The script adds a 600 ms hold before the move so the item is picked up rather than scrolled past.Both fingers share the same duration. Slow pinches are easier for a map or gallery to track.
Gesture preview TapDouble tapLong pressSwipe / scrollDrag and dropPinch / zoom
screen1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 ptpoint540360196188, 1200800426334 (50%, 50%)elementadd_to_cartaddToCartpress duration50 ms80 ms120 mspoint540360196188, 1200800426334 (50%, 50%)elementadd_to_cartaddToCartgap between taps100 ms150 ms250 mspoint540360196188, 1200800426334 (50%, 50%)elementadd_to_cartaddToCarthold duration500 ms1000 ms2000 msstart540360196188, 19201280682534 (50%, 80%)end540360196188, 480320170133 (50%, 20%)start540360196188, 480320170133 (50%, 20%)end540360196188, 19201280682534 (50%, 80%)start864576314300, 1200800426334 (80%, 50%)end2161447975, 1200800426334 (20%, 50%)start2161447975, 1200800426334 (20%, 50%)end864576314300, 1200800426334 (80%, 50%)move duration300 ms600 ms1200 mspick up2701809894, 840560298233drop810540295281, 18001200639500elementadd_to_cartaddToCartdrop oncart_drop_zonecartDropZonemove duration300 ms600 ms1200 mscentre540360196188, 1200800426334finger 1486324177169 → 2161447975finger 2594396216206 → 864576314300finger 12161447975 → 486324177169finger 2864576314300 → 594396216206move duration300 ms600 ms1200 ms
UiAutomator2 resolves the point in physical pixels, the same unit Pointer location reports on the device.XCUITest resolves the point in screen points. If you measured on a pixel ruler, divide by the device scale factor first.Inside the double-tap window on both platforms. Expect the zoom or like action, not two selections.Inside the double-tap window on both platforms. Expect the zoom or like action, not two selections.Inside the double-tap window on both platforms. Expect the zoom or like action, not two selections.Just over the iOS threshold, with little margin. Use it only if the app sets its own shorter timeout.Comfortably past both platforms’ long-press thresholds. The context menu or edit mode should be open by the time the pointer lifts.Comfortably past both platforms’ long-press thresholds. The context menu or edit mode should be open by the time the pointer lifts.Sixty percent of the screen in 300 ms is fling territory: the list keeps moving after release, so the next assertion may run on a moving target.Sixty percent of the screen in 600 ms reads as a controlled scroll on both platforms, which is what you want before asserting on what became visible.A slow, deliberate scroll. Nothing keeps moving after release, at the cost of a slower test.The pointer holds for 600 ms before moving so the OS enters drag mode, then moves over 3006001200 ms and releases on the target.Both pointers press at the same time, move together over 3006001200 ms and lift together. Performed as one call with two sequences, so the server interleaves them.
Recommended automation steps 6 steps · 6 steps · 6 steps · 7 steps · 7 steps · 7 steps · UiAutomator2XCUITest
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.Locate the element first. AppiumBy.accessibilityId("add_to_cartaddToCart") on UiAutomator2XCUITest resolves to its centre, so the gesture lands correctly on any screen size.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.Wait until it is displayed, not merely present. Gestures on an element that is still animating in are the most common flaky step.
  3. Pointer down, pause 5080120 ms, pointer up. Keep the pause short so the platform does not promote it to a long press.
  4. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  5. Assert the outcome, not the gesture: the screen or state the tap should produce is visible.
  6. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.Locate the element first. AppiumBy.accessibilityId("add_to_cartaddToCart") on UiAutomator2XCUITest resolves to its centre, so the gesture lands correctly on any screen size.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.Wait until it is displayed, not merely present. Gestures on an element that is still animating in are the most common flaky step.
  3. Two down/up pairs with a 100150250 ms pause between them in a single sequence. Two separate perform() calls add transport latency and can fall outside the double-tap window.
  4. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  5. Assert the outcome, not the gesture: the zoom level, selection or like state changed.
  6. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.Locate the element first. AppiumBy.accessibilityId("add_to_cartaddToCart") on UiAutomator2XCUITest resolves to its centre, so the gesture lands correctly on any screen size.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.Wait until it is displayed, not merely present. Gestures on an element that is still animating in are the most common flaky step.
  3. Pointer down, hold 50010002000 ms, pointer up. The hold is a Pause action inside the sequence, not a Thread.sleep in the test.
  4. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  5. Assert the outcome, not the gesture: the context menu, edit mode or tooltip appeared.
  6. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.
  3. Press at the start point, hold about 100 ms, move to the end point over 3006001200 ms, release. One finger, one sequence.
  4. Keep the other axis constant. X stays at 50% of the width so the gesture is a clean scroll, not a diagonal.X stays at 50% of the width so the gesture is a clean scroll, not a diagonal.Y stays at 50% of the height so the gesture is a clean swipe, not a diagonal.Y stays at 50% of the height so the gesture is a clean swipe, not a diagonal.
  5. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  6. Assert the outcome, not the gesture: an element that was off screen is now displayed, or the first visible item changed.
  7. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.Locate the element first. AppiumBy.accessibilityId("add_to_cartaddToCart") on UiAutomator2XCUITest resolves to its centre, so the gesture lands correctly on any screen size.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.Wait until it is displayed, not merely present. Gestures on an element that is still animating in are the most common flaky step.
  3. Press, hold about 600 ms, move over 3006001200 ms, release on the target. The hold is what turns a swipe into a pick-up.
  4. Move in small steps if the app uses a drop zone. Some drag implementations only fire enter/hover events when the pointer crosses the boundary, so a single long move can skip them.
  5. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  6. Assert the outcome, not the gesture: the item is now a child of the drop target, or the order changed.
  7. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
  1. Read the screen size at run time with driver.manage().window().getSize() and express every point as a fraction of it. This 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt layout is only one device.
  2. Confirm the point is inside the right surface. Overlay the fractions on a screenshot from the target device before trusting them, especially near the status bar, notch or home indicator.
  3. Create two pointer inputs (finger1 and finger2), each with its own sequence: move to start, down, pause, move to end over 3006001200 ms, up.
  4. Perform both sequences in one call. The server interleaves them so the fingers move together. Performing them one after the other produces two swipes.
  5. Call perform(). Until then nothing has been sent to the server; this is the single most common reason a gesture test “does nothing”.
  6. Assert the outcome, not the gesture: the content scale, map zoom level or image size changed.
  7. Run the same test on a second device profile with a different resolution and aspect ratio with a different point size (an SE and a Pro Max) before merging. If it passes only on one, a coordinate assumption is hiding somewhere.
Example script
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.width  * 0.50);  // 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.50);  // 1200800426334

// Locate the element; origin fromElement() means offsets are measured from its centre
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence tap = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.fromElement(el), 0, 0))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(5080120)))
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(List.of(tap));  // nothing is sent until perform()
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
from appium.webdriver.common.appiumby import AppiumBy

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
start_x = int(size["width"] * 0.50)  # 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.50)  # 1200800426334

# Locate the element; move_to(element) targets its centre on any screen
el = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "add_to_cartaddToCart")

finger = PointerInput(interaction.POINTER_TOUCH, "finger1")
actions = ActionBuilder(driver, mouse=finger)
actions.pointer_action.move_to_location(start_x, start_y)
actions.pointer_action.move_to(el)
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.050.080.12)
actions.pointer_action.pointer_up()

actions.perform()  # nothing is sent until perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));
String elId = ((RemoteWebElement) el).getId();

driver.executeScript("mobile: clickGesture", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId));
driver.executeScript("mobile: tap", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId));
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.width  * 0.50);  // 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.50);  // 1200800426334

// Locate the element; origin fromElement() means offsets are measured from its centre
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence doubleTap = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.fromElement(el), 0, 0))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(50)))
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(100150250)))  // gap between the two taps
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(50)))
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(List.of(doubleTap));  // nothing is sent until perform()
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
from appium.webdriver.common.appiumby import AppiumBy

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
start_x = int(size["width"] * 0.50)  # 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.50)  # 1200800426334

# Locate the element; move_to(element) targets its centre on any screen
el = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "add_to_cartaddToCart")

finger = PointerInput(interaction.POINTER_TOUCH, "finger1")
actions = ActionBuilder(driver, mouse=finger)
actions.pointer_action.move_to_location(start_x, start_y)
actions.pointer_action.move_to(el)
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.05)
actions.pointer_action.pointer_up()
actions.pointer_action.pause(0.10.150.25)  # gap between taps
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.05)
actions.pointer_action.pointer_up()

actions.perform()  # nothing is sent until perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));
String elId = ((RemoteWebElement) el).getId();

driver.executeScript("mobile: doubleClickGesture", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId));
driver.executeScript("mobile: doubleTap", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId));
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.width  * 0.50);  // 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.50);  // 1200800426334

// Locate the element; origin fromElement() means offsets are measured from its centre
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence longPress = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.fromElement(el), 0, 0))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(50010002000)))  // hold
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(List.of(longPress));  // nothing is sent until perform()
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
from appium.webdriver.common.appiumby import AppiumBy

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
start_x = int(size["width"] * 0.50)  # 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.50)  # 1200800426334

# Locate the element; move_to(element) targets its centre on any screen
el = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "add_to_cartaddToCart")

finger = PointerInput(interaction.POINTER_TOUCH, "finger1")
actions = ActionBuilder(driver, mouse=finger)
actions.pointer_action.move_to_location(start_x, start_y)
actions.pointer_action.move_to(el)
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.51.02.0)  # hold, in seconds
actions.pointer_action.pointer_up()

actions.perform()  # nothing is sent until perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));
String elId = ((RemoteWebElement) el).getId();

driver.executeScript("mobile: longClickGesture", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId, "duration", 50010002000));  // duration in ms
driver.executeScript("mobile: touchAndHold", Map.of("x", 540360196188, "y", 1200800426334"elementId", elId, "duration", 0.51.02.0));  // duration in SECONDS on iOS
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.width  * 0.50);  // 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.80);  // 19201280682534
int endX   = (int) (size.width  * 0.50);  // 540360196188
int endY   = (int) (size.height * 0.20);  // 480320170133
int startX = (int) (size.width  * 0.50);  // 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.20);  // 480320170133
int endX   = (int) (size.width  * 0.50);  // 540360196188
int endY   = (int) (size.height * 0.80);  // 19201280682534
int startX = (int) (size.width  * 0.80);  // 864576314300 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.50);  // 1200800426334
int endX   = (int) (size.width  * 0.20);  // 2161447975
int endY   = (int) (size.height * 0.50);  // 1200800426334
int startX = (int) (size.width  * 0.20);  // 2161447975 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.50);  // 1200800426334
int endX   = (int) (size.width  * 0.80);  // 864576314300
int endY   = (int) (size.height * 0.50);  // 1200800426334

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence swipe = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(100)))  // short hold so it is not read as a fling
    .addAction(finger.createPointerMove(Duration.ofMillis(3006001200), PointerInput.Origin.viewport(), endX, endY))  // move to the end point
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(List.of(swipe));  // nothing is sent until perform()
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
start_x = int(size["width"] * 0.50)  # 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.80)  # 19201280682534
end_x   = int(size["width"] * 0.50)  # 540360196188
end_y   = int(size["height"] * 0.20)  # 480320170133
start_x = int(size["width"] * 0.50)  # 540360196188 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.20)  # 480320170133
end_x   = int(size["width"] * 0.50)  # 540360196188
end_y   = int(size["height"] * 0.80)  # 19201280682534
start_x = int(size["width"] * 0.80)  # 864576314300 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.50)  # 1200800426334
end_x   = int(size["width"] * 0.20)  # 2161447975
end_y   = int(size["height"] * 0.50)  # 1200800426334
start_x = int(size["width"] * 0.20)  # 2161447975 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.50)  # 1200800426334
end_x   = int(size["width"] * 0.80)  # 864576314300
end_y   = int(size["height"] * 0.50)  # 1200800426334

finger = PointerInput(interaction.POINTER_TOUCH, "finger1")
actions = ActionBuilder(driver, mouse=finger)
actions.pointer_action.move_to_location(start_x, start_y)
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.1)  # short hold so it is not read as a fling
actions.pointer_action.move_to_location(end_x, end_y, duration=3006001200)  # move duration in ms
actions.pointer_action.pointer_up()

actions.perform()  # nothing is sent until perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
// Swipe inside a bounding box; "percent" is how much of that box the finger travels (0..1).
driver.executeScript("mobile: swipeGesture", Map.of(
    "left", 0, "top", 0, "width", 1080720393375, "height", 24001600852667,
    "direction", "updownleftright",        // the direction the finger moves
    "percent", 0.60
));
// Coordinate-precise swipe. "duration" is how long the finger presses BEFORE it starts moving, in seconds.
driver.executeScript("mobile: dragFromToForDuration", Map.of(
    "duration", 0.1,
    "fromX", 540360196188, "fromY", 19201280682534,
    "toX", 540360196188, "toY", 480320170133
    "fromX", 540360196188, "fromY", 480320170133,
    "toX", 540360196188, "toY", 19201280682534
    "fromX", 864576314300, "fromY", 1200800426334,
    "toX", 2161447975, "toY", 1200800426334
    "fromX", 2161447975, "fromY", 1200800426334,
    "toX", 864576314300, "toY", 1200800426334
));

// Or let XCUITest pick the distance for a simple directional swipe over the whole app:
// driver.executeScript("mobile: swipe", Map.of("direction", "updownleftright"));
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.width  * 0.25);  // 2701809894 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int startY = (int) (size.height * 0.35);  // 840560298233
int endX   = (int) (size.width  * 0.75);  // 810540295281
int endY   = (int) (size.height * 0.75);  // 18001200639500

// Locate the element; origin fromElement() means offsets are measured from its centre
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));
WebElement target = driver.findElement(AppiumBy.accessibilityId("cart_drop_zonecartDropZone"));

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence dragAndDrop = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.fromElement(el), 0, 0))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger, Duration.ofMillis(600)))  // long hold: pick the item up
    .addAction(finger.createPointerMove(Duration.ofMillis(3006001200), PointerInput.Origin.viewport(), endX, endY))  // move to the target
    .addAction(finger.createPointerMove(Duration.ofMillis(3006001200), PointerInput.Origin.fromElement(target), 0, 0))  // move to the target
    .addAction(new Pause(finger, Duration.ofMillis(150)))  // let the drop zone register hover
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(List.of(dragAndDrop));  // nothing is sent until perform()
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
from appium.webdriver.common.appiumby import AppiumBy

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
start_x = int(size["width"] * 0.25)  # 2701809894 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
start_y = int(size["height"] * 0.35)  # 840560298233
end_x   = int(size["width"] * 0.75)  # 810540295281
end_y   = int(size["height"] * 0.75)  # 18001200639500

# Locate the element; move_to(element) targets its centre on any screen
el = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "add_to_cartaddToCart")
target = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "cart_drop_zonecartDropZone")

finger = PointerInput(interaction.POINTER_TOUCH, "finger1")
actions = ActionBuilder(driver, mouse=finger)
actions.pointer_action.move_to_location(start_x, start_y)
actions.pointer_action.move_to(el)
actions.pointer_action.pointer_down()
actions.pointer_action.pause(0.6)  # long hold: pick the item up
actions.pointer_action.move_to_location(end_x, end_y, duration=3006001200)  # move to the target
actions.pointer_action.move_to(target, duration=3006001200)  # move to the target
actions.pointer_action.pause(0.15)  # let the drop zone register hover
actions.pointer_action.pointer_up()

actions.perform()  # nothing is sent until perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
WebElement el = driver.findElement(AppiumBy.accessibilityId("add_to_cartaddToCart"));
String elId = ((RemoteWebElement) el).getId();
WebElement target = driver.findElement(AppiumBy.accessibilityId("cart_drop_zonecartDropZone"));
int endX = target.getLocation().x + target.getSize().width / 2;
int endY = target.getLocation().y + target.getSize().height / 2;

driver.executeScript("mobile: dragGesture", Map.of(
    "startX", 2701809894, "startY", 840560298233,
    "endX", 810540295281, "endY", 18001200639500
    "elementId", elId,
    "endX", endX, "endY", endY
));
driver.executeScript("mobile: dragFromToForDuration", Map.of(
    "duration", 0.6,   // press this long before moving, so the item is picked up
    "fromX", 2701809894, "fromY", 840560298233,
    "toX", 810540295281, "toY", 18001200639500
    "fromX", el.getLocation().x + el.getSize().width / 2, "fromY", el.getLocation().y + el.getSize().height / 2,
    "toX", endX, "toY", endY
));
// Appium java-client 8+ / Selenium 4 — W3C Actions.
// TouchAction and MultiTouchAction were removed in java-client 9; this works on Appium 2 servers.
import org.openqa.selenium.Dimension;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.List;

// Read the size at run time so the same test survives a device swap
Dimension size = driver.manage().window().getSize();
int centerY  = (int) (size.height * 0.50);  // 1200800426334 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
int f1StartX = (int) (size.width * 0.45);  // 486324177169
int f1EndX   = (int) (size.width * 0.20);  // 2161447975
int f2StartX = (int) (size.width * 0.55);  // 594396216206
int f2EndX   = (int) (size.width * 0.80);  // 864576314300
int f1StartX = (int) (size.width * 0.20);  // 2161447975
int f1EndX   = (int) (size.width * 0.45);  // 486324177169
int f2StartX = (int) (size.width * 0.80);  // 864576314300
int f2EndX   = (int) (size.width * 0.55);  // 594396216206

PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
PointerInput finger2 = new PointerInput(PointerInput.Kind.TOUCH, "finger2");

Sequence finger1Seq = new Sequence(finger1, 1)
    .addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), f1StartX, centerY))
    .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger1, Duration.ofMillis(100)))
    .addAction(finger1.createPointerMove(Duration.ofMillis(3006001200), PointerInput.Origin.viewport(), f1EndX, centerY))
    .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

Sequence finger2Seq = new Sequence(finger2, 1)
    .addAction(finger2.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), f2StartX, centerY))
    .addAction(finger2.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(new Pause(finger2, Duration.ofMillis(100)))
    .addAction(finger2.createPointerMove(Duration.ofMillis(3006001200), PointerInput.Origin.viewport(), f2EndX, centerY))
    .addAction(finger2.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

// Both sequences in ONE perform() call so the fingers move together — zoom inzoom out
driver.perform(List.of(finger1Seq, finger2Seq));
# Appium-Python-Client 3+ / Selenium 4 — W3C Actions (TouchAction is deprecated).
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.pointer_actions import PointerActions

# Read the size at run time so the same test survives a device swap
size = driver.get_window_size()
center_y   = int(size["height"] * 0.50)  # 1200800426334 pxpt on this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen
f1_start_x = int(size["width"] * 0.45)  # 486324177169
f1_end_x   = int(size["width"] * 0.20)  # 2161447975
f2_start_x = int(size["width"] * 0.55)  # 594396216206
f2_end_x   = int(size["width"] * 0.80)  # 864576314300
f1_start_x = int(size["width"] * 0.20)  # 2161447975
f1_end_x   = int(size["width"] * 0.45)  # 486324177169
f2_start_x = int(size["width"] * 0.80)  # 864576314300
f2_end_x   = int(size["width"] * 0.55)  # 594396216206

actions = ActionBuilder(driver)
finger1 = PointerActions(actions.add_pointer_input(interaction.POINTER_TOUCH, "finger1"))
finger2 = PointerActions(actions.add_pointer_input(interaction.POINTER_TOUCH, "finger2"))

for finger, start_x, end_x in ((finger1, f1_start_x, f1_end_x), (finger2, f2_start_x, f2_end_x)):
    finger.move_to_location(start_x, center_y)
    finger.pointer_down()
    finger.pause(0.1)
    finger.move_to_location(end_x, center_y, duration=3006001200)
    finger.pointer_up()

# One perform() for both fingers so they move together — zoom inzoom out
actions.perform()
// Driver-level gesture via executeScript. Faster to write than W3C Actions, but UiAutomator2XCUITest-only.
import java.util.Map;

// Values resolved for this 1080 × 2400 px720 × 1600 px393 × 852 pt375 × 667 pt screen. In a real test derive them from driver.manage().window().getSize().
// Pinch inside a bounding box around the centre; "percent" is the fraction of the box the fingers travel.
driver.executeScript("mobile: pinchOpenGesturepinchCloseGesture", Map.of(
    "left", 2161447975, "top", 876584308221, "width", 648432236225, "height", 648432236225,
    "percent", 0.75
));
// scale > 1 zooms in (fingers open), scale < 1 zooms out. Applies to the app element unless "elementId" is given.
driver.executeScript("mobile: pinch", Map.of(
    "scale", 2.00.5,
    "velocity", 1.0-1.0  // positive opens, negative closes
));
The chain is recorded on the client and converted to JSON only when perform() runs. Forgetting it is still the number one reason a gesture test passes without doing anything.Pauses are in seconds, move durations in milliseconds: that mismatch inside the Selenium Python bindings catches most people once.UiAutomator2 mobile: gestures take milliseconds and physical pixels, and accept an elementId in place of a point. The W3C tabs are portable across both platforms; this one is not.XCUITest mobile: commands take seconds, not milliseconds, and screen points, not pixels. The W3C tabs are portable across both platforms; this one is not.
Getting coordinates on a real device
Open Settings, Developer options, and turn on Pointer location. A bar at the top of the screen reports the x, y of every touch.
Appium reports the size in physical pixels. The same percentages give the same spot on a 720-wide and a 1440-wide handset, so write tests in percentages of the runtime size.
The UiAutomator2 driver’s mobile: gestures accept an elementId instead of a point. Prefer that whenever a locator exists.
iOS has no pointer location overlay. Take a screenshot, open it in Preview or any editor that shows the cursor position, and read the point you need.
XCUITest reports points, not pixels. An iPhone 15 Pro with a 1179 × 2556 px panel reports 393 × 852. Divide by the scale factor (2× or 3×) when converting from a pixel ruler.
Appium Inspector shows the bounds of every element. Tap an element in the tree to read its x, y, width and height without guessing.
Coordinates are the last resort. They break on the next device, the next OS keyboard, and the next design tweak. Use them for canvas, game and chart surfaces where nothing is addressable.
Things that silently break gesture tests
TouchAction and MultiTouchAction are gone from Appium java-client 9 and the W3C protocol. Code that compiles against an old client stops working on Appium 2 servers.
Nothing happens until perform() is called. Actions are queued on the client and only sent to the server at the end of the chain.
A pointer down with no pause before the move is often read as a fling rather than a drag. Add a short hold before moving, longer for drag and drop.
Hard-coded 1080 × 1920 coordinates land in the wrong place on a notched 1080 × 2400 screen. Compute from driver.manage().window().getSize() at run time.
Emulators report pointer events, not touch pressure, velocity or palm rejection. Gestures that pass on an emulator still fail on real glass, so run the final pass on physical devices.
Appium 2 java-client 9 Appium-Python-Client 3 W3C Actions
Scripts are starting points. Resolve sizes at run time and assert the result of every gesture.

1. Tap on element

  • Method: tap(TapOptions tapOptions)
  • Usage: It is the simplest action, as the name suggests it will simply click/tap on a particular location. It is a combination of press() and release()
  • Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(tapOptions()
.withElement(element(androidElement)))
.perform()

NOTE: Here you can also put the wait along with the tap action, for example:

new TouchAction(driver)					        
.tap(tapOptions().withElement(element(androidElement)))
.waitAction(waitOptions(Duration.ofMillis(millis)))
.perform();

2. Tap on x, y coordinates:

  • Method: tap(PointOption pointOptions)
  • Usage: It is used to tap on a particular x,y coordinate point.
  • Example:
 TouchAction touchAction = new TouchAction(driver);
 touchAction.tap(PointOption.point(1280, 1013))
 .perform();

NOTE: Similar like Tap on element you can put the wait along with the tap action, for example:

 new TouchAction(driver)
.tap(tapOptions().withElement(element(androidElement)))
.waitAction(waitOptions(Duration.ofMillis(millis)))
.perform();

3. Press an element for a particular duration.

  • Method: press(PointOption pressOptions)
  • Usage: It is used to apply the press action. After the press action you also need to release so that the state would be in press mode. You do so by calling the release() function after calling press().
  • Example:
 TouchAction touchAction = new TouchAction(driver);
 touchAction.press(element(element))
.waitAction(waitOptions(ofSeconds(seconds)))
.release()
.perform();

4. Press x, y coordinates for a particular duration.

  • Method: press(PointOption pressOptions)
  • Usage: It is used to apply the press action. After the press action you also need to release so that the state would be in press mode. You do so by calling the release() function after calling press().
  • Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.press(point(x,y))
.waitAction(waitOptions(ofSeconds(seconds)))
.release()
.perform();

Automating Swipe Actions in Appium

Before we look into the Horizontal swipe let’s understand how we can automate swipe actions generally.

Swiping is a combination of tapping + moving actions. Appium does not provide a direct method for swiping, so you need to combine a few methods in order to achieve swiping. For example if you want to perform swiping then first you have to press on a particular point and then specify the particular amount of time during which you want to perform the swiping action and at last you to move to another point – and don’t forget to call the release method which used to release all the actions. So it’s actually simple: first press -> wait(duration of swiping) -> move to (moveTo()) particular location.

You might be thinking why can’t we directly use moveTo() ?

If you recall, using the press method requires you to eventually call the release method. So we are basically mimicking a swipe by entering the press state, moving to a location, and THEN releasing.

Swiping can have an up/down/right/left direction so you need to apply the right logic and have to provide the x, y coordinates for the press() and moveTo() methods.

And also please note these appium methods to get the device screen measurements:

Annotated screenshot of a mobile application screen illustrating screen coordinate measurements for test automation. Red arrows indicate the device's full screen dimensions of 1080 pixels wide and 1920 pixels high. Additional markings highlight the midpoint at 50% of the screen width and height, corresponding to coordinates X=540 and Y=960. Green arrows show the X and Y coordinate axes, while the top overlay displays real-time touch coordinates and gesture data. The diagram demonstrates how screen coordinates are calculated and used for tap, swipe, and gesture automation on mobile devices.
Figure-3: Screen Measurements.
int heightOfScreen = driver.manage().window().getSize().getHeight();
int widthOfScreen = driver.manage().window().getSize().getWidth();
int middleHeightOfScreen = heightOfScreen/2;
// To get 50% of width
int x = widthOfScreen * 0.5;
// To get 50% of height
int y = heightOfScreen * 0.5;

Here Width → X and Height → Y coordinates.

Now let’s look into each swiping type.

5. Horizontal swipe: Using start and end percentage of the screen height and width.

  • Method and Usage: As we discussed above there is no particular method for Horizontal swipe, and you need to perform the combination of press()->wait()->moveTo(). The moveTo() method is new to us – it is used to move to particular location. Its syntax is: moveTo(PointOption pressOptions)The secret to moveTo lies in the coordinates – you need to mention the starting and ending x,y coordinates in such a way that swiping can done from left → right OR right → left direction. Note that when we say swipe right, we mean moving the content from right to left, but the physical gesture is is moving to the left. See figure 4 below for clarification.Example: Let say we want to swipe right on the screen, so in practice you need to press on the right side and, without taking your finger off of the device, move your finger to left side. So we need to move from in the Right to Left direction in order to make the Right Swipe.Referring to figure 4 below, we have the swiping UI element placed on the screen from location (0,360) to (1080, 780). Now in order to attempt swipe in the right direction you have to first press anywhere in swipe area, for example let’s say (972, 500), and now without taking away the press action you need to move to left side suppose (108, 500) [Please note that Y coordinate is constant as we just need to change the X coordinates for swiping]. At that point we have achieved the swipe and now we can able to release the action and at last call the perform method to send all commands to the Appium server to perform on the UI.
Appium eBook
Figure-4: Swiping in action.

This is just one scenario for achieving a swipe gesture. Ideally first we need to make a decision as to what is the “right side of the screen”. We do this by considering 90% of the screen width. For example, if the screen resolution is 1920 x 1080, 1080 is the width of Screen and 90% of that width would equals to 972, so we have got our X coordinate for what we consider the “Right” side. In a similar manner we will need the X coordinate for the Left side and this time we can consider 10% of the width which would give us an X coordinate of about 108. So we have got X coordinates for Left and Right direction. For the Y coordinate we can choose any value as long as it falls in the swiping area – for example, let’s say our swiping area is between (0, 360) to (1080, 780), so you can choose any value for the Y coordinate in between 360 to 780.

NOTE: It is important that the Y coordinate have same value because we are focusing on only swiping (not scrolling) so only the X coordinate will change during the process and Y will remain constant. Ideally you should choose the half height of swiping area for Y coordinate.

Finally, you can perform the swipe gesture:

TouchAction swipe = new TouchAction(driver)
.press(PointOption.point(972,500))
.waitAction(waitOptions(ofMillis(800)))
.moveTo(PointOption.point(108,500))
.release()
.perform();

Same way if you want to swipe in the left direction you have to first press on the left side and move to the right side.

NOTE: This method works in a similar way on Android and iOS but the location differs according to Mobile device being used (given the swipe is dependent on coordinates which is dependent on screen resolution). Moreover on the iOS side you can’t find the location directly so you will need to use a trial and error approach.

6. Vertical swipe(scroll): Using start and end percentage of the screen height and width.

  • Usage: Scroll is the same as swipe but the direction is different. In swiping we are dealing with horizontal direction where as in scrolling we are dealing with vertical direction – but the rest of the logic will remain the same.Scrolling can done in the up → down OR down → up direction.Example: Let’s look at scrolling Down. On a mobile device in order to scroll down on screen we swipe in the “down to up” direction. It’s actually 3 steps we need to complete:
  1. Find the swiping area.
    • Starting point = (0,360)
    • Starting point = (0,360)
  1. Mark the scrolling points (We will use the height from scrolling area only. As per below image, the scrolling area is starting from approximately 30% of the screen height and ending at the end of the screen).
    • Down area point:
      1. X = Middle of Screen= 0.5 x 1080 = 540 (This will be same for starting and ending location)
      2. Y = 95% height of Screen = 0.95 x 1920 = 1824.
      3. Location = (540,1824)
    • Up area point:
      1. X = Middle of Screen=540
      2. Y = 35% height of Screen = 0.35 x 1920 = 672(Percentage value must be >30%).
      3. Location = (540,672)
  1. Perform scroll action using Appium.
TouchAction swipe = new TouchAction(driver)
.press(PointOption.point(540,1824))
.waitAction(waitOptions(ofMillis(800)))
.moveTo(PointOption.point(540,672))
.release()
.perform();
Appium eBook
Figure-5: Scrolling in action.

Scroll up will work the same but with different location points.

7. Drag(Swipe) one element to an another element.

  • Dragging one element to another element is one kind of swiping action.But here location in coordinates would not matter as we have both of the elements(1. Element which needs to be dragged, 2. Element upon which another element will be dragged).
TouchAction swipe = new TouchAction(driver)
.press(ElementOption.element(element1))
.waitAction(waitOptions(ofSeconds(2)))
.moveTo(ElementOption.element(element2))
.release()
.perform();

8. MultiTouch.

As the name suggests it means multiple touches happening at the same time.For example on iOS if you want to move to the Main screen, you need to use 5 fingers and do a swipe.Multi Touch is handled by MultiTouchAction class. It has a add(TouchActions touchActions) method so in which we need to pass a TouchActions object.So let say you want to press on 5 different points at a time then first you need to create 5 TouchActions, but here the important thing is we are not having a perform method at the end. We just need to call the release method for the TouchAction object, and then pass those values into the add method of the MultiTouchAction class.You can perform Multi Touch for:

  1. Multiple touches at a time.
TouchAction touchActionOne = new TouchAction();
touchActionOne.press(PointOption.point(100, 100));
touchActionOne.release();
TouchAction touchActionTwo = new TouchAction();
touchActionTwo.press(PointOption.point(200, 200));
touchActionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(touchActionOne);
action.add(touchActionTwo);
action.perform();
  • OR if you want to perform multi touch on particular elements then use below code snippet.
TouchAction touchAction1 = new TouchAction(driver)
.tap(ElementOption.element(e1))
.release();
TouchAction touchAction2 = new TouchAction(driver)
.tap(ElementOption.element(e2))
.release();
MultiTouchAction action = new MultiTouchAction();
action.add(touchAction1);
action.add(touchAction2);
action.perform();
  1. Swiping using multiple fingers

NOTE: As mentioned earlier only MultiTouchAction should call the perform() method at the end. For TouchActions perform() method should not be called otherwise instructions will be sent to Appium server and the click will happen before the Multi Touch action.In this chapter we have looked into the most used scenarios in the Appium world. These methods all work on both Android and iOS.More details about all the different TouchAction methods can be found on the official appium docs.https://appium.github.io/java-client/io/appium/java_client/TouchAction.html

Interested in Learning More?

Subscribe today to stay informed and get regular updates from Kobiton

Ready to accelerate delivery of
your mobile apps?

Request a Demo