app自動(dòng)化測試(Android)–App 控件定位

app自動(dòng)化測試(Android)–App 控件定位

更多技術(shù)文章

客戶端的頁面通過 XML 來實(shí)現(xiàn) UI 的布局,頁面的 UI 布局作為一個(gè)樹形結(jié)構(gòu),而樹葉被定義為節(jié)點(diǎn)。這里的節(jié)點(diǎn)也就對應(yīng)了要定位的元素,節(jié)點(diǎn)的上級節(jié)點(diǎn),定義了元素的布局結(jié)構(gòu)。在 XML 布局中可以使用 XPath 進(jìn)行節(jié)點(diǎn)的定位。

App的布局結(jié)構(gòu)

[[圖片上傳失敗...(image-593212-1654652224421)]

1080×607 155 KB](https://ceshiren.com/uploads/default/original/3X/1/d/1d0c729e53157c8761d85ea68520872e48e72b96.png)

從上面這張圖中可以看到最左側(cè)是應(yīng)用的頁面的展示,中間部分展示了這個(gè)頁面的樹形結(jié)構(gòu)的 XML 代碼。

其中包含的內(nèi)容為:

  • 節(jié)點(diǎn) node

  • 節(jié)點(diǎn)屬性:包括 clickable(是否可點(diǎn)擊)、content-desc(內(nèi)容)、resource-id(元素 id)、text(文本)、bounds(坐標(biāo))等。

通過 ID 定位

在 Android 系統(tǒng)元素的 ID 稱為 resource-id,使用頁面分析工具比如 Appium Inspector 能夠獲取元素的唯一標(biāo)識是 ID 屬性,可以使用 ID 進(jìn)行元素定位,方便快捷。

示例代碼如下:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element(By.ID, "android:id/text1").click() </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElement(By.id("android:id/text1")).click(); </pre>

注意 resource-id 對應(yīng)的屬性(包名:id/id 值),在使用這個(gè)屬性的時(shí)候要把它當(dāng)作一個(gè)整體。

通過 Accessibility 定位

當(dāng)分析工具能抓取到的 content-desc 的屬性值是唯一時(shí),可以采用 Accessibility 的定位方式,示例代碼:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element_by_accessibility_id("Accessibility") </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElementByAccessibilityId("Accessibility"); </pre>

通過 XPath 定位

與 Selenium 類似,可以使用 XPath 的定位方式完成頁面的元素定位。XPath 分為絕對路徑定位與相對路徑定位兩種形式,下面介紹的都是相對定位的形式。

XPath:resource-id 屬性定位

元素可以通過 resource-id 定位。

格式:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">//*[@resource-id='resource-id屬性'] </pre>

示例代碼:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element(By.XPATH, \ '//*[@resource-id="rl_login_phone"]') </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElement(By.xpath(\ "http://*[@resource-id=\"rl_login_phone\"]")); </pre>

XPath:text 屬性定位

元素可以通過 text 文本屬性定位。

格式:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">//*[@text=’text文本屬性’] </pre>

示例代碼如下

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element(By.XPATH,'//*[@text="我的"]') </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElement(By.xpath("http://*[@text=\"我的\"]")); </pre>

XPath:class 屬性定位

元素可以通過 class 定位。

格式:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">//*[@class=’class 屬性’] </pre>

示例代碼:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element(By.XPATH,\ '//*[@class="android.widget.EditText"]') </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElement(By.xpath(\ "http://*[@class=\"android.widget.EditText\"]")); </pre>

XPath:content-desc 屬性定位

元素可以通過 content-desc 定位。

格式:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">//*[@content-desc='content-desc 屬性'] </pre>

示例代碼:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.find_element((By.XPATH,\ '//*[@content-desc="搜索"]') </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">driver.findElement(By.xpath(\ "http://*[@content-desc=\"搜索\"]"); </pre>

uiautomatorviewer介紹

使用 Android SDK(sdk/tools/uiautomatorviewer)路徑下自帶的 uiautomatorviewer 工具也可以抓取當(dāng)前頁面的元素。

提前配置 sdk/tools/ 路徑到環(huán)境變量 $PATH 中,直接在命令行輸入下面的命令:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">uiautomatorviewer </pre>

可以打開下面這樣一個(gè)頁面,點(diǎn)擊頁面左上角第二個(gè)圖標(biāo)(Android 手機(jī)圖標(biāo)),就可以獲取下面的 uiautomatorviewer 快照圖:

[[圖片上傳失敗...(image-78056b-1654652224421)]

1066×756 158 KB](https://ceshiren.com/uploads/default/original/3X/0/7/07e4f97a5ec92b5923d086a0371626307e879c08.png)

uiautomatorviewer 抓取快照展示出來的元素屬性是經(jīng)過解析的,如果想要查看 XML DOM 的真實(shí)結(jié)構(gòu)可以打印 pagesource ,得到的內(nèi)容如下,紅色框起來的部分為上圖的定位的 XML DOM 中的一個(gè)節(jié)點(diǎn):

[[圖片上傳失敗...(image-4ea33f-1654652224421)]

975×299 55.5 KB](https://ceshiren.com/uploads/default/original/3X/b/0/b0c1331009c2047a263fcd316c65bd7f31455f71.png)

通過圖片分析,android.widget.TextView 是文本類型的節(jié)點(diǎn),其中包含的屬性信息都在上面的 uiautomatorviewer 快照圖中有展示。如果只想定位 Android 系統(tǒng)的頁面元素,可以直接使用 uiautomatorviewer,速度快并且不需要配置任何參數(shù),直接點(diǎn)擊獲取頁面的圖標(biāo)就可以將客戶端頁面抓取出來。

另外,uiautomatorviewer 只能抓取 android8 以下的版本,如果要抓取 android8 以上的版本的頁面信息,可以使用 Appium Inspector 或 WEditor。

更多技術(shù)文章

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容