BrowserStack App Automate enables you to run common app gestures using the Appium automation framework on BrowserStack’s real Android and iOS device cloud. In this guide, you will learn about :
Appium support for mobile app gestures
Usage examples of common mobile app gestures
Appium support for mobile app gestures
Appium supports mobile app gestures using the following 3 ways :
TouchAction/MultiAction class
Appium commands for iOS gestures
Appium commands for Android gestures
TouchActions/MultiAction Class
TouchAction class allows one to simulate various mobile gestuers by creating a chain of events such as tap, press, wait, longPress, release, etc which can then performed in sequence on a mobile device using the perform event.
// Long Press gesture using TouchAction class
TouchAction touchAction = new TouchAction(driver);
LongPressOptions longPressOptions = new LongPressOptions();
longPressOptions.withElement(ElementOption.element(element));
touchAction.longPress(longPressOptions).release().perform();
// Scroll/Swipe gesture using TouchAction class
TouchAction touchAction = new TouchAction(driver);
touchAction.press(PointOption.point(startX, startY))
.waitAction(waitOptions.withDuration(Duration.ofMillis(1000)))
.moveTo(PointOption.point(endX, endY))
.release()
.perform();
// Long Press gesture using TouchAction class
await driver.touchAction([
{ action: 'press', element: elementId }, // Replace 'elementId' with your element identifier
{ action: 'wait', ms: 1000 }, // Wait for 1000 milliseconds (1 second)
{ action: 'release' },
]);
// Scroll/Swipe gesture using TouchAction class
await driver.touchAction([
{ action: 'press', x: startX, y: startY }, // Replace 'startX' and 'startY' with your start coordinates
{ action: 'wait', ms: 1000 }, // Wait for 1000 milliseconds (1 second)
{ action: 'moveTo', x: endX, y: endY }, // Replace 'endX' and 'endY' with your end coordinates
{ action: 'release' },
]);
// Long Press gesture using TouchAction class
TouchAction touchAction = new TouchAction(driver);
touchAction.LongPress(new LongPressGesture().WithElement(ElementOption.Element(element))).Release().Perform();
// Scroll/Swipe gesture using TouchAction class
TouchAction touchAction = new TouchAction(driver);
touchAction.Press(PointOption.Point(startX, startY)).Wait(1000).MoveTo(PointOption.Point(endX, endY)).Release().Perform();
# Long Press gesture using TouchAction class
TouchAction(driver).long_press(element).release().perform()# Scroll/Swipe gesture using TouchAction class
TouchAction(driver).press(startX,startY).wait(1000).move_to(endX,endY).release().perform()
Make sure to replace the desired capabilities, element, startX, startY, endX, and endY with your actual values in your code.
For multi touch gestures, Appium provides MultiAction class. For more details on TouchAction/MultiAction class refer Appium documentation.
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.
Appium commands for iOS gestures
Appium provides native mobile commands for commonly used iOS gestures such as tap, doubleTap, swipe, scroll, etc.
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.
Double Tap Gesture
It will perform a double click/tap gesture on a UI element or a particular x,y coordinate point.
// Using TouchAction
TouchAction touchAction = new TouchAction(driver);
TapOptions tapOptions = new TapOptions();
tapOptions.withElement(ElementOption.element(element)).withTapsCount(2);;
touchAction.tap(tapOptions).perform();
// OR
// Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations
// Note: Available since Appium version 1.21
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("x", x);
params.put("y", y);
js.executeScript("mobile: doubleClickGesture", params);
//Using TouchAction
await element.touchAction([
{ action: 'tap', count: 2 }
]);
//OR
// Using Appium command. One can provide arguments such as elementId or x,y co-ordinate
// Note: Available since Appium version 1.21
await driver.execute('mobile: doubleClickGesture', {
elementId: AndroidElement.id
});
// Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations
// Note: Available since Appium version 1.21
driver.ExecuteScript("mobile: doubleClickGesture", new Dictionary<string, object>()
{
{ "x", x },
{ "y", y }
});
# Using TouchAction
TouchAction(driver).double_tap(AndroidElement).release().perform()# OR # Using Appium command. One can provide arguments such as elementId or x,y co-ordinate# Note: Available since Appium version 1.21
driver.execute_script('mobile: doubleClickGesture',{'elementId':AndroidElement.id})
Note the following points before running app gestures using the Appium framework:
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.
Make sure to replace the desired capabilities, element, startX, startY, endX, and endY with your actual values in your code.
Long Press Gesture
It will perform a long press gesture on a UI element or a particular x,y coordinate point.
// Using TouchAction
TouchAction touchAction = new TouchAction(driver);
LongPressOptions longPressOptions = new LongPressOptions();
longPressOptions.withElement(ElementOption.element(element));
touchAction.longPress(longPressOptions).release().perform();
// OR
// Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations
// Note: Available since Appium version 1.19
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("x", x);
params.put("y", y);
params.put("duration", 1000);
js.executeScript("mobile: longClickGesture", params);
// Long Press gesture using TouchAction class
await driver.touchAction([
{ action: 'press', element: elementId }, // Replace 'elementId' with your element identifier
{ action: 'wait', ms: 1000 }, // Wait for 1000 milliseconds (1 second)
{ action: 'release' },
]);
// OR
// Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations
// Note: Available since Appium version 1.19
await driver.execute('mobile: longClickGesture', {
x: x,
y: y,
duration: 1000,
});
// Using TouchAction
AndroidElement androidElement = driver.FindElement(By.Id("your_element_id"));
TouchAction touchAction = new TouchAction(driver);
touchAction.LongPress(LongPressAction.LongPressOptions().WithElement(ElementOption.Element(androidElement))
.WithDuration(TimeSpan.FromMilliseconds(1000))) // Duration in milliseconds
.Perform();
// OR
// Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations
// Note: Available since Appium version 1.19
driver.ExecuteScript("mobile: longClickGesture", new Dictionary<string, object>()
{
{ "x", x },
{ "y", y },
{ "duration", 1000 }
});
# Using TouchAction
TouchAction(driver).long_press(AndroidElement).release().perform()# OR# Using Appium Command. In supported arguments one can pass element, duration in milliseconds, or x,y co-ordinations# Note: Available since Appium version 1.19
pointLocation = AndroidElement.location
driver.execute_script("mobile: longClickGesture",{"x":pointLocation["x"],"y":pointLocation["y"],"duration":1000})
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.
Swipe & Scroll Gestures
Horizontal Swipe
The gesture performs a ‘horizontal swipe’ on a particular screen element or the whole screen. To implement horizontal swipe use percentage of screen width as starting and ending X co-ordinate while keeping Y co-ordinate same.
// Using TouchAction
// Get screen dimensions
org.openqa.selenium.Dimension dimensions = driver.manage().window().getSize();
// Set coordinates for the swipe
int start_x = (int) (dimensions.width * 0.8);
int end_x = (int) (dimensions.width * 0.2);
int y = (int) (dimensions.height * 0.5);
// Perform horizontal swipe gesture using TouchAction API
TouchAction<?> touchAction = new TouchAction<>(driver);
touchAction.press(start_x, y).waitAction(java.time.Duration.ofMillis(1000))
.moveTo(end_x, y).release().perform();
// OR
// Using Appium command. One can provide arguments such as elementId, direction, and speed
// Note: Available since Appium v1.19
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("elementId", AndroidElement.getId());
params.put("direction", "left");
js.executeScript("mobile: swipeGesture", params);
// Using TouchAction
// Get screen dimensions
const { width, height } = await client.getWindowRect();
// Set coordinates for the swipe
const start_x = Math.floor(width * 0.8);
const end_x = Math.floor(width * 0.2);
const y = Math.floor(height * 0.5);
// Perform horizontal swipe gesture
await client.touchAction([
{ action: 'press', x: start_x, y },
{ action: 'wait', ms: 1000 },
{ action: 'moveTo', x: end_x, y },
{ action: 'release' }
]);
// OR
// Using Appium command. One can provide arguments such as elementId, direction, and speed
// Note: Available since Appium v1.19
await driver.execute('mobile: swipeGesture', {
elementId: AndroidElement.id,
direction: 'left',
});
// Using TouchAction
// Get screen dimensions
Size dimensions = driver.Manage().Window.Size;
// Set coordinates for the swipe
int start_x = (int)(dimensions.Width * 0.8);
int end_x = (int)(dimensions.Width * 0.2);
int y = (int)(dimensions.Height * 0.5);
// Perform horizontal swipe gesture using TouchAction API
TouchAction touchAction = new TouchAction(driver);
touchAction.Press(start_x, y).Wait(1000).MoveTo(end_x, y).Release().Perform();
// OR
// Using Appium command. One can provide arguments such as elementId, direction, and speed
// Note: Available since Appium v1.19
driver.ExecuteScript("mobile: swipeGesture", new Dictionary<string, object>()
{
{ "elementId", AndroidElement.Id },
{ "direction", "left" }
});
# Using TouchAction# Get screen dimensions
screen_dimensions = driver.get_window_size()# Set co-ordinate start X and end X according to the left or right swipe
location_start_x = dimensions["width"]*0.8
location_end_x = dimensions["width"]*0.2# Set co-ordinate Y according to the element you want to swipe on.
location_y = dimensions["height"]*0.5# Perform horizontal swipe gesture using TouchAction API.
TouchAction(driver).press(x=location_start_x,y=location_y).wait(1000).move_to(x=location_end_x,y=location_y).release().perform()# OR # Using Appium command. One can provide arguments such as elementId, direction, and speed# Note: Available since Appium v1.19
driver.execute_script("mobile: swipeGesture",{'elementId':AndroidElement.id,'direction':'left'})
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.
Vertical Scroll
The gesture performs a ‘vertical scroll’ on a particular screen element or the whole screen. To implement horizontal swipe use percentage of screen height as starting and ending Y co-ordinate while keeping X co-ordinate same.
// Using TouchAction
// Get screen dimensions
org.openqa.selenium.Dimension dimensions = driver.manage().window().getSize();
// Set the X coordinate for the scroll element
int location_x = (int) (dimensions.width * 0.5);
// Set start and end Y coordinates for the scroll direction (up or down)
int location_start_y = (int) (dimensions.height * 0.6);
int location_end_y = (int) (dimensions.height * 0.3);
// Perform vertical scroll gesture using TouchAction API
TouchAction<?> touchAction = new TouchAction<>(driver);
touchAction.press(location_x, location_start_y).waitAction(java.time.Duration.ofMillis(1000))
.moveTo(location_x, location_end_y).release().perform();
// OR
// Using Appium command. One can provide arguments such as direction, element, and velocity
// Note: Available since Appium v1.19
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("elementId", AndroidElement.getId());
params.put("direction", "up");
js.executeScript("mobile: scrollGesture", params);
//Using TouchAction
// Get screen dimensions
const { width, height } = await client.getWindowRect();
// Set the X coordinate for the scroll element
const location_x = Math.floor(width * 0.5);
// Set start and end Y coordinates for the scroll direction (up or down)
const location_start_y = Math.floor(height * 0.6);
const location_end_y = Math.floor(height * 0.3);
// Perform vertical scroll gesture
await client.touchAction([
{ action: 'press', x: location_x, y: location_start_y },
{ action: 'wait', ms: 1000 },
{ action: 'moveTo', x: location_x, y: location_end_y },
{ action: 'release' }
]);
// OR
// Using Appium command. One can provide arguments such as direction, element, and velocity
// Note: Available since Appium v1.19
await driver.execute('mobile: scrollGesture', {
elementId: AndroidElement.id,
direction: 'up',
});
// Using TouchAction
// Get screen dimensions
Size dimensions = driver.Manage().Window.Size;
// Set the X coordinate for the scroll element
int location_x = (int)(dimensions.Width * 0.5);
// Set start and end Y coordinates for the scroll direction (up or down)
int location_start_y = (int)(dimensions.Height * 0.6);
int location_end_y = (int)(dimensions.Height * 0.3);
// Perform vertical scroll gesture using TouchAction API
TouchAction touchAction = new TouchAction(driver);
touchAction.Press(location_x, location_start_y).Wait(1000).MoveTo(location_x, location_end_y).Release().Perform();
/ OR
// Using Appium command. One can provide arguments such as direction, element, and velocity
// Note: Available since Appium v1.19
driver.ExecuteScript("mobile: scrollGesture", new Dictionary<string, object>()
{
{ "elementId", AndroidElement.Id },
{ "direction", "up" }
});
# Using TouchAction# Get screen dimensions
screen_dimensions = driver.get_window_size()# Set co-ordinate X according to the element you want to scroll on.
location_x = dimensions["width"]*0.5# Set co-ordinate start Y and end Y according to the scroll driection up or down
location_start_y = dimensions["height"]*0.6
location_end_y = dimensions["height"]*0.3# Perform vertical scroll gesture using TouchAction API.
TouchAction(driver).press(x=location_x,y=location_start_y).wait(1000).move_to(x=x,y=location_end_y).release().perform()# OR # Using Appium command. One can provide arguments such as direction, element, and velocity# Note: Available since Appium v1.19
driver.execute_script("mobile: scrollGesture",{'elementId':AndroidElement.id,'direction':'up'})
Appium version 2.0 and above no longer support touchAction classes. Instead, use performActions. For more information, see Migrating to Appium 2.0 on BrowserStack.