appium---uiautomator定位方法


  前面總結了7種定位方法,今天在介紹一種uiautomator方法,其實appium就是基於uiautomator框架實現的,讓我們一起看下uiautomator有哪些定位方法可以使用

uiautomator是什么

UIAutomator是android的自動化測試框架,也是Android-Sdk中一個查看頁面組件元素工具

uiautomator定位方法

前面介紹了常規的定位方法,這里uiautomator又提供了3種常用的定位方法,這次我們主要通過uiautomator方法進行介紹,定位的話依舊拿淘寶來做實戰

通過Text方法

1、text(“text文本”)

text = 'text("注冊/登錄")'
driver.find_element_by_android_uiautomator(text).click()

2、文本比較長,可以使用textContains模糊查詢定位 textContains('text文本')

text1 = 'textContains("請輸入手機號碼")'
driver.find_element_by_android_uiautomator(text1).send_keys("123456")

3、textStartsWith("以text文本開頭")

text2 = 'textStartsWith("請輸入驗證碼")'
driver.find_element_by_android_uiautomator(text2).send_keys("12234")

運行結果:

由於用的是模擬器可能有點卡頓。抱歉哈

代碼結果:

from appium import webdriver
import time
desired_caps = {
                 'platformName': 'Android',  # 測試版本
                 'deviceName': 'emulator-5554',   # 設備名
                 'platformVersion': '5.1.1', # 系統版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 啟動launch Activity
                "noReset": True,  # 不清空數據
                "unicodeKeyboard": True,    # 使用Unicode編碼方式發送字符串
              "resetKeyboard": True,      # 鍵盤隱藏起來
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(4)
text = 'text("注冊/登錄")'
driver.find_element_by_android_uiautomator(text).click()
time.sleep(5)
text1 = 'textContains("請輸入手機號碼")'
driver.find_element_by_android_uiautomator(text1).send_keys("123456")
time.sleep(5)
text2 = 'textStartsWith("請輸入驗證碼")'
driver.find_element_by_android_uiautomator(text2).send_keys("12234")

Class Name方法

這個方法和appium定位方法一樣都是通過Class屬性進行定位

# 通過class定位登錄按鈕
className = 'className("android.widget.Button")'
driver.find_element_by_android_uiautomator(className).click()

通過uiautomator工具找到注冊/登錄按鈕的Class屬性

resource-id方法

resourceld 和appium中的id一樣。

# 通過resourceId定位輸入框
id = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et")'
driver.find_element_by_android_uiautomator(id).send_keys("123456")

同意的方法通過uiautomator找到搜索框的id

 id和class定位執行結果:

代碼結果:

# coding:utf-8
from appium import webdriver
import time
desired_caps = {
                 'platformName': 'Android',  # 測試版本
                 'deviceName': 'emulator-5554',   # 設備名
                 'platformVersion': '5.1.1', # 系統版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 啟動launch Activity
                "noReset": True,  # 不清空數據
                "unicodeKeyboard": True,    # 使用Unicode編碼方式發送字符串
              "resetKeyboard": True,      # 鍵盤隱藏起來
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(6)
# 通過class定位登錄按鈕
className = 'className("android.widget.Button")'
driver.find_element_by_android_uiautomator(className).click()
time.sleep(5)
# 通過resourceId定位輸入框
id = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et")'
driver.find_element_by_android_uiautomator(id).send_keys("123456")

上面寫了一些單獨的定位方法,其實uiautomator還支持組合定位元素,意思就是支持id和text或者text和className等,這樣的定位更加准確,具體的繼續往下看吧

組合定位

1、id和text方法組合

# 通過text+ClassName組合 (resourceId(屬性).text(屬性))
classText = 'className("android.widget.Button").text("注冊/登錄")'
driver.find_element_by_android_uiautomator(classText).click()

2、class和text方法組合

# 通過text+ID組合 (resourceId(屬性).text(屬性))
IdText = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et").text("請輸入手機號碼")'
driver.find_element_by_android_uiautomator(IdText).send_keys("123456")

執行結果:

# coding:utf-8
from appium import webdriver
import time
desired_caps = {
                 'platformName': 'Android',  # 測試版本
                 'deviceName': 'emulator-5554',   # 設備名
                 'platformVersion': '5.1.1', # 系統版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 啟動launch Activity
                "noReset": True,  # 不清空數據
                "unicodeKeyboard": True,    # 使用Unicode編碼方式發送字符串
              "resetKeyboard": True,      # 鍵盤隱藏起來
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(6)
classText = 'className("android.widget.Button").text("注冊/登錄")'
driver.find_element_by_android_uiautomator(classText).click()
time.sleep(5)
# 通過text+ID組合
IdText = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et").text("請輸入手機號碼")'
driver.find_element_by_android_uiautomator(IdText).send_keys("123456")

父子定位childSelector

 定位的時候我們也可以通過父級找到子級定位

 

 

格式:

# 通過父子定位
(父親屬性).childSelector(定位屬性)
# 通過父子定位
fuzi = 'resourceId("com.taobao.taobao:id/home_searchbar").childSelector(className("android.widget.EditText"))'
driver.find_element_by_android_uiautomator(fuzi).click()

兄弟定位fromParent

定位也可以通過兄弟之間的完成定位

格式:

# 通過兄弟定位
(兄弟屬性).fromParent(定位屬性)
# 通過兄弟元素定位
xiongdi = 'resourceId("com.taobao.taobao:id/photoBtn").fromParent(className("android.widget.EditText"))'
driver.find_element_by_android_uiautomator(xiongdi).send_keys(u"牛仔褲")

 完成代碼:

# coding:utf-8
from appium import webdriver
import time
desired_caps = {
                 'platformName': 'Android',  # 測試版本
                 'deviceName': 'emulator-5554',   # 設備名
                 'platformVersion': '5.1.1', # 系統版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 啟動launch Activity
                "noReset": True,  # 不清空數據
                "unicodeKeyboard": True,    # 使用Unicode編碼方式發送字符串
                "resetKeyboard": True,      # 鍵盤隱藏起來
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(6)
# 通過父子定位
fuzi = 'resourceId("com.taobao.taobao:id/home_searchbar").childSelector(className("android.widget.EditText"))'
driver.find_element_by_android_uiautomator(fuzi).click()
time.sleep(6)
# 通過兄弟定位
xiongdi = 'resourceId("com.taobao.taobao:id/photoBtn").fromParent(className("android.widget.EditText"))'
driver.find_element_by_android_uiautomator(xiongdi).send_keys(u"牛仔褲")

結果:

這個地方沒有設置appium的鍵盤所有導致的是亂碼的。

 元素定位方面方法非常的多,喜歡那種我們用哪種,哪種簡單我們就用那種

 

感覺安靜寫的對您有幫助的話,可以點歌關注,不迷路,有哪里寫錯的或者不懂的可以下方留言!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM