{"id":792,"date":"2023-02-28T11:17:20","date_gmt":"2023-02-28T03:17:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=792"},"modified":"2023-04-05T17:44:26","modified_gmt":"2023-04-05T09:44:26","slug":"appium-automation-testing-positioning-control","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/28\/appium-automation-testing-positioning-control\/","title":{"rendered":"Appium\u81ea\u52a8\u5316\u6d4b\u8bd5\uff088\uff09\u2014\u2014 \u5b9a\u4f4d\u63a7\u4ef6"},"content":{"rendered":"<p>Appium \u901a\u8fc7 uiautomatorviewer.bat \u5de5\u5177\u6765\u67e5\u770b\u63a7\u4ef6\u7684\u5c5e\u6027\u3002\u8be5\u5de5\u5177\u4f4d\u4e8e Android SDK \u7684 \/tools\/bin\/ \u76ee\u5f55\u4e0b\u3002<\/p>\n<h2>id \u5b9a\u4f4d<\/h2>\n<p>\u901a\u8fc7uiautomatorviewer.bat \u5de5\u5177\u53ef\u4ee5\u67e5\u770b\u5bf9\u8c61\u7684id\u5c5e\u6027\u3002<\/p>\n<p><!-- more --><\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u76ee\u6807\u8bbe\u5907\u7684API Level\u4f4e\u4e8e18\u5219UIAutomatorViewer\u4e0d\u80fd\u83b7\u5f97\u5bf9\u5e94\u7684Resource ID\uff0c\u53ea\u6709\u7b49\u4e8e\u5927\u4e8e18\u7684\u65f6\u5019\u624d\u80fd\u4f7f\u7528\u3002<\/p>\n<\/blockquote>\n<p>\u6253\u5f00uiautomatorviewer.bat\u5de5\u5177\uff0cresource-id \u5c31\u662f\u6211\u4eec\u7406\u89e3\u7684id\u5c5e\u6027\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">driver.findElement(By.id(&quot;com.android.calculator2:id\/formula&quot;))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_id.png\" alt=\"id \u5b9a\u4f4d\" \/><\/p>\n<h2>name \u5b9a\u4f4d<\/h2>\n<p>\u6253\u5f00uiautomatorviewer.bat\u5de5\u5177\uff0ctext \u5c31\u662f\u6211\u4eec\u8981\u67e5\u627e\u7684name\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">driver.findElement(By.name(&quot;9&quot;))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_name.png\" alt=\"name \u5b9a\u4f4d\" \/><\/p>\n<h2>class name \u5b9a\u4f4d<\/h2>\n<p>\u8ba1\u7b97\u5668\u754c\u9762\u4e0a\u7684\u7684class\u5c5e\u6027\u662f\uff1aandroid.widget.Button\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">WebElement button = driver.findElement(By.className(&quot;android.widget.Button&quot;));<\/code><\/pre>\n<p>\u4f7f\u7528 Class Name \u4e00\u822c\u83b7\u5f97\u7684 view \u90fd\u4e0d\u6b62\u4e00\u4e2a\uff0c\u6240\u4ee5\u5e94\u8be5\u9700\u8981\u904d\u5386\u4e00\u904d\u5f97\u5230\u7684 views\uff0c\u7136\u540e\u7f29\u5c0f\u641c\u7d22\u6761\u4ef6\u6765\u83b7\u5f97\u76ee\u6807\u63a7\u4ef6\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_class_name.png\" alt=\"class name \u5b9a\u4f4d\" \/><\/p>\n<h2>XPath \u5b9a\u4f4d<\/h2>\n<p>\u5728 WebDriver \u4e0a XPath \u5b9a\u4f4d\u662f\u529f\u80fd\u5f3a\u5927\u7684\u4e00\u79cd\u5b9a\u4f4d\u65b9\u5f0f\u3002\u6211\u4e2a\u4eba\u60ef\u7528\u4e8e\u6b64\u65b9\u6cd5\u6765\u5b9a\u4f4dWeb\u9875\u9762\u4e0a\u7684\u5143\u7d20\u3002\u4e0b\u9762\u770b\u770b\u5728 Android \u4e0a XPath \u5b9a\u4f4d\u7684\u7528\u6cd5\u3002<\/p>\n<p>\u7528class\u7684\u5c5e\u6027\u6765\u66ff\u4ee3\u505a\u6807\u7b7e\u7684\u540d\u5b57\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">driver.findElement(By.xpath(&quot;\/\/android.view.ViewGroup\/android.widget.Button&quot;))  \/\/7<\/code><\/pre>\n<p>\u5f53\u679c\u5982\u679c\u51fa\u73b0class \u76f8\u540c\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528\u63a7\u4ef6\u7684\u5c5e\u6027\u503c\u8fdb\u884c\u533a\u5206\u3002<\/p>\n<pre><code class=\"language-java\">driver.findElement(By.xpath(&quot;\/\/android.widget.Button[contains(@index,0)]&quot;)).click(); \/\/7\ndriver.findElement(By.xpath(&quot;\/\/android.widget.Button[contains(@content-desc,&#039;times&#039;)]&quot;)).click(); \/\/*\ndriver.findElement(By.xpath(&quot;\/\/android.widget.Button[contains(@text,&#039;7&#039;)]&quot;)).click();  \/\/7\ndriver.findElement(By.xpath(&quot;\/\/android.widget.Button[contains(@content-desc,&#039;equals&#039;)]&quot;)).click(); \/\/=<\/code><\/pre>\n<p>XPath \u5728 Appium \u4e0a\u7684\u7528\u6cd5\u4f9d\u7136\u5f88\u5f3a\u5927\uff0c\u6709\u65f6\u9700\u8981\u5199\u66f4\u957f\u7684\u5b9a\u4f4d\u8bed\u6cd5\uff0c\u56e0\u4e3aAPP\u4e0a\u5143\u7d20\u7684class\u547d\u4ee4\u672c\u6765\u5c31\u957f\uff0c\u518d\u52a0\u4e0a\u591a\u5c42\u7ea7\uff0c\u7ed3\u679c\u53ef\u60f3\u800c\u77e5\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_xpath.png\" alt=\"XPath \u5b9a\u4f4d\" \/><\/p>\n<h2>Accessibility ID\u5b9a\u4f4d<\/h2>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5c5e\u4e8eAppium\u6269\u5c55\u7684\u5b9a\u4f4d\u65b9\u6cd5\u3002\u6838\u5fc3\u662f\u8981\u627e\u5230\u5143\u7d20\u7684contentDescription\u5c5e\u6027\uff0c\u5373\u5143\u7d20\u7684 content-desc \u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">driver.findElementByAccessibilityId(&quot;plus&quot;).click();<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_ac_id.png\" alt=\"Accessibility ID\u5b9a\u4f4d\" \/><\/p>\n<h2>uiautomator\u5b9a\u4f4d<\/h2>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u5c5e\u4e8e Appium\uff08Android\uff09\u6269\u5c55\u7684\u5b9a\u4f4d\u65b9\u6cd5\u3002\u540c\u6837\u4f7f\u7528 UIAutomatorViewer.bat \u5de5\u5177\u76f4\u63a5\u67e5\u770b\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u5143\u7d20\u7684\u4efb\u610f\u5c5e\u6027\u90fd\u53ef\u4ee5\u901a\u8fc7android uiautomator\u65b9\u6cd5\u6765\u8fdb\u884c\u5b9a\u4f4d\uff0c\u4f46\u8981\u4fdd\u8bc1\u8fd9\u79cd\u5b9a\u4f4d\u65b9\u5f0f\u7684\u552f\u4e00\u6027\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">driver.findElementByAndroidUIAutomator(&quot;new UiSelector().text(\\&quot;clr\\&quot;)&quot;).click();\ndriver.findElementByAndroidUIAutomator(&quot;new UiSelector().text(\\&quot;8\\&quot;)&quot;).click();\ndriver.findElementByAndroidUIAutomator(&quot;new UiSelector().description(\\&quot;plus\\&quot;)&quot;).click();\ndriver.findElementByAndroidUIAutomator(&quot;new UiSelector().text(\\&quot;5\\&quot;)&quot;).click();\ndriver.findElementByAndroidUIAutomator(&quot;new UiSelector().description(\\&quot;equals\\&quot;)&quot;).click();<\/code><\/pre>\n<blockquote>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f description() \u65b9\u6cd5\u7528\u7684\u662fcontent-desc\u5c5e\u6027<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/appium_find_element_uiautomator.png\" alt=\"uiautomator\u5b9a\u4f4d\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Appium \u901a\u8fc7 uiautomatorviewer.bat \u5de5\u5177\u6765\u67e5\u770b\u63a7\u4ef6\u7684\u5c5e\u6027\u3002\u8be5\u5de5\u5177\u4f4d\u4e8e Androi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[223],"tags":[],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-appium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=792"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}