uiautomator2 獲取APP Toast內容


前言:appium必須是1.6以上的版本

環境(安裝和安裝都可以運行成功,我也不確定要不要這個)

1、抓到toast需要安裝uiautomator2,安裝npm:npm install -g cnpm --registry=https://registry.npm.taobao.org   

2、 然后通過這個命令: cnpm install appium-uiautomator2-driver   安裝uiautomator2的配置文件

一、先看看toast

二、代碼實現:

#coding= utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
desired_caps = {
                'platformName':"Android",
                'deviceName':"R8V5T15930002010",
                'platformVersion':"5.0.1",
                'appPackage':"com.yuedan",
                'appActivity':"com.yuedan.ui.Activity_Splash",
                'unicodeKeyboard': True,#使用unicode編碼方式發布字符串
                'resetKeyboard': True , #屏蔽軟鍵盤,
                'automationName': 'uiautomator2',
                'noReset':True
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主頁面activity出現
time.sleep(20)
print(driver.current_activity)
driver.wait_activity(".ui.MainActivity", 20)
search_view = WebDriverWait(driver,10,0.1).until(EC.presence_of_element_located((By.ID, "com.yuedan:id/search_view")))
search_view.click()
search= WebDriverWait(driver,10,0.1).until(EC.presence_of_element_located((By.ID, "com.yuedan:id/iv_search")))
search.click()
toast_message = "請輸入搜索關鍵字"
message ='//*[@text=\'{}\']'.format(toast_message)
# 獲取toast提示框內容
toast_element = WebDriverWait(driver,5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
driver.quit()

運行結果:

三、代碼實現

#coding= utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
desired_caps = {
                'platformName':"Android",
                'deviceName':"R8V5T15930002010",
                'platformVersion':"5.0.1",
                'appPackage':"com.yuedan",
                'appActivity':"com.yuedan.ui.Activity_Splash",
                'unicodeKeyboard': True,#使用unicode編碼方式發布字符串
                'resetKeyboard': True , #屏蔽軟鍵盤,
                'automationName': 'uiautomator2'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(2)
driver.find_element_by_id("com.yuedan:id/login").click()
time.sleep(2)
driver.find_element_by_id("com.yuedan:id/btnQuickLogin").click()
driver.find_element_by_id("com.yuedan:id/et_name").send_keys("39999")
driver.find_element_by_id("com.yuedan:id/tv_re_sent").click()
# 定位toast元素
toast_message = "手機號碼格式錯誤"
message ='//*[@text=\'{}\']'.format(toast_message)
# 獲取toast提示框內容
toast_element = WebDriverWait(driver,5,0.1).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
driver.quit()

運行結果:


免責聲明!

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



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