1. 程式人生 > >Robotframework獲取移動端toast問題

Robotframework獲取移動端toast問題

背景:

在做移動端自動化測試的時候,經常會遇到一個問題就是獲取toast提示問題,如果需要解決這個問題需要重新處理,不能按照正常的邏輯,使用robotframework自帶的關鍵字進行獲取,需要重新考慮新的處理獲取模式

一 環境配置

如果環境配置OK直接跳到第6步安裝uiautomator2

1.下載Appium最新版

地址:https://github.com/appium/appium-desktop/releases

 

2.下載Python,並安裝

3.下載Pycharm

4.下載Android SDK

5.安裝uiautomator2

-  安裝NPM映象,地址:https://npm.taobao.org/

-  執行命令:npm install -g cnpm --registry=https://registry.npm.taobao.org

-  安裝uiautomator2的配置檔案執行命令:cnpm install appium-uiautomator2-driver

環境搭建好後,進行獲取toast

 1 進入appium安裝根目錄,D:\Python27\Lib\site-packages\robotframework_appiumlibrary-1.5-py2.7.egg\AppiumLibrary\keywords,找到_element.py進行修改檔案
 2 ,加入這段語句
 3 def toast_open_application(self,automationName,platformName,platformVersion,deviceName,uiid,appPackage,appActivity,unicodeKeyboard,resetKeyboard,noReset):
 4 
 5         desired_caps={}
 6         desired_caps['automationName']=automationName
 7         desired_caps["platformName"] = platformName
 8         desired_caps["platformVersion"] = platformVersion
 9         desired_caps["deviceName"] = deviceName
10         desired_caps["uiid"] = uiid
11         desired_caps["appPackage"] = appPackage
12         desired_caps["appActivity"] = appActivity
13 
14         desired_caps["unicodeKeyboard"] = unicodeKeyboard
15         desired_caps["resetKeyboard"] = resetKeyboard
16         desired_caps["noReset"] = noReset
17 
18         driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
19         return driver
20 
21     def new_clear(self,driver,path):
22         WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.X.XX.debug:id/account')))
23         driver.find_element_by_id(path).clear()
24 
25     def  new_input(self,driver,path,data):
26         driver.find_element_by_id(path).send_keys(data)
27 
28     def new_click(self,driver,path,message):
29         driver.find_element_by_id(path).click()
30         # toast_loc = '//*[contains(@text,"請輸入正確規則的密碼")]'
31         toast_loc =".//*[contains(@text,'%s')]" %message
32         # print((MobileBy.XPATH,toast_loc))
33         try:
34             WebDriverWait(driver,5,0.01).until(EC.presence_of_all_elements_located((MobileBy.XPATH,toast_loc)))
35             print(driver.find_element_by_xpath(toast_loc).text)
36         except:
37             print('沒有獲取到toast資訊')

 

robotframework使用該封裝的關鍵字進行獲取

1 登入_密碼錯誤(toast)
2     Comment    Login Toast    請輸入正確規則的密碼
3     ${driver}    Toast Open Application    UiAutomator2    Android    6.0    Lenovo TB3-X70N    FACU8TP7UCAAMNUO
4     ...    com.X.cXd.debug    com.XX.XXactivity.SplashActivity    True    True    True
5     New Clear    ${driver}    com.XX.XX.debug:id/account
6     New Input    ${driver}    com.XX.cXXd.debug:id/account    123456
7     New Clear    ${driver}    com.XX.XX.debug:id/pwd
8     New Input    ${driver}    com.XX.XX.debug:id/pwd    123456ab
9     New Click    ${driver}    com.XX.XX.debug:id/login    請輸入正確規則的密碼

 







結果:

 

&n