Python+Appium 查找 toast 方法的封装

使用场景:在操作应用时常见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异常内容,如下:

  

 

第一种封装方法:

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 True except: logging.info("未查找到toast----%s"%message) return False

 

第二种封装方法如下:

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 False

 说明:

  获取toast的方法封装点击链接:http://www.cnblogs.com/syw20170419/p/8280115.html

posted @ 2018-01-13 17:50  Syw_文  阅读(806)  评论(0编辑  收藏  举报