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

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

更多技術(shù)文章

客戶(hù)端的頁(yè)面通過(guò) XML 來(lái)實(shí)現(xiàn) UI 的布局亮钦,頁(yè)面的 UI 布局作為一個(gè)樹(shù)形結(jié)構(gòu),而樹(shù)葉被定義為節(jié)點(diǎn)。這里的節(jié)點(diǎn)也就對(duì)應(yīng)了要定位的元素销斟,節(jié)點(diǎn)的上級(jí)節(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)用的頁(yè)面的展示轰枝,中間部分展示了這個(gè)頁(yè)面的樹(shù)形結(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))等。

通過(guò) ID 定位

在 Android 系統(tǒng)元素的 ID 稱(chēng)為 resource-id果复,使用頁(yè)面分析工具比如 Appium Inspector 能夠獲取元素的唯一標(biāo)識(shí)是 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 對(duì)應(yīng)的屬性(包名:id/id 值)走搁,在使用這個(gè)屬性的時(shí)候要把它當(dāng)作一個(gè)整體独柑。

通過(guò) 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>

通過(guò) XPath 定位

與 Selenium 類(lèi)似忌栅,可以使用 XPath 的定位方式完成頁(yè)面的元素定位。XPath 分為絕對(duì)路徑定位與相對(duì)路徑定位兩種形式曲稼,下面介紹的都是相對(duì)定位的形式索绪。

XPath:resource-id 屬性定位

元素可以通過(guò) 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 屬性定位

元素可以通過(guò) 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 屬性定位

元素可以通過(guò) 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 屬性定位

元素可以通過(guò) 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)前頁(yè)面的元素窄坦。

提前配置 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>

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

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

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

uiautomatorviewer 抓取快照展示出來(lái)的元素屬性是經(jīng)過(guò)解析的拴袭,如果想要查看 XML DOM 的真實(shí)結(jié)構(gòu)可以打印 pagesource ,得到的內(nèi)容如下曙博,紅色框起來(lá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)

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

另外,uiautomatorviewer 只能抓取 android8 以下的版本杆融,如果要抓取 android8 以上的版本的頁(yè)面信息楞卡,可以使用 Appium Inspector 或 WEditor。

更多技術(shù)文章

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末脾歇,一起剝皮案震驚了整個(gè)濱河市蒋腮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌藕各,老刑警劉巖池摧,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異激况,居然都是意外死亡作彤,警方通過(guò)查閱死者的電腦和手機(jī)膘魄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)竭讳,“玉大人创葡,你說(shuō)我怎么就攤上這事〈蹋” “怎么了蹈丸?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)呐芥。 經(jīng)常有香客問(wèn)我,道長(zhǎng)奋岁,這世上最難降的妖魔是什么思瘟? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮闻伶,結(jié)果婚禮上滨攻,老公的妹妹穿的比我還像新娘。我一直安慰自己蓝翰,他們只是感情好光绕,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著畜份,像睡著了一般诞帐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上爆雹,一...
    開(kāi)封第一講書(shū)人閱讀 52,584評(píng)論 1 312
  • 那天停蕉,我揣著相機(jī)與錄音,去河邊找鬼钙态。 笑死慧起,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的册倒。 我是一名探鬼主播蚓挤,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼驻子!你這毒婦竟也來(lái)了灿意?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拴孤,失蹤者是張志新(化名)和其女友劉穎脾歧,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體演熟,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鞭执,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年司顿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兄纺。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡大溜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出估脆,到底是詐尸還是另有隱情钦奋,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布疙赠,位于F島的核電站付材,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏圃阳。R本人自食惡果不足惜厌衔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望捍岳。 院中可真熱鬧富寿,春花似錦、人聲如沸锣夹。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)银萍。三九已至变勇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砖顷,已是汗流浹背贰锁。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滤蝠,地道東北人豌熄。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像物咳,于是被迫代替她去往敵國(guó)和親锣险。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容