一,前言

在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,因此我们可以封装如下:

# -*- coding:utf-8 -*-
# @author: 给你一页白纸 from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy as By def is_toast_exist(driver, text, timeout=20, poll_frequency=0.1):
'''
判断toast是否存在,是则返回True,否则返回False
:param driver: driver实例对象
:param text: toast文本
:param timeout: 定位超时时间
:param poll_frequency: 查询频率
:return: True or False
'''
try:
toast_loc = (By.XPATH, ".//*[contains(@text, %s)]" % text)
WebDriverWait(driver, timeout, poll_frequency).until(
ec.presence_of_element_located(toast_loc)
)
return True
except:
return False

2.2,根据toast的属性className定位toast

toast的className值为:android.widget.Toast

driver.find_element_by_xpath('//*[@class="android.widget.Toast"]')

这种方式一般用于获取toast的文本内容,封装如下:

# -*- coding:utf-8 -*-
# @author: 给你一页白纸 def get_toast_text(driver, timeout=20, poll_frequency=0.1):
'''
定位toast元素,获取text属性
:param driver: driver实例对象
:param timeout: 元素定位超时时间
:param poll_frequency: 查询频率
:return: toast文本内容
'''
toast_loc = (By.XPATH, '//*[@class="android.widget.Toast"]')
try:
toast = WebDriverWait(driver, timeout, poll_frequency).until(
ec.presence_of_element_located(toast_loc)
)
toast_text = toast.get_attribute('text')
return toast_text
except Exception as e:
return e

注意

  • 等待方式只能用presence_of_element_located(),即只能等待其存在,而不能等待其可见。

  • 如果初始化构造driver时已经使用了隐式等待implicitly_wait(),则timeout参数可以不写。

四,示例代码

定位今日头条app账号密码登录成功后的 "登录成功"toast

注意:我这里是将desired_caps里的Uiautomator2参数注释掉了,且未安装appium-uiautomator2-driver,也同样能定位到,大家可在与我相同的环境下进行尝试。

# -*- coding:utf-8 -*-
# @author: 给你一页白纸 from appium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy as By def android_driver():
desired_caps = {
"platformName": "Android",
"platformVersion": "10",
"deviceName": "PCT_AL10",
"appPackage": "com.ss.android.article.news",
"appActivity": ".activity.MainActivity",
# "automationName": "UiAutomator2",
"unicodeKeyboard": True,
"resetKeyboard": True,
"noReset": True,
}
# 启动app
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
return driver def is_toast_exist(driver, text, timeout=20, poll_frequency=0.1):
'''
判断toast是否存在,是则返回True,否则返回False
'''
try:
toast_loc = (By.XPATH, ".//*[contains(@text, %s)]" % text)
WebDriverWait(driver, timeout, poll_frequency).until(
ec.presence_of_element_located(toast_loc)
)
return True
except:
return False def get_toast_text(driver, timeout=20, poll_frequency=0.1):
'''
定位toast元素,获取text属性
'''
toast_loc = (By.XPATH, '//*[@class="android.widget.Toast"]')
try:
toast = WebDriverWait(driver, timeout, poll_frequency).until(
ec.presence_of_element_located(toast_loc)
)
toast_text = toast.get_attribute('text')
return toast_text
except Exception as e:
return e def login_opera(driver):
'''登录今日头条操作'''
try:
# driver.find_element_by_id("com.ss.android.article.news:id/cji").click() # 点击【同意】
driver.find_element_by_id("com.ss.android.article.news:id/cji").click() # 点击【我知道了】
driver.find_element_by_id("android:id/button1").click() # 点击权限管理-确定按钮
driver.find_element_by_xpath("//android.widget.TabWidget/android.widget.RelativeLayout[@index=3]").click() # 点击未登录
driver.find_element_by_id("com.ss.android.article.news:id/a10").click() # 未登录页点击登录按钮
driver.find_element_by_id("com.ss.android.article.news:id/bgh").click() # 登录页点击“。。。”
driver.find_element_by_xpath("//android.widget.LinearLayout[@index=4]").click() # 选择密码登录
driver.find_element_by_id("com.ss.android.article.news:id/bu").send_keys("xxxxxxxx") # 输入账号
driver.find_element_by_id("com.ss.android.article.news:id/c5").send_keys("xxxxxxxx") # 输入密码
driver.find_element_by_id("com.ss.android.article.news:id/a2o").click() # 点击登录
except Exception as e:
print("登录错误,原因为:{}".format(e))
# 报错时截图
driver.get_screenshot_as_file(r'E:\blog\blog_script\images\test_login_error_01.png')
else:
toast_text = get_toast_text(driver)
print(toast_text)
toast_el = is_toast_exist(driver, "登录成功")
print(toast_el) if __name__ == '__main__':
driver = android_driver()
login_opera(driver)

运行结果如下,说明定位该toast成功:

C:\Users\xiaoqq\AppData\Local\Programs\Python\Python37\python.exe E:/blog/blog_script/login_jrtt.py
登录成功
True Process finished with exit code 0

