1. 特点
- 是简易消息提示框
- 无法获得焦点
- 显示时间较短,一般情况在3~5s左右
- 系统级别的控件,归属于系统settings。app发送消息时,向系统发送请求,由系统统一弹框,这类控件不在app内
2. 定位
- appium 使用 uiautomator 底层机制来分析抓取toast,把toast放到控件树里,它本身并非控件
- 必须使用xpath进行查找
- //*[@class='android.widget.toast']
- //*[contains(@text,'xxxx')]
- 定位前需要在desire_cap中添加 "automatorName":"uiautomator2"参数设置项(表示使用uiautomator2引擎)
- 以下代码是基于已经打开menu页面的
#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-
# @Author : Tina Yu
# @Time : 2021-12-11 21:07
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
class TestToastDemo:
def setup(self):
desire_cap = {
"platformName": "android",
"deviceName": "tinayu",
"appPackage": "io.appium.android.apis",
"appActivity": ".view.PopupMenu1",
"dontStopAppOnReset": True,
"automatorName": "uiautomator2" # 安卓系统默认就是使用该引擎,所以即时不设置也能运行成功
}
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desire_cap)
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
def test_toast_demo(self):
self.driver.find_element(MobileBy.ACCESSIBILITY_ID, "Make a Popup!").click()
self.driver.find_element(MobileBy.XPATH, "//*[@text='Search']").click()
toast_text = self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text
print(toast_text)