Python+Appium自动化测试之toast定位
目录
一. 前言
二. 环境
三. toast定位准备与定位方法
1. 准备
2. 定位方法
四. 示例代码
一. 前言
在app自动化测试的过程中经常会遇到需要对toast进行定位,最常见的就是定位toast或者获取toast的文案进行断言,如下图,通过定位"登录成功"的toast就可以断言今日头条登录用例是否通过。但toast区别于控件元素,无法获取焦点,不能通过uiautomatorviewer.bat、appium、weditor等工具定位,因此我们就需要通过别的方法来定位。
今日头条app登录成功页面
二. 环境
- 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,且能定位成功!!!大家可以尝试,如果报错的话就老实按照下面步骤进行吧。
- 需要在Capablity里新增参数uiautomator2:
desired_caps['automationName'] = 'uiautomator2',
- 再安装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 -*-from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy as Bydef 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 Trueexcept: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 -*-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_textexcept Exception as e:return e
注意:
1,等待方式只能用presence_of_element_located(),即只能等待其存在,而不能等待其可见。
2,如果初始化构造driver时已经使用了隐式等待implicitly_wait(),则timeout参数可以不写。
四. 示例代码
定位今日头条app账号密码登录成功后的 "登录成功"toast
注意:我这里是将desired_caps里的Uiautomator2参数注释掉了,且未安装appium-uiautomator2-driver,也同样能定位到,大家可在与我相同的环境下进行尝试。
# -*- coding:utf-8 -*-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 Bydef 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,}# 启动appdriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)return driverdef 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 Trueexcept:return Falsedef 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_textexcept Exception as e:return edef 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
登录成功
TrueProcess finished with exit code 0
参考文章:
1.appium—定位Toast元素
2.Python+Appium自动化测试之toast定位
Python+Appium自动化测试之toast定位相关推荐
- python+appium 自动化2--元素定位uiautomatorviewer
出处:https://www.cnblogs.com/yoyoketang/p/6128741.html 前言: 可以打开手机上的app了,下一步元素定位uiautomatorviewer,通过定位到 ...
- Appium+Python安卓自动化测试之启动APP和配置获取
Appium+Python安卓自动化测试之启动APP和配置获取 本文章未讲述appium+python环境部署,环境部署会新开文章 一.手机连接电脑 1.USB连接电脑和手机,手机上点确认连接(最好用 ...
- Python+selenium自动化八大元素定位方法及实例(超详细)
目录 一.selenium模块中的find_element_by_id方法无法使用 二.Python+selenium自动化八大元素定位方法 使用场景: 1.通过id属性定位:driver.find_ ...
- Appium移动端自动化测试之元素定位(三)
1.name定位 driver.find_element_by_id('com.shanjian.originaldesign:id/edit_Tel').send_keys('15817252876 ...
- Appium+Python appium启动夜神模拟器定位元素(三)
① 目的 使用appium启动夜神模拟器定位元素 ② 环境 Python+Appium+Android模拟器 ③配置启动项 1.platformName:Android //设备型号 2.platfo ...
- Python+Appium自动化测试-通过坐标定位元素
在使用appium做app自动化测试的过程中,可能会遇到元素的属性值不是唯一的情况,导致不能通过find_element_bi_xx()方法定位元素,这个时候我们就可以通过坐标来定位元素. 1,通过绝 ...
- Windows下Python3+nose+appium自动化测试之Android篇
[本文出自天外归云的博客园] 简介 以下用来做自动化测试的这款app叫最爱抓娃娃,以后会改名为网易抓娃娃. 下文提到的appiumier项目里会包含用来测试的apk包以及自动化测试代码. 先说一个坑 ...
- Web UI自动化测试之元素定位
目前,在自动化测试的实际应用中,接口自动化测试被广泛使用,但UI自动化测试也并不会被替代.让我们看看二者的对比: 接口自动化测试是跳过前端界面直接对服务端的测试,执行效率和覆盖率更高,维护成本更低,整 ...
- UI自动化测试之元素定位方法
Python语言Selenium库UI自动化测试(一)元素定位方法 简介 当我们日常搭建自动化测试框架时,用Python调用浏览器时,通常有Requests库.Selenium库 这两个库是进行爬虫或 ...
最新文章
- LIVE 预告 | 华为诺亚韩凯:Transformer in Transformer
- 被誉为「教科书」,牛津大学231页博士论文全面阐述神经微分方程,Jeff Dean点赞...
- boost::histogram::axis::option用法的测试程序
- 1px问题在ios与android,IOS安卓常见问题
- 词法分析程序 LEX和VC6整合使用的一个简单例子
- 设计模式速查手册-创建型
- Eclipse开启全字母代码提示
- python有趣小程序-知道了这个,你也能写出 Python 趣味小程序
- CPU卡发卡总结(三)——充值和消费
- 服务器全息显示修改,全息显示
- 如何找到好书?有什么技巧或建议?
- 6G概念及愿景白皮书
- python基础个人总结
- Android 高级混淆和代码保护技术
- 华为机试:身高体重排序
- pyControl | 用于控制行为的神经科学实验的开源硬件和软件
- 扬州大学计算机软件工程博士,0836 软件工程博士点
- 【总结】alter table *** add constraint *** 用法 . 建立约束 ,主键、外键的SQL语句写法
- Macbook环境配置之个人配置
- matlab环境下的yalmip+cplex的安装过程
热门文章
- 这是给程序员专用的书吗?
- 每日一皮:原型还可以啊,怎么上线后就这样了。。。
- 8月最新阿里技术栈架构资料
- C语言比较法排大小,c语言 比较法排序区别
- oracle中的sql%rowcount
- ‘utf-8‘ codec can‘t encode character ‘\udcc0‘ in position 35
- python argparse中action 的可选参数store_true
- torch-toolbox
- PyTorch cat
- Qt lnk1158 无法运行rc.exe 解决