Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & App Percy

Automating mobile gestures

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 :

  1. Appium support for mobile app gestures
  2. 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.

Examples:

// 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 commands for iOS gestures

Appium provides native mobile commands for commonly used iOS gestures such as tap, doubleTap, swipe, scroll, etc.

Examples :

// Double Tap Gesture
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("element", ((RemoteWebElement) element).getId());
js.executeScript("mobile: doubleTap", params);

// Scroll Down Gesture
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("element", ((RemoteWebElement) element).getId());
params.put("direction", "down");
js.executeScript("mobile: scroll", params);
    // Double Tap Gesture
    await driver.execute("mobile: doubleTap", { element: element.elementId });

    // Scroll Down Gesture
    await driver.execute("mobile: scroll", { direction: "down" });

// Double Tap Gesture
driver.ExecuteScript("mobile: doubleTap", new Dictionary<string, object>() { { "element", element.Id } } );

// Scroll Down Gesture
driver.ExecuteScript("mobile: scroll", new Dictionary<string, object>() { { "direction", "down" } } );
# Double tap gesture using Appium command
driver.execute_script('mobile: doubleTap', {'x': element_x, 'y': element_y})

# Scroll gesture using Appium command
driver.execute_script('mobile: scroll', {'direction': 'down'})

For more details on native mobile commands for iOS gestures refer Appium documentation.

Appium commands for Android gestures

Appium provides native mobile commands for some of the commonly used Android gestures such as long press, double click, swipe, scroll, etc.

Examples :

// Long press gesture using Appium command
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);

// Swipe gesture using Appium command
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("elementId", AndroidElement.getId());
params.put("direction", "left");
js.executeScript("mobile: swipeGesture", params);

  // Long press gesture using Appium command
  await driver.execute('mobile: longClickGesture', {
    x: x,
    y: y,
    duration: 1000,
  });

  // Swipe gesture using Appium command
  await driver.execute('mobile: swipeGesture', {
    elementId: AndroidElement.id,
    direction: 'left',
  });
// Long press gesture using Appium command
driver.ExecuteScript("mobile: longClickGesture", new Dictionary<string, object>()
{
    { "x", x },
    { "y", y },
    { "duration", 1000 }
});

// Swipe gesture using Appium command
driver.ExecuteScript("mobile: swipeGesture", new Dictionary<string, object>()
{
    { "elementId", AndroidElement.Id },
    { "direction", "left" }
});
# Long press gesture using Appium command
driver.execute_script('mobile: longClickGesture', {'x': 100, 'y': 100, 'duration': 1000})

# Swipe gesture using Appium command
driver.execute_script('mobile: swipeGesture', {'elementId':AndroidElement.id, 'direction':'left'})

For more details on native mobile commands for Android gestures, refer Appium documentation.

Usage examples of common mobiles gestures

Tap Gesture

It will perform a click/tap gesture on a UI element or a particular x,y coordinate point.

// Using TouchAction
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(element).perform();
// Using TouchAction
await driver.touchAction([
    { action: 'tap', element: elementId }, // Replace 'elementId' with your element identifier 
  ]);
// Using TouchAction
var singleTap = new TouchAction(driver)
          .Tap(element)
          .Perform();
# Using TouchAction
TouchAction(driver).tap(AndroidElement).release().perform()
JavascriptExecutor js = (JavascriptExecutor) driver;
        Map<String, Object> params = new HashMap<>();
        params.put("element", ((RemoteWebElement) element).getId());
        js.executeScript("mobile: tap", params);
await driver.execute("mobile: tap", { element: element.elementId });
driver.ExecuteScript("mobile: tap", new Dictionary<string, object>() { { "element", element.Id } } );
driver.execute_script('mobile: tap', {'x': element_x, 'y': element_y})

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})

Make sure to replace the desired capabilities, element, startX, startY, endX, and endY with your actual values in your code.

 JavascriptExecutor js = (JavascriptExecutor) driver;
 Map<String, Object> params = new HashMap<>();
 params.put("element", ((RemoteWebElement) element).getId());
 js.executeScript("mobile: doubleTap", params);
driver.execute('mobile: doubleTap', {element: element.value.ELEMENT});
driver.ExecuteScript("mobile: doubleTap", new Dictionary<string, object>()element);
driver.execute_script("mobile: doubleTap", {"element":iOSElement})

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})
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("duration", 2.0);
params.put("element", ((RemoteWebElement) element).getId());
js.executeScript("mobile: touchAndHold", params);
driver.execute('mobile: touchAndHold', {element: element.value.ELEMENT, duration: 2.0});
Dictionary<string, object> tfLongTap = new Dictionary<string, object>();
tfLongTap.Add("element", element.Id);
tfLongTap.Add("duration", 2.0);
((IJavaScriptExecutor)driver).ExecuteScript("mobile: touchAndHold", tfLongTap);
driver.execute_script('mobile: touchAndHold', {'element': element.Id ,'duration': 2.0});

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'})
 JavascriptExecutor js = (JavascriptExecutor) driver;
        Map<String, Object> params = new HashMap<>();
        params.put("element", ((RemoteWebElement) element).getId());
        params.put("direction", "left");
        js.executeScript("mobile: swipe", params);
 await driver.execute("mobile: swipe", { element:iOSElement, direction: "left" });
driver.ExecuteScript("mobile: swipe", new Dictionary<string, object>()direction);
driver.execute_script("mobile: swipe", {'element':iOSElement, 'direction':'left'})

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'})
JavascriptExecutor js = (JavascriptExecutor) driver;
        Map<String, Object> params = new HashMap<>();
        params.put("element", ((RemoteWebElement) element).getId());
        params.put("direction", "down");
        js.executeScript("mobile: scroll", params);
await driver.execute("mobile: scroll", { direction: "down" });
driver.ExecuteScript("mobile: scroll", new Dictionary<string, object>()direction);
driver.execute_script('mobile: scroll', {'direction': 'down'})

Need help?

If you have any queries, get in touch with us.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy