Problem: Documentation on automating mobile gestures is not extensive.
In this post, we show how to automate mobile gestures like tap, swipe, pinch and zoom using Appium.
Why this post?
I watched Steve Jobs introduce the iPhone back in 2007. Among the jaw dropping moments were using the best stylus ever invented (our fingers) and the multi-touch pinch-to-zoom feature. Mobile applications were no longer shrunk versions of web applications. Users were going to interact with applications on mobile devices differently. Today tap, swipe, zoom and pinch are key gestures that humans use to interact with products on the mobile platform. Which in turn means, automating mobile gestures is an important part of writing test automation scripts. Documentation on gestures like swipe, pinch and zoom are not so clear. This post hopes to clarify how to perform these gestures using Appium.
Pinch, zoom and swipe using Appium and Python
We have picked the camera application to show the zoom and pinch gestures. We will launch the camera app and automate these gestures. Also we will show you how to perform swipe gesture on the home screen after closing camera app.
""" Qxf2: Example script to run test for a camera app using Appium The test will cover some mobile gestures like tap, swipe, pinch and zoom. """ import unittest, time from appium import webdriver from time import sleep class Android_Gestures(unittest.TestCase): "Class to run tests for mobile gestures" def setUp(self): "Setup for the test" desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.3' desired_caps['deviceName'] = 'GT-I9300' # Get the Package and Activity name to launch the Camera app desired_caps['appPackage'] = 'com.sec.android.app.camera' desired_caps['appActivity'] = 'com.sec.android.app.camera.Camera' #You may need to change the line below depending on your setup self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self): "Tear down the test" self.driver.quit() def test_mobile_gestures(self): "Testing mobile gestures " self.driver.implicitly_wait(10) # Get the position of camera icon to take picture positions = [(1200,360)] time.sleep(3) # BONUS: TAP # Perform Tap gesture on the position to take picture self.driver.tap(positions) # Find the android element of camera view elm = self.driver.find_element_by_id('com.sec.android.app.camera:id/GLSurfaceLayout') # 1. ZOOM # Perform zoom gesture- zoom(element=None, percent=200, steps=50) self.driver.zoom(element = elm) #(doesnt work in android 4.2) # Tap to take close up snap self.driver.tap(positions) time.sleep(5) # 2. PINCH # Perform pinch gesture- pinch(element=None, percent=200, steps=50) self.driver.pinch(element = elm, percent=150, steps=100) #(doesnt work in android 4.2) # Tap again to take zoomed out picture self.driver.tap(positions) time.sleep(5) # Press back button to go to home screen self.driver.keyevent(4) time.sleep(5) # 3. SWIPE # Perform swipe gesture on home screen- swipe(startX, startY, endX, endY, duration) self.driver.swipe(475, 500, 75, 500, 400) #---START OF SCRIPT if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(Android_Gestures) unittest.TextTestRunner(verbosity=2).run(suite) |
Running the test script
This step expects you to be setup to run Appium tests. If you are new to Appium, you can refer to our posts here or here.
Note 1: Some features such as zoom, pinch are available starting API level 18 in Android.
Note 2: In Moto E (android API level 19), we noted that the pinch,zoom gestures for camera app do not work consistently. Instead use swipe up and down for zoom, pinch.Here is the code snippet.
self.driver.swipe(475, 500, 475, 200, 400) #swipe up action.tap(e1).perform() time.sleep(5) self.driver.swipe(475, 200, 475, 500, 400) #swipe down action.tap(e1).perform() time.sleep(5) |
Looking for a Pythonic Appium automation framework? Check out our open-sourced Mobile automation framework based on the page object model.
Hi Avinash ,
I am kind of fan now. I always look out for your new post. This is excellent post once gain. As i mention in other Block why don’t you do some you tube video as well. Since you have talent in commercial world this kind of tutorial is lacking in you tube.
anyway good luck and god bless you for your kind help.
thanks
selva
Hi ,
I have one question how do we get value for swap from UI Automator Viewer for any application.
swipe(startX, startY, endX, endY, duration)
i mean the ” x”, “y” ,” end x” and “end y”
if this from the Node Details screen where we see next Bounds [120,0][996,720]
any advice is usefull.
Thanks
Hover over the snapshot in the left-hand panel to see the UI components identified by the uiautomatorviewer tool. You can view the position(x,y) in the upper right-hand panel.Make sure the UIAutomatorViewer is maximized.
You can also see this post -https://qxf2.com/blog/naf-issue-mobile-testing/
Hi,
Thanks for your help. I can get my app to work.
Regards
Selva
Hi Avinash,
Good article.
I was aware of these commands and I am using them in automating an iOS application.
The swipe command is working fine in real iOS device but the same command doesn’t work in iOS simulator.
Can you think of some reasons that why this thing is happening?
Reagrds,
Mohit Anand
Hi Mohit,
Thanks for your Feedback. As mentioned earlier we have not taken a serious shot at iOS automation yet.
Researching on your issue, i guess there is already some known issues with gestures not working on the iOS 7 simulator. https://github.com/appium/appium/issues/1696.
Thanks & Regards
Avinash Shetty
Thanks for pointing this out.
I will try with iOS8 simulator.
Hi Avinash,
When I am using xPath to identify some elements in iOS app, it sometimes changes when we move from iOS7 simulator to iOS 8 simulator.
So, I am thinking to write a common script for them using if-else condition in python.
Could you please tell me how can we get the platform version which we have provided in the desired capabilities section?
In other words, how can we refer that attribute?
Mohit, short answer is to:
1) maintain a configuration file with xpaths for each version of iOS (we used Python’s ConfigParser)
2) accept the iOS version as a command line parameter to the test script
3) make the script read and load the xpaths you need based on the os version
This way you write only one script that runs on both devices.
It so happens Vrushali and Avinash are writing up a post on solving a very similar problem we faced with running scripts on different Android versions. Our case was more complex in that even the identifier (xpath vs id vs classname) differed between the different versions. The detailed blog post should be out in the next 5-10 days.
Thanks for the detailed answer Arun. I will try to implement your ideas.
I am eagerly waiting to see that post.
Hey, Mohit, the post is out: https://qxf2.com/blog/running-mobile-automation-on-multiple-devices/
Hi I am getting error as :
: Activity used to start app doesn’t exist or cannot be launched! Make sure it exists and is a launchable activity)
Please help
Hi Akshata,
Can you check the Activity Name of the app you are trying. The activity name on different devices may vary.
You can refer to the 3rd step on this blogs to get the Activity and Package name.
Hi,
Is there any method or way to automate(validation and verification) of playing video in android app using Appium?
I tried searching to automate this usecase but couldnt get any help.
Thanks in advance,
Anil
Anil, I honestly don’t know. We have not hit this problem before. We had fun researching this question – so thank you! Unfortunately I do not have a concrete answer for you. The more I read about it, the more it seems that this problem is tackled a lot more in academia than the industry.
BTW, if you are googling for the problem, I’d suggest you drop Appium which is for GUI automation. We found that the phrase “Video Quality Assessment Tools” is a good starting point for Googling.
Hi,
I am using java client 2.2. I am getting error when i try to use driver.swipe action- An unknown server-side error occurred while processing the command
Please help me out.
Hi Praveen,
Are you using the SwipeElementDirection as mentioned in the docs?
https://github.com/appium/java-client/blob/master/src/test/java/io/appium/java_client/ios/iOSGestureTest.java
Hi Avinash…
First thanks for posting such a good article on Appium automation.
I tried the above script and getting the error –
Undefined variable Android_gestures
Please help me here……….
Thanks in advance.
Hi Kunal,
Nice to hear that you found the post helpful.
Regarding your question. How are you trying to run the script? Can you check the file name. Probably you have used Android_gestures instead of Android_Gestures.
Regards
Avinash