Appium常用操作之toast彈出框處理


toast是android中用來顯示信息的一種機制,和dialog對話框不一樣的是toast是沒有焦點的,而且toast顯示的時間有限,過一定的時間就會自動消失,並且也不能被點擊。

在appium中,如果想要定位到toast信息,通過appium自帶的inspactor或者sdk里面的uiautomator工具發現是定位不到的,沒有對應的屬性信息,不過好消息是在appiumV1.6.3及之后的版本中支持toast的獲取。

1.toast獲取

定位toast信息要求

1.uiautomator2只支持安卓俺呢白牛5.0+(模擬器可以使用夜神模擬器)

2.appium server版本1.6.3+以上

3.在desiredcapabilities中指定對應的屬性automationName為UIAutomator2

desired_caps = {
        'platformName': 'Android',
        'platformVersion': '7.0',
        'deviceName': 'V889F',
        'appPackage': 'com.alibaba.mts.mtsdemoapp',
        'appWaitPackage': 'com.alibaba.mts.mtsdemoapp',
        'app': 'D:/home/mdp/result/GroovyTest/case1/task.apk',
        'newCommandTimeout': 30,
        'automationName': 'Appium'
    }
desired_caps['automationName']="UIAutomator2"

 要求滿足之后,接下來我們就可以用代碼來獲取對應的信息了。

注意:使用presence_of_element_located,而不能使用visibility_of_element_located,在這里它對toast的可見處理並不支持,會直接拋出命令錯誤無法執行

#獲取對應的toast信息
part_str="登錄成功"
xpath_lotor='//*[contains(text(),"{0}")]'.formant(part_str)
try:
    WebDriverWait(driver,10,1).until(EC.precence
_of_element_located(MobileBy.XPATH,xpath_locator))
    print("找到了toast")
except Error as e:
    print("toast錯過了,報錯{0}".format(e))

  

def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):        
    try:         
        toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)                 
        WebDriverWait(driver, timeout,poll_frequency).until(EC.presence_of_element_located(toast_loc))             
        return True    
    except:         
        return False
is_toast_exist(driver,\"登錄成功\")

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM