Best Practices to Elevate Your Mobile App Design
Cara Suarez
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:
Appium supports these gestures using the TouchActions class.
TouchAction touchAction = new TouchAction(driver);
Some of the supported methods are:
| Method Name | Purpose |
| 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?
Getting the pointer location in Android:
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:
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.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.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.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.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.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.// 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
));
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();
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();
TouchAction touchAction = new TouchAction(driver); touchAction.press(element(element)) .waitAction(waitOptions(ofSeconds(seconds))) .release() .perform();
TouchAction touchAction = new TouchAction(driver); touchAction.press(point(x,y)) .waitAction(waitOptions(ofSeconds(seconds))) .release() .perform();
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:

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.
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.
TouchAction swipe = new TouchAction(driver) .press(PointOption.point(540,1824)) .waitAction(waitOptions(ofMillis(800))) .moveTo(PointOption.point(540,672)) .release() .perform();
Scroll up will work the same but with different location points.
TouchAction swipe = new TouchAction(driver) .press(ElementOption.element(element1)) .waitAction(waitOptions(ofSeconds(2))) .moveTo(ElementOption.element(element2)) .release() .perform();
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:
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();
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();
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