python+Appium自動化:toast定位


Toast簡介

Toast是一種簡易的消息提示框。

當視圖顯示給用戶,在應用程序中顯示為浮動。和Dialog不一樣的是,它永遠不會獲得焦點,無法被點擊。

用戶將可能是在中間鍵入別的東西。Toast類的思想就是盡可能不引人注意,同時還向用戶顯示信息,希望他們看到。

而且Toast顯示的時間有限,Toast會根據用戶設置的顯示時間后自動消失。

舉個例子:下方圖片就是淘寶退出app時出現的toast信息

 

如果用 UI Automation Viewer這個工具是無法定位到的,那么如何進行定位呢?

這個主要是基於UiAutomator2,因此需要在Capablity配置如下參數:

'automationName': 'uiautomator2'

安裝appium-uiautomator2-driver:

cnpm install appium-uiautomator2-driver

安裝成功后可以在 C:\Users\XX路徑\node_modules看到對應的文件:

_appium-uiautomator2-driver@1.23.0@appium-uiautomator2-driver

_appium-uiautomator2-server@2.6.0@appium-uiautomator2-server

 

 

具體代碼如下:

from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
from selenium.webdriver.support.ui import WebDriverWait


desired_caps = {
"platformName": "Android",
"platformVersion": "5.1",
"deviceName": "U4KF9HSK99999999",
"appPackage": "com.taobao.taobao",
"appActivity": "com.taobao.tao.welcome.Welcome",
"unicodeKeyboard":True,
"resetKeyboard":True,
"noReset": True,
"automationName": "Uiautomator2"
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(1)
driver.back()
# 用xpath定位
toast_message = "再按一次返回鍵退出手機淘寶"
message ='//*[@text=\'{}\']'.format(toast_message)
#顯示等待檢測元素
toast_element=WebDriverWait(driver, 5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
#結果進行比較
assert toast_element.text == "再按一次返回鍵退出手機淘寶"
 


免責聲明!

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



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