目錄
- 一.認識定位控制元件(元素定位工具)
- 二.功能及按鈕介紹
- 三.Appuim支持的定位方式
- 3.01 id定位
- 3.02 name定位
- 3.03 className定位
- 3.04 classNameMatches定位
- 3.05 textMatches 正則匹配查找
- 3.06 textStartsWith定位
- 3.07 class_name定位
- 3.08父子關系、兄弟關系定位
- 3.09 滾動查找
- 3.10 Accessibility_id定位
- 3.11 XPath定位
- 3.12 contains模糊定位
- 3.13 AndroidUiAutomator定位元素
- 3.14 resourceID定位
- 3.15 resourceIDMatches 定位
- 3.16組合定位
- 3.17 Android UIAutomator定位
- 3.18 支持層級定位
- 3.19 支持部分定位
- 3.20 Android原生支持元素定位方式
- 3.21Appium不支持的元素定位方式
- 3.22 appium與selenium元素定位之比較
一.認識定位控制元件(元素定位工具)
- 該工具位于
android-sdk-windows\tools\uiautomatorviewer.bat雙擊即可啟動,啟動程序中組件所打開的CMD視窗不用關閉;手機開啟USB除錯,或打開模擬器即可使用 - 在使用
Appium的時候,需要填寫Desired Capabilities,這里使用Android SDK自帶的uiautomatorviewer.bat控制元件可以便捷的獲取所需資訊
二.功能及按鈕介紹
-
通過截屏并分析XML布局檔案的方式,為用戶提供控制元件資訊查看服務
-
獲取節點元素相關屬性

-
獲取點位坐標

-
The first button:Open:
Screenshot;UI XML Dump打開本地截圖和打開.uix格式檔案(uix檔案需安裝Binary Data打開) -
Second button:Device Screenshot(uiautomator dump)設備螢屏截圖(uiautomator轉儲)
-
Third button:Device Screenshot with Compressed Hierarchy(uiautomator dump --compressed)具有壓縮層次結構的設備螢屏截圖
-
Save:保存至本地指定位置
-
在Android Studio中可以通過Android Device Monitor 呼叫UI Automator Viewer

-
First button on the right:
+Expand All-全部展開 -
Second button on the right:Toggle NAF Nodes-切換NAF節點,點擊后展示不可被識別的控制元件
-
Third button on the right:Clear search results-清除搜索結果
三.Appuim支持的定位方式
3.01 id定位
- 通過uiautomatorviewer.bat 工具可以查看物件的id屬性;
- 如果目標設備的API Level低于18則UIAutomatorViewer不能獲得對應的Resource ID,只有等于大于18的時候才能使用;
- resource-id 就是的id屬性;
- 使用方法(未驗證):driver.find_element_by_id(‘android:id/text1’).click();driver.findElement(By.id(“com.android.calculator2:id/formula”))

3.02 name定位
一般text屬性認為是name

3.03 className定位
通過呼叫android uiautomator使用className進行定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")')
ele.send_keys('132')
3.04 classNameMatches定位
通過className正則匹配進行定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches (".*EditText")')
ele.send_keys('132')
3.05 textMatches 正則匹配查找
textMatches故名思義就是通過正則的來進行查找定位,他也是通過text的屬性來進行正則匹配
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^請輸入手.*")')
ele.send_keys("123")
3.06 textStartsWith定位
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("請輸入")')
3.07 class_name定位
- 定位一個控制元件class就是class_name ,一般獲得的view都不止一個,所以應該需要遍歷一遍得到的views,然后縮小搜索條件來獲得目標控制元件;如果多個只操作第一個
driver.find_element_by_class_name("com.jingdong.app.mall:id/ic").click() - 定位組控制元件,例如:
com.jingdong.app.mall:id/ic;
driver.find_elements_by_class_name("com.jingdong.app.mall:id/ic")[1].click(),或WebElement button = driver.findElement(By.className(“com.jingdong.app.mall:id/ic”));
3.08父子關系、兄弟關系定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").childSelector(text("密碼"))')
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").fromParent(text("密碼"))')
3.09 滾動查找
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')
3.10 Accessibility_id定位
引數取content-desc的值:driver.find_element_by_accessibility_id('Accessibility').click()或findElement(By.AccessibilityId("sharebutton"))