Python+Appium自动化测试(13)-toast定位的更多相关文章

  1. python+Appium自动化:toast定位

    Toast简介 Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动.和Dialog不一样的是,它永远不会获得焦点,无法被点击. 用户将可能是在中间键入别的东西.Toast类的 ...

  2. Python+Appium自动化测试(6)-元素等待方法与重新封装元素定位方法

    在appium自动化测试脚本运行的过程中,因为网络不稳定.测试机或模拟器卡顿等原因,有时候会出现页面元素加载超时元素定位失败的情况,但实际这又不是bug,只是元素加载较慢,这个时候我们就会使用元素等待 ...

  3. Python+Appium自动化测试(5)-appium元素定位常用方法

    对于Android而言,查找appUI界面元素属性的工具有三种:appium desktop,uiautomatorviewer.bat,weditor.之前已经介绍过了weditor的使用,这里我将 ...

  4. python+appium自动化测试(一)-----环境搭建

    基础背景: windows7系统 +python3.4版本 环境搭建目标: 使用python编写app自动化测试脚本并成功执行. 搭建步骤:   1.安装python3,安装步骤详见:https:// ...

  5. Python+Appium自动化测试(15)-使用Android模拟器(详细)

    做APP的UI自动化测试时,我们往往会使用真机跑自动化测试脚本,因为这样才是最真实的使用场景.但前期调试脚本的话,可以先使用模拟器,这样相对更加方便. 不推荐使用Android SDK里自带模拟器,太 ...

  6. Python+Appium自动化测试(14)-yaml配置Desired capabilities

    一,前言 在之前的appium自动化测试示例中,我们都是把构造driver实例对象的数据(即Desired Capabilities)写在业务代码里,如下: # -*- coding:utf-8 -* ...

  7. Python+Appium自动化测试(1)-appium环境搭建

    用python+appium做appUI自动化测试,系统为Windows.首先是JDK与Android SDK的安装与环境变量的设置. 一,安装JDK,配置JDK环境变量 1,下载jdk jdk8官网 ...

  8. Python+Appium自动化测试(9)-自动选择USB用于传输文件(不依赖appium对手机页面元素进行定位)

    一,问题 app自动化测试使用Android真机连接电脑时,通常会遇到两种情况: 1.测试机连接电脑会弹窗提示USB选项,选择USB用于"传输文件",有些手机不支持设置默认USB选 ...

  9. Python+Appium自动化测试(12)-通过坐标定位元素

    在使用appium做app自动化测试的过程中,可能会遇到元素的属性值不是唯一的情况,导致不能通过find_element_bi_xx()方法定位元素,这个时候我们就可以通过坐标来定位元素. 1,通过绝 ...

随机推荐

  1. 读取文本文件中的中文打印到Eclipse控制台为何显示问号

    原因:未将文本文件存为utf-8编码格式而是ascii编码格式.

  2. AWD攻防技战法

    round1 弱口令 cat /etc/passwd  查看用户信息 修改用户密码(passwd  username) 通过ssh弱口令批量getshell  (通过msf的auxiliary/sca ...

  3. 有关Sql中时间范围的问题

    背景 有时候需要利用sql中处理关于时间的判别问题,简单的如比较时间的早晚,判断一个时间是否在一段时间内的问题等.如果简单将时间判断与数值比较等同,那就会出现一些问题. 处理方式 处理Sql时间范围的 ...

  4. Ajax获取接口数据,url拼接参数跳转页面,js获取上一级页面参数给本页面

    1.Ajax获取接口数据 function demo(){ //假设请求参数 var requestBody = [{ "name":"zhang", &quo ...

  5. 学习go的一些笔记

    反射 reflect type CfgSt struct{ Match *int64 `ini:"match"` Desc string `ini:"desc" ...

  6. 小BUG大原理:FastJSON实体转换首字母小写的尴尬事件

    问题描述 因为项目连接的Oracle数据库,字段名映射方便使用大写,但是通过接口调用返回到前端的字段名首字母为小写,这样带来的问题前端显示的字段就需要写这种很尴尬的格式. 原因分析 开发环境使用的是S ...

  7. grpc服务如何添加sentry监控(添加中间件)

    目录 需求 解决 需求 sentry是一款非常好用的工具,可以方便追踪线上的异常,在gin框架里边可以非常方便的使用Use添加中件间,grpc服务在网上搜索了一堆没一个能用的,只能硬着头皮看源码 终于 ...

  8. CentOS 7使用PuppeteerSharp无头浏览器注意事项

    环境: CentOS 7.6.1810 .net core 3.1 PuppeteerSharp 2.0.0 1.如网络部稳定可以提前下载需要的chromium 下载地址:https://storag ...

  9. Docker跨主机通信(九)

    容器网络 在前面的博客中已经详细讲解了几种网络方案: none, host, bridge,user-defined.但是他们只是解决了单个主机间的容器的通信问题,并不能实现多个主机容器之间的通信.本 ...

  10. jenkins在windows系统及linux环境安装

    一.下载 jenkins是当前持续集成(Continuous integration,简称 CI)的主流工具,在项目中得到了广泛的推广和应用. 下载地址: https://jenkins.io/dow ...