{"id":2293,"date":"2015-01-30T04:38:42","date_gmt":"2015-01-30T09:38:42","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=2293"},"modified":"2021-02-25T06:03:20","modified_gmt":"2021-02-25T11:03:20","slug":"automating-pinch-zoom-swipe-appium","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/automating-pinch-zoom-swipe-appium\/","title":{"rendered":"Pinch, Zoom and Swipe using Appium"},"content":{"rendered":"<p><strong>Problem:<\/strong> Documentation on automating mobile gestures is not extensive.<\/p>\n<p>In this post, we show how to automate mobile gestures like tap, swipe, pinch and zoom using Appium.<\/p>\n<hr>\n<h3>Why this post?<\/h3>\n<p>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.<\/p>\n<hr>\n<h3>Pinch, zoom and swipe using Appium and Python<\/h3>\n<p>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.<\/p>\n<pre lang='python'>\r\n\"\"\"\r\nQxf2: Example script to run test for a camera app using Appium\r\nThe test will cover some mobile gestures like tap, swipe, pinch and zoom.\r\n\r\n\"\"\"\r\nimport unittest, time\r\nfrom appium import webdriver\r\nfrom time import sleep\r\n\r\nclass Android_Gestures(unittest.TestCase):\r\n    \"Class to run tests for mobile gestures\"\r\n    def setUp(self):\r\n        \"Setup for the test\"\r\n        desired_caps = {}\r\n        desired_caps['platformName'] = 'Android'\r\n        desired_caps['platformVersion'] = '4.3'      \r\n        desired_caps['deviceName'] = 'GT-I9300'\r\n        # Get the Package and Activity name to launch the Camera app\r\n        desired_caps['appPackage'] = 'com.sec.android.app.camera'\r\n        desired_caps['appActivity'] = 'com.sec.android.app.camera.Camera'\r\n        #You may need to change the line below depending on your setup \r\n        self.driver = webdriver.Remote('http:\/\/localhost:4723\/wd\/hub', desired_caps)\r\n\r\n    def tearDown(self):\r\n        \"Tear down the test\"\r\n        self.driver.quit()\r\n\r\n    def test_mobile_gestures(self):\r\n        \"Testing mobile gestures \"\r\n        self.driver.implicitly_wait(10)\r\n        # Get the position of camera icon to take picture\r\n        positions = [(1200,360)]\r\n        time.sleep(3)\r\n\r\n        # BONUS: TAP\r\n        # Perform Tap gesture on the position to take picture\r\n        self.driver.tap(positions)\r\n\r\n        # Find the android element of camera view       \r\n        elm = self.driver.find_element_by_id('com.sec.android.app.camera:id\/GLSurfaceLayout')\r\n        \r\n        # 1. ZOOM \r\n        # Perform zoom gesture- zoom(element=None, percent=200, steps=50)\r\n        self.driver.zoom(element = elm) #(doesnt work in android 4.2)\r\n        # Tap to take close up snap \r\n        self.driver.tap(positions)\r\n        time.sleep(5)\r\n\r\n        # 2. PINCH \r\n        # Perform pinch gesture- pinch(element=None, percent=200, steps=50) \r\n        self.driver.pinch(element = elm, percent=150, steps=100) #(doesnt work in android 4.2)\r\n        # Tap again to take zoomed out picture\r\n        self.driver.tap(positions) \r\n        time.sleep(5)\r\n        \r\n        # Press back button to go to home screen\r\n        self.driver.keyevent(4)\r\n        time.sleep(5)\r\n\r\n        # 3. SWIPE        \r\n        # Perform swipe gesture on home screen- swipe(startX, startY, endX, endY, duration)\r\n        self.driver.swipe(475, 500, 75, 500, 400)\r\n       \r\n#---START OF SCRIPT\r\nif __name__ == '__main__':\r\n    suite = unittest.TestLoader().loadTestsFromTestCase(Android_Gestures)\r\n    unittest.TextTestRunner(verbosity=2).run(suite)\r\n\r\n<\/pre>\n<p><strong>Running the test script<\/strong><br \/>\nThis step expects you to be setup to run Appium tests. If you are new to Appium, you can refer to our posts <a href=\"https:\/\/qxf2.com\/blog\/appium-mobile-automation\/\">here<\/a> or <a href=\"https:\/\/qxf2.com\/blog\/appium-tutorial-python-physical-device\/\">here<\/a>.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/01\/run_test_gestures.gif\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/01\/run_test_gestures-300x114.gif\" alt=\"mobile_gestures\" width=\"300\" height=\"114\" class=\"aligncenter size-medium wp-image-2344\" \/><\/a><\/p>\n<p><strong>Note 1:<\/strong> Some features such as zoom, pinch are available starting API level 18 in Android.<\/p>\n<p><strong>Note 2:<\/strong> 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.<\/p>\n<pre lang='python'>\r\n        self.driver.swipe(475, 500, 475, 200, 400) #swipe up\r\n        action.tap(e1).perform()\r\n        time.sleep(5)\r\n        self.driver.swipe(475, 200, 475, 500, 400) #swipe down\r\n        action.tap(e1).perform()\r\n        time.sleep(5)\r\n<\/pre>\n<p>Looking for a Pythonic Appium automation framework? Check out our open-sourced <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">Mobile automation framework based on the page object model<\/a>.\n<\/div>\n<hr>\n<script>(function() {\n\twindow.mc4wp = window.mc4wp || {\n\t\tlisteners: [],\n\t\tforms: {\n\t\t\ton: function(evt, cb) {\n\t\t\t\twindow.mc4wp.listeners.push(\n\t\t\t\t\t{\n\t\t\t\t\t\tevent   : evt,\n\t\t\t\t\t\tcallback: cb\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n})();\n<\/script><!-- Mailchimp for WordPress v4.10.1 - https:\/\/wordpress.org\/plugins\/mailchimp-for-wp\/ --><form id=\"mc4wp-form-1\" class=\"mc4wp-form mc4wp-form-6165 mc4wp-form-theme mc4wp-form-theme-blue\" method=\"post\" data-id=\"6165\" data-name=\"Newsletter\" ><div class=\"mc4wp-form-fields\"><div style=\"border:3px; border-style:dashed;border-color:#56d1e1;padding:1.2em;\">\r\n  <h1 style=\"text-align: center; padding-top: 20px; padding-bottom: 20px; color: #592b1b;\">Subscribe to our weekly Newsletter<\/h1>\r\n  <input style=\"margin: auto;\" type=\"email\" name=\"EMAIL\" placeholder=\"Your email address\" required \/>\r\n  <br>\r\n  <p style=\"text-align: center;\">\r\n    <input style=\"background-color: #890c06 !important; border-color: #890c06;\" type=\"submit\" value=\"Sign up\" \/>\r\n    \r\n  <\/p>\r\n  <p style=\"text-align: center;\">\r\n    <a href=\"http:\/\/mailchi.mp\/c9c7b81ddf13\/the-informed-testers-newsletter-20-oct-2017\"><small>View a sample<\/small><\/a>\r\n  <\/p>\r\n  <br>\r\n<\/div><\/div><label style=\"display: none !important;\">Leave this field empty if you're human: <input type=\"text\" name=\"_mc4wp_honeypot\" value=\"\" tabindex=\"-1\" autocomplete=\"off\" \/><\/label><input type=\"hidden\" name=\"_mc4wp_timestamp\" value=\"1776759284\" \/><input type=\"hidden\" name=\"_mc4wp_form_id\" value=\"6165\" \/><input type=\"hidden\" name=\"_mc4wp_form_element_id\" value=\"mc4wp-form-1\" \/><div class=\"mc4wp-response\"><\/div><\/form><!-- \/ Mailchimp for WordPress Plugin -->\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,50,71,18],"tags":[],"class_list":["post-2293","post","type-post","status-publish","format-standard","hentry","category-android","category-appium","category-mobile-automation","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/2293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=2293"}],"version-history":[{"count":30,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions"}],"predecessor-version":[{"id":14938,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions\/14938"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=2293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=2293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=2293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}