RF+Appium Toast 问题:获取 Toast 信息
问题:获取 Toast 信息
Appium 1.6 版本之后,可以使用 Uiautomator2 获取 Toast 信息
准备:下载安装 Appium-desktop
一、登陆测试
1. 思路
- 打开 APP
- 进入登陆页
- 输入错误的用户名或密码
- 获取 Toast 信息
2. 关键字:APP_Open_Application
增加参数:automationName=Uiautomator2
Open Application %{G_APPIUM_HOST_IP0} platformName=%{G_APPIUM_PLATFORM_NAME} platformVersion=%{G_APPIUM_PLATFORM_VERSION} deviceName=%{G_APPIUM_DEVICE_NAME} app=${app} appPackage=${appPackage} appActivity=${appActivity} automationName=Uiautomator2 noReset=${noReset} unicodeKeyboard=true restKeyboard=true
其他参数参考:RF+Appium环境配置一:项目预设
二、登陆时,获取 Toast 信息
输入错误的用户名或密码时,获取 Toast 信息;否则,登陆成功 PASS
Run Keyword If '${test}'=='TRUE' and '%{U_APP_LOGIN_USER_ERROR}'!='PASS' Run Keywords Find Toast %{U_APP_LOGIN_USER_ERROR} AND Capture Page Screenshot AND Return From Keyword
为了不影响正常登陆的情况,增加参数 ${test},默认值为:FALSE。即,’${test}’==‘TRUE’ and ‘%{U_APP_LOGIN_USER_ERROR}’!=‘PASS’ 条件下,才去获取 Toast 信息。
三、pyhon 源代码:find_toast
修改 AppiumLibrary 库 _element.py
目录:C:\Python27\Lib\site-packages\AppiumLibrary\keywords
# coding=utf-8
# Author: Allan
# 导入三个库文件
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
# 获取 Toast 信息
def find_toast(self, text):
'''Find Android Toast
Arguments:
| text | Toast信息 |
Examples:
| Find Toast | ${text} |
'''
application = self._current_application()
try:
toast_locator = ("xpath", ".//*[contains(@text,'%s')]" %text)
WebDriverWait(application, 6, 0.5).until(expected_conditions.presence_of_element_located(toast_locator))
self._info("Toast has been found: %s." %text)
except:
raise AssertionError("Not found toast: %s." %text)
四、异常问题
增加参数:automationName=Uiautomator2 之后,一直报异常。信息如下:
[JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
查看手机已经应用:
io.appium.uiautomator2.server
io.appium.uiautomator2.server.test
方法1:手动杀掉 adb 进程
方法2:重启手机