{"id":1762,"date":"2014-10-21T01:04:34","date_gmt":"2014-10-21T05:04:34","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=1762"},"modified":"2018-03-05T09:25:18","modified_gmt":"2018-03-05T14:25:18","slug":"identify-ui-elements-mobile-apps","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/identify-ui-elements-mobile-apps\/","title":{"rendered":"Ways to identify UI elements in mobile apps"},"content":{"rendered":"<p><strong>Problem:<\/strong> Many testers do not know how to identify UI elements in mobile apps.<\/p>\n<p>In this post, I explain about different ways to find UI elements in Android&#8217;s calculator app. I also show you examples of using Appium&#8217;s API with the different locator strategies. <\/p>\n<hr>\n<h3>Why this post?<\/h3>\n<p>Identifying UI elements is a key component of writing UI automation. Most testers know how to inspect a web application and uniquely identify the UI elements that they want. However, inspecting and identifying UI elements in a mobile application is still somewhat of a dark art for most testers. Case in point &#8211; when I was learning Appium, it was hard to find a single article that showed me the various options I have to identify UI elements. This post aims to rectify this problem. In this post you will learn different ways to identify and interact with UI elements in a mobile application. <\/p>\n<hr>\n<h3>uiautomatorviewer: Mobile&#8217;s Firebug<\/h3>\n<p>If you automate web application testing, you are sure to have used either developer tools or Firebug. uiautomatorviewer is a GUI tool to scan and analyze the UI components of an Android app. uiautomatorviewer comes with the Android SDK. Please make sure to <a href=\"http:\/\/developer.android.com\/sdk\/installing\/index.html?pkg=adt\">install Android SDK<\/a> to follow along. <\/p>\n<p>To launch uiautomatorviewer, pull up your command prompt, navigate to your <em>$android-sdk\/tools<\/em> directory and issue the command <em>uiautomatorviewer<\/em>. Launch uiautomatorviewer and connect your Android device to your machine. Launch the calculator app. In uiautomatorviewer, click on the <em>Device Screenshot<\/em> button (top left) to take the snapshot of the calculator app. You can now inspect the layout hierarchy and view the properties of the individual UI components that are displayed on the device.<\/p>\n<figure id=\"attachment_1896\" aria-describedby=\"caption-attachment-1896\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/calculator_uiautomatorviewer.png\" 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\/10\/calculator_uiautomatorviewer-1024x542.png\" alt=\"Screenshot of calculator app using uiautomatorviewer\" width=\"1024\" height=\"542\" class=\"size-large wp-image-1896\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/calculator_uiautomatorviewer-1024x542.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/calculator_uiautomatorviewer-300x159.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/10\/calculator_uiautomatorviewer.png 1360w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-1896\" class=\"wp-caption-text\">Figure 1. Inspecting a mobile application<\/figcaption><\/figure>\n<p>NOTE 1: For more details on connecting your Android device to your computer, please refer to the Step 2 of our <a href=\"https:\/\/qxf2.com\/blog\/appium-tutorial-python-physical-device\/\">previous<\/a> blog.<\/p>\n<hr>\n<h3> Different ways of finding UI elements <\/h3>\n<p>Figure 1 is your path to happiness. You can use the properties of the elements found in the <strong>Node Detail<\/strong> table like text, class, resource-id, content-desc etc to find the UI components of the app. Here are a few locator strategies that map well with Appium&#8217;s API:<br \/>\n1. Finding element by xpath in mobile application<br \/>\n2. Finding mobile element by Android UIAutomator<\/p>\n<p><strong>1. Finding element by xpath in mobile applications<\/strong><br \/>\nTo write your xpath:<br \/>\na. start with the <strong>class<\/strong> <code>\/\/class<\/code><br \/>\nb. [OPTIONAL] add attributes <code>@attribute=\"blah\"<\/code><br \/>\nE.g.: XPath to identify the number 7 in the calculator app is<\/p>\n<pre lang=\"python\">\/\/android.widget.Button[@text='7']<\/pre>\n<p>With Python and Appium, your corresponding call would be:  <\/p>\n<pre lang=\"python\">driver.find_element_by_xpath(\"\/\/android.widget.Button[@text='7']\")<\/pre>\n<p><strong> 2. Finding mobile elements by Android UIAutomator <\/strong><br \/>\nAnother way to identify UI elements in a mobile application is via UiSelector. To do so,<br \/>\na. start with a new UiSelector object <code>new UiSelector()<\/code><br \/>\nb. dot-add any of the other properties of the element <code>.text()<\/code><br \/>\nE.g.: To identify the number 7 in the calculator app:<\/p>\n<pre lang=\"python\">new UiSelector().text(\"7\")<\/pre>\n<p>With Python and Appium, your corresponding call would be:<\/p>\n<pre lang=\"python\">driver.find_element_by_android_uiautomator('new UiSelector().text(\"7\")')<\/pre>\n<p>NOTE: This method uses properties such as text, content-desc, resorurce id etc to find elements. Please refer to <a href=\"http:\/\/developer.android.com\/tools\/help\/uiautomator\/UiSelector.html\">this<\/a> link for more details.<\/p>\n<p>Here are a few shortcuts to identifying elements that are specific to Appium:<br \/>\n1. Via accessibility ID: On Android, you can use the string in content-desc.<br \/>\nE.g.: To identify the delete button in the calculator,<\/p>\n<pre lang=\"python\">driver.find_element_by_accessibility_id('delete')<\/pre>\n<p>2. Finding element by Id: Corresponds to the resource-id field<br \/>\nE.g.: To identify the number 7 in the calculator app<\/p>\n<pre lang=\"python\">driver.find_element_by_id(\"com.android.calculator2:id\/digit7\")<\/pre>\n<p>3. Finding element by class name<br \/>\nE.g.: To find the text\/results box in the calculator app<\/p>\n<pre lang=\"python\">driver.find_element_by_class_name(\"android.widget.EditText\")<\/pre>\n<hr>\n<h3>An example Appium + Python script with different locator strategies<\/h3>\n<p>I am providing a fully functioning Python script for you to practice your locator strategies. Challenge yourself to identify different elements in the calculator app and make corresponding changes to this script.<\/p>\n<pre lang=\"python\">\r\n\"\"\"\r\nQxf2: Example script to run one test against calculator app\r\nThe test will show you how you can find UI elements by various methods like xpath, id, accessibility_id and android UIautomator\r\non a android calculator app\r\n\r\n\"\"\"\r\nimport os\r\nimport unittest, time\r\nfrom appium import webdriver\r\nfrom time import sleep\r\n\r\nclass Android_Calculator(unittest.TestCase):\r\n    \"Class to run tests for android calculator\"\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.4'\r\n        desired_caps['deviceName'] = 'Moto E'\r\n        # Get the package and activity name of the calculator to launch the calculator app\r\n        desired_caps['appPackage'] = 'com.android.calculator2'\r\n        desired_caps['appActivity'] = 'com.android.calculator2.Calculator'\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_calculator(self):\r\n        \"Testing android calculator\"\r\n        self.driver.implicitly_wait(10)\r\n        # Find the UI element using xpath  \r\n        self.driver.find_element_by_xpath(\"\/\/android.widget.Button[@text='7']\").click()\r\n        self.driver.find_element_by_xpath(\"\/\/android.widget.Button[@resource-id='com.android.calculator2:id\/mul']\").click()\r\n\r\n        # Find UI element using id\r\n        self.driver.find_element_by_id(\"com.android.calculator2:id\/digit7\").click()\r\n        \r\n        # Find UI element using accessibility_id\r\n        self.driver.find_element_by_accessibility_id('delete').click()\r\n        \r\n        # Find UI element using android UIautomator\r\n        self.driver.find_element_by_android_uiautomator('new UiSelector().text(\"3\")').click()\r\n        self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId(\"com.android.calculator2:id\/equal\")').click()\r\n\r\n        # Find UI element using class name\r\n        result=self.driver.find_element_by_class_name(\"android.widget.EditText\")\r\n        print result.get_attribute('text')\r\n               \r\n#---START OF SCRIPT\r\nif __name__ == '__main__':\r\n    suite = unittest.TestLoader().loadTestsFromTestCase(Android_Calculator)\r\n    unittest.TextTestRunner(verbosity=2).run(suite)\r\n<\/pre>\n<hr>\n<p>If you have read so far, you may be interested in our open-sourced <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">Python-based mobile automation framework<\/a>.Happy inspecting! Happy locating! <\/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=\"1776688795\" \/><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: Many testers do not know how to identify UI elements in mobile apps. In this post, I explain about different ways to find UI elements in Android&#8217;s calculator app. I also show you examples of using Appium&#8217;s API with the different locator strategies. Why this post? Identifying UI elements is a key component of writing UI automation. Most testers [&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,38,15,47,71,18,45],"tags":[],"class_list":["post-1762","post","type-post","status-publish","format-standard","hentry","category-android","category-appium","category-automation","category-how-to","category-mobile","category-mobile-automation","category-python","category-xpath"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1762","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=1762"}],"version-history":[{"count":33,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1762\/revisions"}],"predecessor-version":[{"id":8681,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/1762\/revisions\/8681"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=1762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=1762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=1762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}