網站首頁 編程語言 正文
Toast簡介
Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。
Toast 定位
Appium 1.6.3開始支持識別Toast內容,主要是基于UiAutomator2,因此需要在Capablity配置參數
啟動參數配置
desired_caps['automationName']='uiautomator2'
環境
- Appium-Python-Client: 2.1.2
- selenium: 4.1.0
- Appium:v1.20.2
測試應用
- 網易云課堂
測試設備
- 夜神模擬器 Android 7.1.2
測試場景
- 進入登錄界面輸入用戶名和錯誤的密碼,獲取Toast內容
代碼實現
# _*_ 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)
執行結果:
說明
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 ? - 頁面上看到的文本內容 ? ? ?- timeout - 最大超時時間,默認20s ? ? ?- poll_frequency ?- 間隔查詢時間,默認0.5s查詢一次 ? ? :Usage: ? ? ?is_toast_exist(driver, "看到的內容") ? ? ''' ? ? 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信息內容獲取
代碼
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
原文鏈接:https://ceshizhilu.blog.csdn.net/article/details/122693435
相關推薦
- 2022-11-29 C#使用泛型隊列Queue實現生產消費模式_C#教程
- 2022-07-15 VBScript編寫Windows防止鎖屏腳本程序_vbs
- 2022-07-01 Go?數據結構之二叉樹詳情_Golang
- 2023-03-26 WPF實現頁面的切換的示例代碼_C#教程
- 2022-03-30 C語言代碼實現猜數字游戲_C 語言
- 2024-03-09 【Redis】什么是緩存穿透,如何預防緩存穿透?
- 2022-06-01 Python中plt.plot()、plt.scatter()和plt.legend函數的用法示例_
- 2022-06-12 C#自定義特性(Attribute)詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支