
获取toast消息
Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显示时间自动消失。
由于在Windows与Mac环境下操作基本一致,此次示例为在Windows环境。
1、除了使用Appium-Python-Client,还需要用到selenium
脚本引用到的包:
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 By2、在脚本参数中,将automationName值填写uiautomator2。
# 启用 UIAutomator2
desired_caps['automationName'] = 'uiautomator2'3、如图所示,获取帮帮应用的toast信息为“网络异常”。

message = '//*[contains(@text,\'{}\')]'.format("网络异常")
element = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH, message)))
print("toast: " + element.text)4、脚本代码:
#!/usr/bin/env python
# -*- 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 os
import time
"""
获取toast消息
"""
desired_caps = {}
# 启用 UIAutomator2
desired_caps['automationName'] = 'uiautomator2'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'Galaxy C7'
desired_caps['udid'] = '316d9073'
desired_caps['app'] = os.path.abspath('C:\\PycharmProjects\\My_Appium_Demo\\app\\Bangbang.apk')
desired_caps['appPackage'] = 'com.xiaoV.BWalletBeta'
desired_caps['appActivity'] = 'com.xiaoV.BWallet.yklogin.YkSplashActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['noReset'] = True
desired_caps['fullReset'] = False
desired_caps['newCommandTimeout'] = 60
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
print("启动应用程序")
message = '//*[contains(@text,\'{}\')]'.format("网络异常")
element = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH, message)))
print("toast: " + element.text)
time.sleep(2)
driver.quit()1、开启Appium服务(任选一种即可)
方式一:开启Appium Server
打开命令行,输入 appium --address 127.0.0.1 --port 4723 --no-reset --session-override
如图所示,Appium服务开启。

方式二:开启Appium Desktop
打开Appium Desktop

如服务IP和端口默认的情况下,直接点击Start Server v1.8.0来开启Appium服务,如图所示。

2、选中脚本鼠标右键Run,执行测试脚本。
(1)启动应用程序
(2)获取toast信息为“网络异常”,并打印到控制台
(3)关闭应用程序
脚本执行结束后,控制台打印的信息:获取到toast信息。

本文分享自 AllTests软件测试 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!