Pinch, Zoom and Swipe using Appium

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.

mobile_gestures

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)

A weekly newsletter for testers


View a sample



Avinash Shetty
I am a software tester with over 10 years of experience in software testing. Currently I am working at Qxf2 Services Bangalore. As a student of the context-driven approach to software testing I feel there is a lot to learn out there which keeps me very excited. My work has helped me gain good experience in different areas of testing like CRM, Web, Mobile and Database testing. I have good knowledge of building test scripts using Automation tools like Selenium and Appium using Java and Python. Beside testing I am a “Sports Fanatic” and love watching and playing sports.

© 2013-2017, Avinash Shetty. All rights reserved.

24 Comments

  1. selva said:

    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

    February 2, 2015
    Reply
  2. selva said:

    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

    February 2, 2015
    Reply
    • Vrushali Toshniwal Vrushali Mahalley said:

      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.
      UIAutomatorViewer X,Y xo-ordinates
      You can also see this post -http://qxf2.com/blog/naf-issue-mobile-testing/

      February 2, 2015
      Reply
      • selva said:

        Hi,
        Thanks for your help. I can get my app to work.

        Regards
        Selva

        February 6, 2015
        Reply
  3. Mohit said:

    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

    February 24, 2015
    Reply
    • Avinash Shetty Avinash Shetty said:

      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

      February 25, 2015
      Reply
  4. Mohit said:

    Thanks for pointing this out.
    I will try with iOS8 simulator.

    February 25, 2015
    Reply
  5. Mohit said:

    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?

    March 4, 2015
    Reply
    • 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.

      March 4, 2015
      Reply
  6. Mohit said:

    Thanks for the detailed answer Arun. I will try to implement your ideas.
    I am eagerly waiting to see that post.

    March 5, 2015
    Reply
  7. Akshata said:

    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

    April 17, 2015
    Reply
    • Avinash Shetty Avinash Shetty said:

      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.

      April 20, 2015
      Reply
  8. Anil said:

    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

    June 22, 2015
    Reply
    • 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.

      July 2, 2015
      Reply
  9. PRAVEEN SINGH said:

    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.

    August 25, 2015
    Reply
  10. kunal said:

    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.

    September 28, 2015
    Reply
    • Avinash Shetty Avinash Shetty said:

      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

      September 29, 2015
      Reply
  11. Badari said:

    Since these native commands wont work on Hybrid Mobile App .Please let me know how to carry out the same on Hybrid App.

    January 21, 2016
    Reply
    • Badari, unfortunately, we don’t know the answer to your question right now. If you do figure out how to do it, please do post your solution here.

      January 27, 2016
      Reply
  12. vks said:

    Does zoom really work with python?I get method not implemented.

    July 25, 2016
    Reply
    • Avinash Shetty Avinash Shetty said:

      Yes zoom works fine for me. What version of appium python client are you using?

      July 26, 2016
      Reply

Leave a Reply to selva Cancel reply

Your email address will not be published.