Appium自動(dòng)化測試中獲取Toast信息操作
Toast簡介
Toast是Android中用來顯示顯示信息的一種機(jī)制,和Dialog不一樣的是,Toast是沒有焦點(diǎn)的,而且Toast顯示的時(shí)間有限,過一定的時(shí)間就會(huì)自動(dòng)消失。
Toast 定位
Appium 1.6.3開始支持識(shí)別Toast內(nèi)容,主要是基于UiAutomator2,因此需要在Capablity配置參數(shù)
啟動(dòng)參數(shù)配置
desired_caps['automationName']='uiautomator2'
環(huán)境
- Appium-Python-Client: 2.1.2
- selenium: 4.1.0
- Appium:v1.20.2
測試應(yīng)用
- 網(wǎng)易云課堂
測試設(shè)備
- 夜神模擬器 Android 7.1.2
測試場景
- 進(jìn)入登錄界面輸入用戶名和錯(cuò)誤的密碼,獲取Toast內(nèi)容
代碼實(shí)現(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)
# 點(diǎn)擊我的菜單
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tab_account").click()
# 點(diǎn)擊登錄注冊(cè)按鈕
driver.find_element(AppiumBy.XPATH, "http://*[@text='登錄/注冊(cè)']").click()
# 點(diǎn)擊手機(jī)號(hào)碼登錄
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/login_phone_login").click()
# 輸入手機(jī)號(hào)碼
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_num").send_keys("132****475")
# 輸入錯(cuò)誤密碼
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_pwd").send_keys("wy12345")
# 點(diǎn)擊登錄按鈕
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í)行結(jié)果:

說明
toast 獲取主要使用一個(gè)通用的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 - 最大超時(shí)時(shí)間,默認(rèn)20s
? ? ?- poll_frequency ?- 間隔查詢時(shí)間,默認(rèn)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 Falsetoast信息內(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 - 最大超時(shí)時(shí)間,默認(rèn)20s
? ? ?- poll_frequency ?- 間隔查詢時(shí)間,默認(rèn)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
到此這篇關(guān)于Appium自動(dòng)化測試中獲取Toast信息操作的文章就介紹到這了,更多相關(guān)Appium 獲取Toast內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux下python使用sendmail發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了linux下python使用sendmail發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python實(shí)現(xiàn)的爬取百度貼吧圖片功能完整示例
這篇文章主要介紹了Python實(shí)現(xiàn)的爬取百度貼吧圖片功能,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)的百度貼吧圖片爬蟲相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Python使用re模塊正則提取字符串中括號(hào)內(nèi)的內(nèi)容示例
這篇文章主要介紹了Python使用re模塊正則提取字符串中括號(hào)內(nèi)的內(nèi)容,結(jié)合實(shí)例形式分析了Python使用re模塊進(jìn)行針對(duì)括號(hào)內(nèi)容的正則匹配操作,并簡單解釋了相關(guān)修正符與正則語句的用法,需要的朋友可以參考下2018-06-06
python3檢查字典傳入函數(shù)鍵是否齊全的實(shí)例
這篇文章主要介紹了python3檢查字典傳入函數(shù)鍵是否齊全的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
詳解Django之a(chǎn)uth模塊(用戶認(rèn)證)
這篇文章主要介紹了詳解Django之a(chǎn)uth模塊(用戶認(rèn)證),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
分享3個(gè)簡單的Python代碼高效運(yùn)行技巧
這篇文章主要介紹了分享3個(gè)簡單的Python代碼高效運(yùn)行技巧,下面主要分享三個(gè)有效的,方便理解的,執(zhí)行高效的實(shí)用技巧,需要的朋友可以參考一下2022-03-03
Python數(shù)據(jù)集庫Vaex秒開100GB加數(shù)據(jù)
這篇文章主要為大家介紹了Python數(shù)據(jù)集庫Vaex秒開100GB加數(shù)據(jù)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python3利用scapy局域網(wǎng)實(shí)現(xiàn)自動(dòng)多線程arp掃描功能
這篇文章主要介紹了Python3利用scapy局域網(wǎng)實(shí)現(xiàn)自動(dòng)多線程arp掃描功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

