文章目录
一、Toast弹窗是什么
一种消息框类型
无法被点击
Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失
是系统级别的控件,属于系统settings
Toast类的思想:
就是尽可能不引人注意,同时还向用户显示信息,希望他们看到
二、Toast 定位
appium使用uiautomator底层的机制来分析抓取toast,并且把toast放到控件树里面,但本身并不属于控件。
automationName: uiautomator2
getPageSource是无法找到的
必须使用xpath查找
方式一:
self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text,'popup menu')]")
方式二
self.driver.find_element(AppiumBy.XPATH,"//android.widget.Toast[@class='android.widget.Toast']")
三、案例
toast弹窗如下
使用uiautomatorviewer进行定位
自动打开UI Automator Viewer界面
caps["automationName"]='uiautomator2' #todo 安卓工作引擎
import time
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
class TestXueqiu:
def setup(self):
caps = {
}
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:7555 device"
caps["appPackage"] = "io.appium.android.apis"
caps["appActivity"] = "io.appium.android.apis.view.PopupMenu1"
caps["noReset"] = "true"
caps["ensureWebviewsHavePages"] = True
caps["automationName"]='uiautomator2' #todo 安卓工作引擎
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
self.driver.implicitly_wait(8)
def teardown(self):
self.driver.quit()
def test_toast(self):
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Make a Popup!").click()
self.driver.find_element(AppiumBy.ID,"android:id/title").click()
print(self.driver.page_source)
#todo xpath
#text=self.driver.find_element(AppiumBy.XPATH,"//android.widget.Toast[@class='android.widget.Toast']").text
#print(text)
#todo text
text=self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text,'popup menu')]").text
print(text)
1、特别注意
如果不想让脚本从头开始执行脚本,忽略之前的点击操作,直接进入目标页面,
可以获取当前页面的appActivity
使用命令行:adb shell dumpsys window | grep mCurrent获取