{"id":539,"date":"2014-06-18T13:56:00","date_gmt":"2014-06-18T17:56:00","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=539"},"modified":"2018-03-06T09:25:39","modified_gmt":"2018-03-06T14:25:39","slug":"selendroid-mobile-automation","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/selendroid-mobile-automation\/","title":{"rendered":"Get started with mobile automation: Selendroid &#038; Python"},"content":{"rendered":"<p><span style=\"font-weight: bold; font-style: inherit;\"><strong>Problem:<\/strong><\/span> There is a perception among testers that getting started with mobile automation is hard.<\/p>\n<h2> Why this post? <\/h2>\n<p>I strongly believe that a tester must have a wide range of skills. You do not need to be an expert in all of them, but it is extremely important to have contemplated a spectrum of concepts and explored a variety of tools. The context under which you test will inform you of the specific choice of testing methodology and test tools to choose. The more options you are aware of, the better your context specific choice is going to be. In this regard, I began noticing that mobile testing has become somewhat of a niche among testers. Testers are reluctant to get started with mobile testing &#8211; probably because they think getting started involves a lot of time and effort. At <a href=\"http:\/\/www.qxf2.com\/?utm_source=selendroid&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2 Services<\/a>, we have decided to tackle the problem head on. This post is the first in a series of quick, easily consumable tutorials geared towards hands-on testers looking to start exploring the mobile testing landscape. <\/p>\n<p><strong>NOTE:<\/strong> In this post we are concentrating only on the &#8220;getting started&#8221; phase. The tools your test team uses to solve the specific and unique problems can greatly vary. There is no one silver bullet.<\/p>\n<hr>\n<h2>Overview<\/h2>\n<p>For this post, we will show you how to get started with <a href=\"http:\/\/selendroid.io\/\">Selendroid<\/a> and Python in less than 2 hours. Selendroid is a test automation framework which drives off the UI of Android native and hybrid applications (apps) and the mobile web. Tests are written using the Selenium 2 client API. Selendroid can be used on emulators and real devices. We will show you how to setup an Android emulator, launch a browser, click a link and verify the title of the new page that loads.<\/p>\n<p>1. Download and install <a href=\"http:\/\/developer.android.com\/sdk\/index.html\">Android-Sdk<\/a><br \/>\n2. Setup your environment variables<br \/>\n3. Start the emulator<br \/>\n4. Write the Python test<br \/>\n5. Download and start <a href=\"https:\/\/github.com\/selendroid\/selendroid\/releases\/download\/0.10.0\/selendroid-standalone-0.10.0-with-dependencies.jar\">Selendroid<\/a><br \/>\n6. Run the test<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 1: Download and Install <a href=\"http:\/\/developer.android.com\/sdk\/index.html\">Android-Sdk<\/a><\/span><br \/>\nFrom <a href=\"http:\/\/developer.android.com\/sdk\/index.html\">Android<\/a> web site, download the latest Android-Sdk. This also installs the AVD Manager, a graphical user interface for creating and managing Android Virtual Devices (AVDs). <\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 2: Setup your environment variables <\/span><br \/>\nMake sure you have <a href=\"https:\/\/www.java.com\/en\/download\/chrome.jsp\">Java<\/a> Installed on your machine. Setup the following system environment variables:<\/p>\n<pre lang=\"HTML\">\r\nANDROID_HOME= $YOUR_SDK_INSTALL_PATH\\sdk\r\nJAVA_HOME=$YOUR_JAVA_HOME (e.g.: C:\\Program Files\\Java\\jdk1.8.0_05)\r\n<\/pre>\n<p>Add <strong>ANDROID_HOME\/tools<\/strong>, <strong>ANDROID_HOME\/platform-tools<\/strong> and <strong>ANDROID_HOME\/build-tools<\/strong> to your PATH environment variable.<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 3: Start the emulator <\/span><br \/>\nFor this tutorial, we are choosing to use the Android emulator. If you do not have an Android device for testing, you can use an Android virtual device. After installing Android SDK and setting up your system variables, run the below command<\/p>\n<pre lang=\"HTML\">\r\nandroid create avd --name Device Name --target android-19 --abi armeabi-v7a\r\n<\/pre>\n<p>Where &#8220;name&#8221; Name of the new AVD created, &#8220;target&#8221; Target ID of the new AVD created and &#8220;abi&#8221; The CPU\/ABI to use for the AVD. You can use the below command to display the list of all available targets. You can then use this details to update the &#8220;target&#8221; and &#8220;abi&#8221;:<\/p>\n<pre lang=\"python\"> android list target <\/pre>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_List_Target.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\/2014\/06\/Android_List_Target.jpg\" alt=\"Output of running the android list target command\" width=\"669\" height=\"336\" class=\"alignnone size-full wp-image-580\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_List_Target.jpg 669w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_List_Target-300x150.jpg 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/a><\/p>\n<p>You can run the below command to view the AVD created:<\/p>\n<pre lang=\"python\">android list avd <\/pre>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_created.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\/2014\/06\/Android_created.jpg\" alt=\"Shows the List of Emulators created\" width=\"664\" height=\"239\" class=\"alignnone size-full wp-image-579\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_created.jpg 664w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_created-300x107.jpg 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><\/p>\n<p>You can run the below command to verify that the AVD is working:<\/p>\n<pre lang=\"python\">emulator -avd Default<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 4: Write the Python test <\/span><br \/>\nWe are going to be using a chess website called <a href=\"http:\/\/www.chessdom.com\/\">Chessdom<\/a> to write our test. We will launch a browser, click on the &#8216;Chess News&#8217; link and verify the title of the page that loads. The code should look something like the snippet below<\/p>\n<pre lang=\"python\">\r\n\"\"\"\r\nQxf2 Services: Sample Python script to get started with Selendroid\r\n\"\"\"\r\nimport unittest, time\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.common.touch_actions import TouchActions\r\n\r\n\r\nclass FindElementTest(unittest.TestCase):\r\n\r\n    def setUp(self):\r\n        \"Set up the Android Driver\"\r\n        self.driver  = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.ANDROID)\r\n        self.driver.implicitly_wait(30)\r\n\r\n    def test_Launch_Chessdom(self):\r\n        \"Test the title of the Chess News page on Chessdom.com is correct\"\r\n        # Go to url:http:\/\/www.chessdom.com\/ \r\n        self.driver.get('http:\/\/www.chessdom.com\/')\r\n        time.sleep(5)\r\n        \r\n        #Click on the link \"Chess News\"\r\n        ChessNews = self.driver.find_element_by_xpath(\"\/\/a[@title='View all posts filed under Chess News' and text()='Chess News']\")\r\n        ChessNews.click()\r\n        time.sleep(5) #Increase sleep time if your emulator has performance issues\r\n        \r\n        # Assert that the Page has title \"Chess News | Chessdom\"\r\n        self.assertIn(\"Chess News | Chessdom\", self.driver.title)\r\n\r\n        \r\n    def tearDown(self):\r\n        \"Tear down the test\"\r\n        self.driver.quit()\r\n\r\nif __name__ == '__main__':\r\n        unittest.main()\r\n<\/pre>\n<p>Why is chess there in this example? Because, I love chess. I named Qxf2 Services after Black&#8217;s fantastic 21st move in <a href=\"http:\/\/www.chessgames.com\/perl\/chessgame?gid=1477101\">this masterpiece<\/a> by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hikaru_Nakamura\">Hikaru Nakamura<\/a>. <\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 5: Download and start <a href=\"https:\/\/github.com\/selendroid\/selendroid\/releases\/download\/0.10.0\/selendroid-standalone-0.10.0-with-dependencies.jar\">Selendroid<\/a><\/span><br \/>\nDownload Selendroid and cd to the folder where you downloaded the jar. Spin up a new command prompt and run the command below to start Selendroid <\/p>\n<pre lang=\"python\">java -jar selendroid-standalone-0.10.0-with-dependencies.jar<\/pre>\n<p>Selendroid-standalone will start an http server on port 4444 and will scan all Android virtual devices<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Selendroid.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\/2014\/06\/Selendroid.jpg\" alt=\"Selendroid\" width=\"667\" height=\"383\" class=\"alignnone size-full wp-image-574\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Selendroid.jpg 667w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Selendroid-300x172.jpg 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 6: Run the test<\/span> <\/p>\n<p>You can run the test script the normal way you do. We run it via the command prompt.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Running_Test.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\/2014\/06\/Running_Test.jpg\" alt=\"Running Test\" width=\"570\" height=\"119\" class=\"alignnone size-full wp-image-573\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Running_Test.jpg 570w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Running_Test-300x62.jpg 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p>You can also view the test running in the Android emulator<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_Emulator.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\/2014\/06\/Android_Emulator.jpg\" alt=\"Android Emulator\" width=\"813\" height=\"748\" class=\"alignnone size-full wp-image-559\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_Emulator.jpg 813w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/06\/Android_Emulator-300x276.jpg 300w\" sizes=\"auto, (max-width: 813px) 100vw, 813px\" \/><\/a><\/p>\n<hr>\n<p>There you have it! A tutorial to get you started with Selendroid in under two hours.<\/p>\n<p><strong>Note:<\/strong> To save time writing your mobile automation tests, we recommend you start out with our open-sourced <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">test automation framework in Python<\/a>. <\/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=\"1776395591\" \/><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: There is a perception among testers that getting started with mobile automation is hard. Why this post? I strongly believe that a tester must have a wide range of skills. You do not need to be an expert in all of them, but it is extremely important to have contemplated a spectrum of concepts and explored a variety of [&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,38,15,47,71,18,46,30],"tags":[],"class_list":["post-539","post","type-post","status-publish","format-standard","hentry","category-android","category-automation","category-how-to","category-mobile","category-mobile-automation","category-python","category-selendroid","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/539","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=539"}],"version-history":[{"count":43,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"predecessor-version":[{"id":8687,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/539\/revisions\/8687"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}