Appium自動化測試中獲取Toast信息操作
Toast簡介
Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。
Toast 定位
Appium 1.6.3開始支持識別Toast內(nèi)容,主要是基于UiAutomator2,因此需要在Capablity配置參數(shù)
啟動參數(shù)配置
desired_caps['automationName']='uiautomator2'
環(huán)境
- Appium-Python-Client: 2.1.2
- selenium: 4.1.0
- Appium:v1.20.2
測試應用
- 網(wǎng)易云課堂
測試設備
- 夜神模擬器 Android 7.1.2
測試場景
- 進入登錄界面輸入用戶名和錯誤的密碼,獲取Toast內(nèi)容
代碼實現(xiàn)
# _*_ coding:utf-8 _*_ from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy desired_caps = { ? ? "platformName": "Android", ? ? "platformVersion": "7.1.2", ? ? "udid": "127.0.0.1:62001", ? ? "appPackage": "com.netease.edu.study", ? ? "appActivity": "com.netease.edu.study.activity.ActivityWelcome", ? ? "noReset": True, ? ? 'automationName': 'uiautomator2' } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) driver.implicitly_wait(30) # 點擊我的菜單 driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tab_account").click() # 點擊登錄注冊按鈕 driver.find_element(AppiumBy.XPATH, "http://*[@text='登錄/注冊']").click() # 點擊手機號碼登錄 driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/login_phone_login").click() # 輸入手機號碼 driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_num").send_keys("132****475") # 輸入錯誤密碼 driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_pwd").send_keys("wy12345") # 點擊登錄按鈕 driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/button").click() # 獲取toast提示 toast_text = driver.find_element(AppiumBy.XPATH, "http://*[@class=\"android.widget.Toast\"]").text print(toast_text)
執(zhí)行結果:
說明
toast 獲取主要使用一個通用的class屬性獲取,通過xpath的方式://*[@class="android.widget.Toast"]
toast信息存在是否存在判斷封裝
代碼
def is_toast_exist(driver,text,timeout=20,poll_frequency=0.5): ? ? '''is toast exist, return True or False ? ? :Agrs: ? ? ?- driver - 傳driver ? ? ?- text ? - 頁面上看到的文本內(nèi)容 ? ? ?- timeout - 最大超時時間,默認20s ? ? ?- poll_frequency ?- 間隔查詢時間,默認0.5s查詢一次 ? ? :Usage: ? ? ?is_toast_exist(driver, "看到的內(nèi)容") ? ? ''' ? ? try: ? ? ? ? toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text) ? ? ? ? WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc)) ? ? ? ? return True ? ? except: ? ? ? ? return False
toast信息內(nèi)容獲取
代碼
def is_toast_exist(driver,timeout=20,poll_frequency=0.5): ? ? '''is toast exist, return toast_text or None ? ? :Agrs: ? ? ?- driver - 傳driver ? ? ?- timeout - 最大超時時間,默認20s ? ? ?- poll_frequency ?- 間隔查詢時間,默認0.5s查詢一次 ? ? :Usage: ? ? ?is_toast_exist(driver) ? ? ''' ? ? try: ? ? ? ? toast_loc = ("xpath", "http://*[@class=\"android.widget.Toast\"]") ? ? ? ? WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc)) ? ? ? ? toast_text = driver.find_element(AppiumBy.XPATH, "http://*[@class=\"android.widget.Toast\"]").text ? ? ? ? return toast_text ? ? except: ? ? ? ? return None
到此這篇關于Appium自動化測試中獲取Toast信息操作的文章就介紹到這了,更多相關Appium 獲取Toast內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用python實現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例
這篇文章主要介紹了使用python實現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例,需要的朋友可以參考下2014-05-05Python讀取Excel一列并計算所有對象出現(xiàn)次數(shù)的方法
這篇文章主要給大家介紹了關于Python讀取Excel一列并計算所有對象出現(xiàn)次數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09理解生產(chǎn)者消費者模型及在Python編程中的運用實例
生產(chǎn)者消費者模型一般用于體現(xiàn)程序的多線程并發(fā)性,Python的多線程雖然受到GIL控制,但依然可以構建隊列來簡單體現(xiàn)出模型的思路,這里我們就來共同理解生產(chǎn)者消費者模型及在Python編程中的運用實例:2016-06-06使用Python腳本對Linux服務器進行監(jiān)控的教程
這篇文章主要介紹了使用Python程序?qū)inux服務器進行監(jiān)控的教程,主要基于Python2.7的版本,需要的朋友可以參考下2015-04-04