0
点赞
收藏
分享

微信扫一扫

Python+Appium 查找 toast 方法的封装

dsysama 2022-08-02 阅读 233

使用场景:在操作应用时常见toast弹框,通过toast弹框信息的获取判断当前的某个操作是否成功

引用的包:from selenium.webdriver.support import expected_conditions as EC,\expected_conditions

      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait

截图如下,查找如下截图中的toast内容,判断给出的值是否与实际toast的值是否相等,相等则正常执行,不相等直接走except异常内容,如下:

  

Python+Appium   查找 toast 方法的封装_封装

 

第一种封装方法:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC,\
expected_conditions

def find_Toast2(self,message): #查找toast值
'''
method explain:查找toast的值,与find_Toast实现方法一样,只是不同的2种写法
parameter explain:【text】查找的toast值
Usage:
device.find_Toast2('再按一次退出iBer')
'''
logging.info("查找toast值---'%s'" %(message))
try:
message = '//*[@text=\'{}\']'.format(message)
WebDriverWait(self.driver,5,0.5).until(expected_conditions.presence_of_element_located((By.XPATH,message)))
logging.info("查找到toast----%s"%message)
return except:"未查找到toast----%s"%message)
return

 

第二种封装方法如下:

def find_Toast(self,message,timeout=5,poll_frequency = 0.5):  #查找toast值
'''
method explain:查找toast的值
parameter explain:【text】给定的toast值
Usage:
device.find_Toast('再按一次退出iBer')
'''
logging.info("获取toast值---'%s'" %message)
try:
toast_loc = ("xpath",".//*[contains(@text,'%s')]" %message)
WebDriverWait(self.driver,timeout,poll_frequency).until(EC.presence_of_element_located(toast_loc))
logging.info("查找到toast--'%s'"%message)
return True
except:
logging.info("未查找到toast--'%s'"%message)
return

​1.作者:Syw

2.本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

3.如果文中有什么错误,欢迎指出。以免更多的人被误导。

举报

相关推荐

0 条评论