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

TouchAction/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(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()

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 using Appium command
driver.execute_script("mobile: doubleTap", {element_location_x, element_location_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
driver.execute_script('mobile: longClickGesture', {x, y, 'duration': 1000})

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

Please note that these commands are only available from Appium v1.19 or above. To run on older Appium versions use TouchAction class. 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
from appium.webdriver.common.touch_action import TouchAction
TouchAction(driver).tap(AndroidElement).release().perform()

# Using TouchAction
from appium.webdriver.common.touch_action import TouchAction
TouchAction(driver).tap(iOSElement).release().perform()

# OR 

# Using Appium command
pointLocation = iOSElement.location
driver.execute_script("mobile: tap", {"x":pointLocation["x"], "y":pointLocation["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
from appium.webdriver.common.touch_action import 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})


# Using TouchAction
from appium.webdriver.common.touch_action import TouchAction
TouchAction(driver).double_tap(iOSElement).release().perform()

# OR 

# Using Appium command. One can provide arrguments such as UIelement, number of taps to perform and number of touch points
# Note: Only available since Appium 1.17.1
driver.execute_script("mobile: tapWithNumberOfTaps", {"element":iOSElement, "numberOfTaps":2, "numberOfTouches":1})

Long Press Gesture

It will perform a long press gesture on a UI element or a particular x,y coordinate point.


# Using TouchAction
from appium.webdriver.common.touch_action import 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})

# Using TouchAction
from appium.webdriver.common.touch_action import TouchAction
TouchAction(driver).long_press(iOSElement).release().perform()

# OR 

# Using Appium command. In supported arguments one can pass element, press duration in seconds, or (x,y) co-orrdinations
pointLocation = iOSElement.location
driver.execute_script("mobile: touchAndHold", {"x":pointLocation["x"], "y":pointLocation["y"], "duration":1.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
from appium.webdriver.common.touch_action import 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'})

# Using TouchAction
from appium.webdriver.common.touch_action import 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 direction, element, and velocity
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
from appium.webdriver.common.touch_action import 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'})

# Using TouchAction
from appium.webdriver.common.touch_action import 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 element, direction, and speed
driver.execute_script("mobile: scroll", {'element':iOSElement, 'direction':'up'})

Need some help?

If you have any queries, please 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
Talk to an Expert