Get your setup working faster. Join our Discord for optimisation tips from elite testers.Join our Discord
Automate mobile gestures
Automate various mobile gestures using Appium and run on App Automate.
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
The TouchAction class allows one to simulate various mobile gestures by creating a chain of events such as tap, press, wait, longPress, release, etc., which can then be performed in sequence on a mobile device using the perform event.
// Long Press gesture using TouchAction classTouchActiontouchAction=newTouchAction(driver);LongPressOptionslongPressOptions=newLongPressOptions();longPressOptions.withElement(ElementOption.element(element));touchAction.longPress(longPressOptions).release().perform();// Scroll/Swipe gesture using TouchAction classTouchActiontouchAction=newTouchAction(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 classawaitdriver.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 classawaitdriver.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 classTouchActiontouchAction=newTouchAction(driver);touchAction.LongPress(newLongPressGesture().WithElement(ElementOption.Element(element))).Release().Perform();// Scroll/Swipe gesture using TouchAction classTouchActiontouchAction=newTouchAction(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.
// Double Tap GestureJavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("element",((RemoteWebElement)element).getId());js.executeScript("mobile: doubleTap",params);// Scroll Down GestureJavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("element",((RemoteWebElement)element).getId());params.put("direction","down");js.executeScript("mobile: scroll",params);
// Double Tap Gestureawaitdriver.execute("mobile: doubleTap",{element:element.elementId});// Scroll Down Gestureawaitdriver.execute("mobile: scroll",{direction:"down"});
// Double Tap Gesturedriver.ExecuteScript("mobile: doubleTap",newDictionary<string,object>(){{"element",element.Id}});// Scroll Down Gesturedriver.ExecuteScript("mobile: scroll",newDictionary<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.
// Long press gesture using Appium commandJavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("x",x);params.put("y",y);params.put("duration",1000);js.executeScript("mobile: longClickGesture",params);// Swipe gesture using Appium commandJavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("elementId",AndroidElement.getId());params.put("direction","left");js.executeScript("mobile: swipeGesture",params);
// Long press gesture using Appium commandawaitdriver.execute('mobile: longClickGesture',{x:x,y:y,duration:1000,});// Swipe gesture using Appium commandawaitdriver.execute('mobile: swipeGesture',{elementId:AndroidElement.id,direction:'left',});
// Long press gesture using Appium commanddriver.ExecuteScript("mobile: longClickGesture",newDictionary<string,object>(){{"x",x},{"y",y},{"duration",1000}});// Swipe gesture using Appium commanddriver.ExecuteScript("mobile: swipeGesture",newDictionary<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.
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 TouchActionTouchActiontouchAction=newTouchAction(driver);TapOptionstapOptions=newTapOptions();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.21JavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("x",x);params.put("y",y);js.executeScript("mobile: doubleClickGesture",params);
//Using TouchActionawaitelement.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.21awaitdriver.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.21driver.ExecuteScript("mobile: doubleClickGesture",newDictionary<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 TouchActionTouchActiontouchAction=newTouchAction(driver);LongPressOptionslongPressOptions=newLongPressOptions();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.19JavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("x",x);params.put("y",y);params.put("duration",1000);js.executeScript("mobile: longClickGesture",params);
// Long Press gesture using TouchAction classawaitdriver.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.19awaitdriver.execute('mobile: longClickGesture',{x:x,y:y,duration:1000,});
// Using TouchActionAndroidElementandroidElement=driver.FindElement(By.Id("your_element_id"));TouchActiontouchAction=newTouchAction(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.19driver.ExecuteScript("mobile: longClickGesture",newDictionary<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.locationdriver.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 dimensionsorg.openqa.selenium.Dimensiondimensions=driver.manage().window().getSize();// Set coordinates for the swipeintstart_x=(int)(dimensions.width*0.8);intend_x=(int)(dimensions.width*0.2);inty=(int)(dimensions.height*0.5);// Perform horizontal swipe gesture using TouchAction APITouchAction<?>touchAction=newTouchAction<>(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.19JavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("elementId",AndroidElement.getId());params.put("direction","left");js.executeScript("mobile: swipeGesture",params);
// Using TouchAction// Get screen dimensionsconst{width,height}=awaitclient.getWindowRect();// Set coordinates for the swipeconststart_x=Math.floor(width*0.8);constend_x=Math.floor(width*0.2);consty=Math.floor(height*0.5);// Perform horizontal swipe gestureawaitclient.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.19awaitdriver.execute('mobile: swipeGesture',{elementId:AndroidElement.id,direction:'left',});
// Using TouchAction// Get screen dimensionsSizedimensions=driver.Manage().Window.Size;// Set coordinates for the swipeintstart_x=(int)(dimensions.Width*0.8);intend_x=(int)(dimensions.Width*0.2);inty=(int)(dimensions.Height*0.5);// Perform horizontal swipe gesture using TouchAction APITouchActiontouchAction=newTouchAction(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.19driver.ExecuteScript("mobile: swipeGesture",newDictionary<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.8location_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 dimensionsorg.openqa.selenium.Dimensiondimensions=driver.manage().window().getSize();// Set the X coordinate for the scroll elementintlocation_x=(int)(dimensions.width*0.5);// Set start and end Y coordinates for the scroll direction (up or down)intlocation_start_y=(int)(dimensions.height*0.6);intlocation_end_y=(int)(dimensions.height*0.3);// Perform vertical scroll gesture using TouchAction APITouchAction<?>touchAction=newTouchAction<>(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.19JavascriptExecutorjs=(JavascriptExecutor)driver;Map<String,Object>params=newHashMap<>();params.put("elementId",AndroidElement.getId());params.put("direction","up");js.executeScript("mobile: scrollGesture",params);
//Using TouchAction// Get screen dimensionsconst{width,height}=awaitclient.getWindowRect();// Set the X coordinate for the scroll elementconstlocation_x=Math.floor(width*0.5);// Set start and end Y coordinates for the scroll direction (up or down)constlocation_start_y=Math.floor(height*0.6);constlocation_end_y=Math.floor(height*0.3);// Perform vertical scroll gestureawaitclient.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.19awaitdriver.execute('mobile: scrollGesture',{elementId:AndroidElement.id,direction:'up',});
// Using TouchAction// Get screen dimensionsSizedimensions=driver.Manage().Window.Size;// Set the X coordinate for the scroll elementintlocation_x=(int)(dimensions.Width*0.5);// Set start and end Y coordinates for the scroll direction (up or down)intlocation_start_y=(int)(dimensions.Height*0.6);intlocation_end_y=(int)(dimensions.Height*0.3);// Perform vertical scroll gesture using TouchAction APITouchActiontouchAction=newTouchAction(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.19driver.ExecuteScript("mobile: scrollGesture",newDictionary<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 direction up or down
location_start_y=dimensions["height"]*0.6location_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.