Android appium自动化测试中Toast
1. 首先执行这个命令:npm install -g
cnpm --registry=https://registry.npm.taobao.org 安装cnpm
npm安装不了时,可以先安装NPM镜像,地址:https://npm.taobao.org/;
npm install -g cnpm --
- cnpm install appium-uiautomator2-driver 安装uiautomator2的配置文件
需要导入的包:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
代码实例
def return_toast(driver, number = 3):
for i in range(number):
toast_loc = ("xpath","//*[@text = ‘清理成功‘]")
try:
et = WebDriverWait(driver,3,0.1).until(EC.presence_of_element_located(toast_loc))
return et
except:
print("定位不到toast")
pass
return_toast(self.driver)
try:
driver.find_element(return_toast())
print("清理本地报告的用例执行通过")
except:
img_folder =os.path.abspath(os.path.join(os.path.dirname(__file__),".."))+‘//myhealthdocscreenshots//‘
when = time.strftime("%Y%m%d%H%M", time.localtime(time.time()))
screen_save_path = img_folder + when+ ‘.png‘
driver.get_screenshot_as_file(screen_save_path)
driver.implicitly_wait(3)
最好用for循环
number = 3
for i in range(number):
existedtoast_loc = ("xpath","//*[@text=‘用户与家人已存在绑定关系‘]")
try:
ele = WebDriverWait(driver,3,0.1).until(EC.presence_of_element_located(existedtoast_loc))
print(ele.text)
print("用户与家人已存在绑定关系用例测试 通过")
except Exception as e:
print("没有定位到 用户与家人已存在绑定关系 toast")
img_folder =os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + ‘//familymanagescreenshots//‘
when = time.strftime("%Y%m%d%H%M", time.localtime(time.time()))
screen_save_path = img_folder +when + ‘.png‘
driver.get_screeshot_as_file(screen_save_path)
pass
用while循环执行次数会多,容易出错。