目录:导读
一,前言
在app自动化测试的过程中经常会遇到需要对toast进行定位,最常见的就是定位toast或者获取toast的文案进行断言,如下图,通过定位"登录成功"的toast就可以断言今日头条登录用例是否通过。但toast区别于控件元素,无法获取焦点,不能通过uiautomatorviewer.bat、appium、weditor等工具定位,因此我们就需要通过别的方法来定位。
二,环境
- windows 10
- Android 10
- appium 1.18.0 (desktop)
- selenium 3.141.0
- jdk 1.8
三,toast定位准备与定位方法
1,准备
注意:网上大量的博客都说定位toast需要使用uiautomator2,且需要安装appium-uiautomator2-driver。但我在以上环境定位toast时是不需要uiautomator2,也无需安装appium-uiautomator2-driver,且能定位成功!!!大家可以尝试,如果报错的话就老实按照下面步骤进行吧。
1.1,在Capablity里新增参数使用uiautomator2:
desired_caps['automationName'] = 'uiautomator2',
1.2,再安装appium-uiautomator2-driver,命令如下:
cnpm install appium-uiautomator2-driver
安装成功后在C:\Users\xxx\node_modules会出现如下文件:
_appium-uiautomator2-driver@1.12.0@appium-uiautomator2-driver
_appium-uiautomator2-server@1.10.0@appium-uiautomator2-server
2,定位方法
toast需使用xpath的方式进行定位
2.1,根据toast的文本内容定位toast
driver.find_element_by_xpath('//*[@text="xxxxxx"]')
这种方式一般用于判断或断言是否出现文本为"xxxxxx"的toast,因此我们可以封装如下:
<