{"id":3707,"date":"2016-05-12T05:01:30","date_gmt":"2016-05-12T09:01:30","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=3707"},"modified":"2024-10-17T10:16:06","modified_gmt":"2024-10-17T14:16:06","slug":"get-set-test-an-ios-app-using-appium-and-python","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/get-set-test-an-ios-app-using-appium-and-python\/","title":{"rendered":"Get Set Test an iOS app using Appium and Python"},"content":{"rendered":"<p>It has been more than a year since I wrote my first blog on mobile automation using Appium for Android applications. There were many requests to come up with a similar blog for iOS applications. But due to time constraints I couldn&#8217;t get to it. But as they say, better late than never! I finally have a guide to help you get started with iOS automation using Appium. By the end of this post you should be able to set up your Mac with Appium and run a test for an iOS app using a simulator.<\/p>\n<hr>\n<h2> Setup<\/h2>\n<p>Here are the steps to set up Xcode and Appium on Mac OS <\/p>\n<p><strong>Step1. Download Xcode<\/strong><br \/>\n<a href=\"https:\/\/developer.apple.com\/xcode\/\">Xcode<\/a> is an integrated development environment (IDE) containing a suite of software development tools developed by Apple for developing software for OS X and iOS. You need to download <a href=\"https:\/\/developer.apple.com\/xcode\/download\/\">xcode<\/a> and install it so that you can download the code for an iOS app and run it on a simulator.<\/p>\n<p><strong>Step2. Start an iOS emulator using Xcode<\/strong><br \/>\nThe Simulator app, available within Xcode, presents the iPhone, iPad or Apple Watch user interface in a window on your Mac computer. To launch simulator<br \/>\n1. Launch Xcode.<br \/>\n2. Do one of the following:<\/p>\n<li>Choose Xcode > Open Developer Tool > Simulator.<\/li>\n<li>Control-click the Xcode icon in the Dock, and from the shortcut menu, choose Open Developer Tool > Simulator.<\/li>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/iOS_Simulator.jpg\" 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\/2016\/04\/iOS_Simulator.jpg\" alt=\"iOS Simulator\" width=\"320\" height=\"590\" class=\"aligncenter size-full wp-image-3731\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/iOS_Simulator.jpg 320w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/iOS_Simulator-163x300.jpg 163w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: You can also add simulators for any specific combination you want to test. For more details refer to this <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/IDEs\/Conceptual\/iOS_Simulator_Guide\/GettingStartedwithiOSSimulator\/GettingStartedwithiOSSimulator.html\">link<\/a><\/p>\n<p><strong>Step3. Download Appium client<\/strong><br \/>\nDownload the <a href=\"http:\/\/appium.io\/\">appium<\/a> client. I downloaded version 1.4.13.<\/p>\n<p><strong>Step4. Install Python and Python client Library for Appium<\/strong><br \/>\nThere are several client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#) which support Appium\u2019s extensions to the WebDriver protocol. When using Appium, you want to use these client libraries instead of your regular WebDriver client. I have used the Appium Python client available <a href=\"https:\/\/github.com\/appium\/python-client\">here<\/a>. Assuming you have pip installed on your machine, you can use the following command to install it<\/p>\n<pre lang='python'>\r\npip install Appium-Python-Client\r\n<\/pre>\n<p><strong>Step5. Start the Appium server console<\/strong><br \/>\nJust double click on Appium file to start the appium server console. Then click on Launch button to start the Appium node server<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Appium.jpg\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Appium.jpg\" alt=\"Appium\" width=\"1440\" height=\"797\" class=\"aligncenter size-full wp-image-3725\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Appium.jpg 1440w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Appium-300x166.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Appium-1024x567.jpg 1024w\" sizes=\"auto, (max-width: 1440px) 100vw, 1440px\" \/><\/a><\/p>\n<hr>\n<h2> Your first Test using Appium<\/h2>\n<p>For this test we will use a Table Search with the UISearchController app. We picked the UISearchController app because its source code was available to everyone and we could show you how to build the app and run it on a simulator. UISearchController is an iOS sample application that demonstrates how to use UISearchController.<\/p>\n<p><strong>Step1. Download the app to test<\/strong><br \/>\nYou can download the source code of Table Search with UISearchController app from <a href=\"https:\/\/developer.apple.com\/library\/ios\/samplecode\/TableSearch_UISearchController\/Introduction\/Intro.html\">here<\/a>. The code is available in both swift and Objective-C language. We will be using the <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/\">Swift<\/a> code as it is a new programming language for iOS, OS X, watchOS, and tvOS apps that builds on the best of C and Objective-C.<\/p>\n<p><strong>Step2. Open the project using Xcode<\/strong><br \/>\nLaunch Xcode. Click on Open another project and select TableSearch-swift.xcodeproj inside the Swift folder to open the project<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Open_Project_Xcode.jpg\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Open_Project_Xcode.jpg\" alt=\"Open Project Xcode\" width=\"802\" height=\"470\" class=\"aligncenter size-full wp-image-3733\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Open_Project_Xcode.jpg 802w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Open_Project_Xcode-300x176.jpg 300w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p><strong>Step3. Build the app and run it using a simulator<\/strong><br \/>\nOnce you have the source code you can build the app and run it on the simulator. Just click on the &#8220;Build and then run the current scheme&#8221; button (Play icon near top left). Once the build completes you should get a build succeeded message and the app should open in the simulator selected.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/X-Code.jpg\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/X-Code.jpg\" alt=\"X-Code\" width=\"1400\" height=\"797\" class=\"aligncenter size-full wp-image-3721\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/X-Code.jpg 1400w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/X-Code-300x171.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/X-Code-1024x583.jpg 1024w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Simulator.jpg\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Simulator.jpg\" alt=\"Simulator\" width=\"320\" height=\"590\" class=\"aligncenter size-full wp-image-3722\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Simulator.jpg 320w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Simulator-163x300.jpg 163w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p><strong>Step4. Get the .app file and bundle id <\/strong><br \/>\nTo write your script, you will need the path of the .app file as well as a <a href=\"http:\/\/stackoverflow.com\/questions\/11347470\/what-does-bundle-identifier-mean-in-the-ios-project\">bundle id<\/a>. <\/p>\n<p>In case if you have already built and run the app on your simulator you may not need the .app file to run the test on the same simulator. Else you need the Search.swift.app file to run the test. <\/p>\n<p>If you have run the app on a simulator you can find the .app file in below path<\/p>\n<pre lang='python'>\r\n~\/Library\/Developer\/CoreSimulator\/Devices\/{{Device Code}}\/data\/Containers\/Bundle\/Application\r\n<\/pre>\n<p>To get the bundle id, right click on the Search.swift.app file and do a Show Package Contents to view the info.plist file.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Info_plist.jpg\" data-rel=\"lightbox-image-5\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Info_plist-300x290.jpg\" alt=\"Info.plist\" width=\"300\" height=\"290\" class=\"aligncenter size-medium wp-image-3736\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Info_plist-300x290.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Info_plist.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Step5. Get the xpath of elements using Appium Inspector<\/strong><br \/>\nOur test will search for an Apple device and click on it to view its details and then navigate back to the home screen of the application. <\/p>\n<p>For this we need to find the xpath of different elements like Search field. We will use Appium Inspector for this.<br \/>\nOpen the iOS setting in Appium, enter the App path of your test application and check the box next to it. Give the device information like device name and platform version. Now click on the Inspector icon at the top as shown in the screenshot below<\/p>\n<p><strong>Note:<\/strong> Uncheck the BundelID field as i had issues when trying to launch appium Inspector with it populated<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector.png\" data-rel=\"lightbox-image-6\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector-1024x387.png\" alt=\"Appium_Inspector\" width=\"840\" height=\"317\" class=\"aligncenter size-large wp-image-4620\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector-1024x387.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector-300x113.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector-768x290.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/Appium_Inspector.png 1420w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/a><\/p>\n<p>The Appium Inspector will get launched. Now you can find any element and it\u2019s name by either clicking the element on the preview page provided, or locating it in the UI navigator. In this test, I\u2019m looking for the details of Search box field.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search.png\" data-rel=\"lightbox-image-7\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search-1024x436.png\" alt=\"Appium_Inspector_Search\" width=\"840\" height=\"358\" class=\"aligncenter size-large wp-image-4621\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search-1024x436.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search-300x128.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search-768x327.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/05\/xpath_search.png 1216w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/a><\/p>\n<p>Now we can use the details to build my xpath for Search field. Similarly you can use the appium Inspector to view the details of any field in the App.<\/p>\n<p><em>Xpath of Search field = &#8220;\/\/UIASearchBar[@name=&#8217;Search&#8217;]&#8221;<\/em><\/p>\n<p><strong>Step6. Write the test<\/strong><\/p>\n<p>Create a test script (Table_Search.py) in $Directory_Of_My_Choice based on the snippet below. I have my Search.swift.app file inside the &#8216;TableSearchwithUISearchController\/Swift&#8217; folder within my $Directoy_Of_My_Choice. I have hard coded the path in this example but you can use a combination of os.path.abspath and os.path.dirname(__file__) to make it a path relative to your test file. <\/p>\n<p>Also pay particular attention to the setup() method. You will run into all sorts of cryptic errors if the platformVersion, deviceName and bundleId are not set correctly.<\/p>\n<pre lang=\"python\">\r\n\r\n\"\"\"\r\nSimple iOS tests for table search app.\r\nNOTE: This is throwaway code to help testers get started with iOS automation\r\n\"\"\"\r\nimport unittest\r\nimport os\r\nfrom appium import webdriver\r\nfrom time import sleep\r\n\r\nclass TableSearchTest(unittest.TestCase):\r\n\r\n    def setUp(self):\r\n        # Set up appium\r\n        app = os.path.join(os.path.dirname(__file__),\r\n                           'TableSearchwithUISearchController\/Swift',\r\n                           'Search.swift.app')\r\n        app = os.path.abspath(app)\r\n        self.driver = webdriver.Remote(\r\n            command_executor='http:\/\/127.0.0.1:4723\/wd\/hub',\r\n            desired_capabilities={\r\n                'app': app,\r\n                'platformName': 'iOS',\r\n                'platformVersion': '9.2',\r\n                'deviceName': 'iPhone 5s',\r\n                'bundleId':'com.example.apple-samplecode.Search-swift'\r\n            })\r\n\r\n\r\n    def test_search_field(self):\r\n        # Search for an Apple device and click on it to view the details and navigate back\r\n        # Find the search element and perform send keys action\r\n        search_element = self.driver.find_element_by_xpath(\"\/\/UIASearchBar[@name='Search']\")\r\n        search_element.send_keys(\"iPad\")\r\n        sleep(2)\r\n        # Get the xpath of first element\r\n        first_element = self.driver.find_element_by_xpath(\"\/\/UIAApplication[1]\/UIAWindow[1]\/UIATableView[1]\/UIATableCell[1]\/UIAStaticText[1]\")\r\n        # Assert that the text matches\r\n        self.assertEqual('iPad', first_element.get_attribute('name'))\r\n        # Perform click action\r\n        first_element.click()\r\n        sleep(2)\r\n        # Click on search element\r\n        self.driver.find_element_by_name(\"Search\").click()\r\n       \r\n\r\n    def tearDown(self):\r\n        self.driver.quit()\r\n\r\n    \r\nif __name__ == '__main__':\r\n    suite = unittest.TestLoader().loadTestsFromTestCase(TableSearchTest)\r\n    unittest.TextTestRunner(verbosity=2).run(suite)\r\n<\/pre>\n<p><strong>Step7. Run the test and check result <\/strong><br \/>\nRun the script Table_Search.py file. The Table Search application is launched in the simulator and test runs.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Run_Test.jpg\" data-rel=\"lightbox-image-8\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Run_Test.jpg\" alt=\"Run Test\" width=\"572\" height=\"175\" class=\"aligncenter size-full wp-image-3723\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Run_Test.jpg 572w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/04\/Run_Test-300x92.jpg 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/a><\/p>\n<hr>\n<p>There you have it &#8211; a tutorial to get you started with iOS automation using Appium and Python. <\/p>\n<hr>\n<h3>Hire technical testers from Qxf2<\/h3>\n<p> Qxf2 prides itself on being the home of technical testers who are experts in automation and testing strategy. We specialize in helping startups manage their testing efficiently. Looking for QA consultants who can deliver results from day one? Explore our <a href=\"https:\/\/qxf2.com\/?utm_source=get-set-test-an-ios-app&#038;utm_medium=click&#038;utm_campaign=From%20blog\">range of QA services designed for startups<\/a>, and let&#8217;s discuss how we can assist your team.<\/p>\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=\"1775257936\" \/><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>It has been more than a year since I wrote my first blog on mobile automation using Appium for Android applications. There were many requests to come up with a similar blog for iOS applications. But due to time constraints I couldn&#8217;t get to it. But as they say, better late than never! I finally have a guide to help [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,38,104,47,71,18],"tags":[],"class_list":["post-3707","post","type-post","status-publish","format-standard","hentry","category-appium","category-automation","category-ios","category-mobile","category-mobile-automation","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3707","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=3707"}],"version-history":[{"count":40,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3707\/revisions"}],"predecessor-version":[{"id":22933,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3707\/revisions\/22933"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=3707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=3707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=3707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}