3.11 XPath定位
- Appium對于xpath定位執行效率是比較低的,也就是說遇到xpath的定位代碼的時候,執行比較慢;迫不得已的情況下盡量不用這個定位方式
(引數取text的值)driver.find_element_by_xpath('//*[@text="Accessibility"]').click()
(引數取resource-id的值)driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()
引數格式:
1)//class
2)//class[n]
3)//class[@屬性=“屬性值”]
4)//class[@屬性=“屬性值” and @屬性=“屬性值”]
5)//*[@屬性=“屬性值”]
例如:com.jingdong.app.mall:id/ic
driver.findElement(By.xpath("//com.jingdong.app.mall:id/ic"))
driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()

agree_continue_xpath = "//*[@text='退出登錄']"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.12 contains模糊定位
用于獲取toast時,toast文本內容較長,可采用contains包含部分文本的匹配方式;以用來模糊匹配上面的文本屬性“退出登錄”

agree_continue_xpath = "//android.widget.Button[contains(@text, '退出')]"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.13 AndroidUiAutomator定位元素
AndroidUIAutomator是一個強有力的元素定位方式,它是通過Android UIAutomator類別庫去找元素,定位方式:findElement(By.AndroidUIAutomator(String UIAuto));
可以選擇id,nameclassName,description作為傳入的字串
WebElement el =
driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.tencent.mm:id/do\")");
3.14 resourceID定位
resourceld定位和appium封裝好的id定位一樣,差別在寫法變成了uiautomator的寫法
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/et_phone_edit")')
ele.send_keys('234')
3.15 resourceIDMatches 定位
通過id進行正則匹配定位
ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".+et_phone_edit")')
ele.send_keys('123')
3.16組合定位
xpath中同時包含class和text兩個屬性

agree_continue_xpath = "//*[@class='android.widget.TextView' and @text='退出登錄']"
WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.17 Android UIAutomator定位
android uiautomator原理是通過android 自帶的android uiautomator的類別庫去查找元素,其實和appium的定位一樣,或者說他比appium的定位方式更佳多以及更佳適用,它也支持id、className、text、模糊匹配等進行定位
3.18 支持層級定位
1.driver.find_element_by_xpath("//android.widget.ListView/android.widget.TextView").click()
2.driver.find_element_by_xpath("//android.widget.ListView/*[1]").click()
3.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[1]').click()在父元素API Demos下,Accessibility是第1個子元素;xpath的索引從1開始
4.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[@resource-id="android:id/text1"]').click()
5.driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()
- 引數格式:
//class[@屬性=“屬性值”]/class[]/class
3.19 支持部分定位
driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()
3.20 Android原生支持元素定位方式
①(引數取resource-id的值)driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")').click()
簡化(默認是UiSelector物件):driver.find_element_by_android_uiautomator('.resourceId("android:id/text1")').click()
例如:WebElement element = driver.findElement(By.id("com.tencent.mm:id/do"));
或driver.findElementById("com.tencent.mm:id/do")
②(引數取class的值)driver.find_element_by_android_uiautomator('.className("android.widget.TextView")').click()
③(引數取text的值)driver.find_element_by_android_uiautomator('.text("Accessibility")').click()
3.21Appium不支持的元素定位方式

3.22 appium與selenium元素定位之比較
appium是app端,用appium需匯出selenium包
selenium是web端
| frame | Element positioning method | remarks |
|---|---|---|
| appium | id, className, AccessibilityId, xpath, AndroidUIAutomator | for H5,Same support selenium name,link,text,css |
| selenium | id, className, name, tag name, link text, paratial link text, xpath ,css |
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/273261.html
標籤:其他
上一篇:P2602 [ZJOI2010]數字計數【數位dp】
下一篇:Enrace的博客
