App自動化測試之元素定位
通過uiautomatorviewer輔助元素定位工具,我們可以看到微信注冊的注冊按鈕元素對應的信息:
text:元素的文本信息
resource-id:元素的ID
class:元素的className
package:元素所在的包名
content-desc:
bounds:元素的坐標
1、通過id進行定位(app中的id不一定是唯一的)
driver.find_element_by_id("com.tencent.mm:id/e4c")
2、通過className進行定位
driver.find_element_by_class_name("android.widget.Button")
3、通過xpath進行定位(在App當中text是用@text表示,不是text())
driver.find_element_by_xpath('//android.widget.Button[@resource-id=\"com.tencent.mm:id/e4c\"]')
4、通過content-desc進行定位(有些元素中可能content-desc為空)
driver.find_element_by_accessibility_id('')
5、通過uiautomator進行定位(這是安卓的原生定位方式,快)
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tencent.mm:id/e4c")')
附:UiSelector API 詳細介紹
1、資源ID定位對象
返回值 | API | 說明 |
---|---|---|
UiSelector | resourceId(String id) | 資源ID |
UiSelector | resourceIdMatches(String regex) | 資源ID正則 |
2、文本屬性定位對象
返回值 | API | 說明 |
---|---|---|
UiSelector | text(String text) | 文本 |
UiSelector | textContains(String text) | 文本包含 |
UiSelector | textMatches(String regex) | 文本正則 |
UiSelector | textStartsWith(String text) | 文本起始匹配 |
3、描述屬性定位對象
返回值 | API | 說明 |
---|---|---|
UiSelector | description(String desc) | 描述 |
UiSelector | descriptionContains(String desc) | 描述包含 |
UiSelector | descriptionMatches(String regex) | 描述正則 |
UiSelector | descriptionStartsWith(String desc) | 描述開始字符匹配 |
4、索引與實例屬性定位對象
索引與實例說明
1)索引index:指在同級中的編號,在兄弟類中的主鍵的編號
2)實例instance:整個布局文件中的編號,同一個類(比如6.2中的view類)的同級編號
返回值 | API | 說明 |
---|---|---|
UiSelector | index(int index) | 索引 |
UiSelector | instance(int instance) | 實例 |
5、特殊屬性定位對象
返回值 | API | 說明 |
---|---|---|
UiSelector | checked(booleean val) | 選擇屬性 |
UiSelector | clickable(boolean val) | 可點擊屬性 |
UiSelector | enabled(boolean val) | enabled屬性 |
UiSelector | focusable(boolean val) | 焦點屬性 |
UiSelector | focused(boolean val) | 當前焦點屬性 |
UiSelector | longClickable(boolean val) | 長按屬性 |
UiSelector | scrollable(boolean val) | 滾動屬性 |
UiSelector | selected(boolean val) | 背景選擇屬性 |
6、節點屬性定位對象
返回值 | API | 說明 |
---|---|---|
UiSelector | childSelector(UiSelector selector) | 從當前類中往下遞歸找符合條件的子類組件 |
UiSelector | formPrent(UiSelector selector) | 從父類往下遞歸找符合條件的組件 |