前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Appium Android Toast元素识别

Appium Android Toast元素识别

作者头像
清风穆云
发布2021-08-09 10:52:16
1.3K0
发布2021-08-09 10:52:16
举报
文章被收录于专栏:QA一隅

问题思考

在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢?

Toast简介

Android中的Toast是一种简易的消息提示框。当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。

Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,一般3秒左右就消失了。因此使用传统的元素定位工具,我们是无法定位到Toast元素的(传说中低调奢华有内涵)。

Appium Toast内容获取

Add ability to verify TOAST messages (these can't be interacted with, only text retrieval allowed)

Appium 1.6.3开始支持识别Toast内容,主要是基于UiAutomator2,因此需要在Capablity配置如下参数:

代码语言:javascript
复制
desired_caps['automationName']='uiautomator2'

友情提示: automationName设置为 uiautomator2 后可能会导致整个appium运行比较慢,如果非必要获取Toast不建议这样设置,直接使用默认的配置 desired_caps['automationName']='Appium'即可

安装appium-uiautomator2-driver: 安装命令如下:

代码语言:javascript
复制
cnpm install appium-uiautomator2-driver

安装成功后可以在 C:\Users\XXXX\node_modules看到对应的文件:

  • _appium-uiautomator2-driver@1.12.0@appium-uiautomator2-driver
  • _appium-uiautomator2-server@1.10.0@appium-uiautomator2-server

安装selenium模块

代码语言:javascript
复制
pip install selenium

安装完成后使用如下命令检测是否安装成功

代码语言:javascript
复制
#查看selenium版本
C:\Users\Shuqing>pip show selenium
Name: selenium
Version: 3.11.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: c:\python35\lib\site-packages
Requires:
Required-by: Appium-Python-Client

selenium模块安装视频教程

测试环境

  • jdk版本:"1.8.0_05"
  • appium版本:1.7.2 (不能低于1.6.3)
  • selenium:3.11.0
  • 测试设备:Android 5.1.1
  • Python:3.5
  • 测试App:考研帮Android app V3.1.0

测试场景

进入登录界面输入错误的用户名或者密码,获取Toast内容:

  • “用户名或密码错误,你还可以尝试4次”
  • “验证失败次数过多,请15分钟后再试”

代码实现

get_toast.py

代码语言:javascript
复制
# coding=utf-8
from find_element.capability import driver
from selenium.webdriver.support.ui import WebDriverWait

driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear()
driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('zxss018')

driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('zxw2018')
driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click()


error_message="用户名或密码错误,你还可以尝试4次"
limit_message="验证失败次数过多,请15分钟后再试"

message='//*[@text=\'{}\']'.format(error_message)
# message='//*[@text=\'{}\']'.format(limit_message)

toast_element=WebDriverWait(driver,5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)


注意:Toast内容为中文时,顶部必须注释# coding=utf-8 否则会因为编解码导致文字识别失败。

报错相关

  1. Appium和Uiautomator版本不兼容
代码语言:javascript
复制
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: helpers.installApkRemotely is not a function

修改建议:安装最新版本的Appium

  1. Android SDK版本问题
代码语言:javascript
复制
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate.

修改建议:

  • 更新安装sdk
  • 管理员打开appium再执行
  • 如果还不行把这个把jdk、安卓sdk移动到其他盘里试试
  1. appium服务连接超时
代码语言:javascript
复制
Could not proxy command to remote server. Original error: Error: read ECONNRESET

造成该问题的原因一般是appium服务断开了,查看appium运行的详细log我们可以看到

代码语言:javascript
复制
[BaseDriver] Shutting down because we waited 60 seconds for a command
[debug] [UiAutomator2] Deleting UiAutomator2 session
[debug] [UiAutomator2] Deleting UiAutomator2 server session
[debug] [WD Proxy] Matched '/' to command name 'deleteSession'
[debug] [WD Proxy] Proxying [DELETE /] to [DELETE http://localhost:8208/wd/hub/session/72d5a39c-0535-475a-89d6-8fa3875cea1e] with no body
[Appium] Closing session, cause was 'New Command Timeout of 60 seconds expired. Try customizing the timeout using the 'newCommandTimeout' desired capability'
[Appium] Removing session ee196a53-d383-4b7e-a2a2-a3e998a1e879 from our master session list
[HTTP] --> POST /wd/hub/session/7bc053e6-e088-4ff9-a0a9-46246f931dc9/element
[HTTP] {"value":"com.mgtv.data.sdk:id/add_content","using":"id"}

默认是60秒,我们可以设置更大的等待时间间隙,如下所示即可

代码语言:javascript
复制
desired_caps['newCommandTimeout']=2000

参考资料

  • https://testerhome.com/topics/6685
  • https://github.com/appium/java-client/issues/828
  • https://testerhome.com/topics/15250?locale=zh-CN
  • https://www.jianshu.com/p/728cb9ffc7dc
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 QA一隅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题思考
  • Toast简介
  • Appium Toast内容获取
    • 测试环境
      • 测试场景
        • 代码实现
        • 报错相关
        • 参考资料
        相关产品与服务
        文字识别
        文字识别(Optical Character Recognition,OCR)基于腾讯优图实验室的深度学习技术,将图片上的文字内容,智能识别成为可编辑的文本。OCR 支持身份证、名片等卡证类和票据类的印刷体识别,也支持运单等手写体识别,支持提供定制化服务,可以有效地代替人工录入信息。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档