1.appium识别toast对appium版本有要求,必须使用1.6.3以上才能识别toast
2.识别toast需要使用Uiautomator2库,所以需要安装Uiautomator2
3.需要在Capablity配置如下参数:desired_caps['automationName']='uiautomator2'
4.
Toast内容为中文时,顶部必须注释# coding=utf-8 否则会因为编解码导致文字识别失败。
代码如下
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.topsec.topsap'
desired_caps['appActivity'] = 'com.topsec.topsap.main.Welcome'
# 'automationName':'Selendroid'
desired_caps['automationName'] = 'Uiautomator2'
desired_caps['noReset']="True"
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def is_toast_exist(self, message):
try:
element = WebDriverWait(self.driver, 10, 0.01).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@text=\'{}\']'.format(message))))
for i in element:
print i.text
return True
except Exception, e:
log.error(e.message)
return False