python+Appium自动化:toast定位

Toast简介

Toast是一种简易的消息提示框。

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

用户将可能是在中间键入别的东西。Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。

而且Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失。

举个例子:下方图片就是淘宝退出app时出现的toast信息

如果用 UI Automation Viewer这个工具是无法定位到的,那么如何进行定位呢?

这个主要是基于UiAutomator2,因此需要在Capablity配置如下参数:

'automationName': 'uiautomator2'

安装appium-uiautomator2-driver:

cnpm install appium-uiautomator2-driver

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

[email protected]@appium-uiautomator2-driver

[email protected]@appium-uiautomator2-server

具体代码如下:

from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
from selenium.webdriver.support.ui import WebDriverWait


desired_caps = {
"platformName": "Android",
"platformVersion": "5.1",
"deviceName": "U4KF9HSK99999999",
"appPackage": "com.taobao.taobao",
"appActivity": "com.taobao.tao.welcome.Welcome",
"unicodeKeyboard":True,
"resetKeyboard":True,
"noReset": True,
"automationName": "Uiautomator2"
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(1)
driver.back()
# 用xpath定位
toast_message = "再按一次返回键退出手机淘宝"
message ='//*[@text=\'{}\']'.format(toast_message)
#显示等待检测元素
toast_element=WebDriverWait(driver, 5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
#结果进行比较
assert toast_element.text == "再按一次返回键退出手机淘宝"
 

猜你喜欢

转载自www.cnblogs.com/bugbreak/p/12048032.html