Be careful
toast is only supported by appium 1.6.3 or above, Android 5.0 or above (you need to use the yeshenduokai simulator), jdk1.8 and the environment variables are configured.
toast location
1. First, look at what the toast looks like, as shown in the figure below. The pop-up message like "press again to exit" is toast.
2. To locate the toast element, it is necessary to note that the parameter of automationName must be Uiautomator2 to locate it.
'automationName': 'Uiautomator2'
# coding:utf-8 from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep desired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity', 'noReset': 'true', 'automationName': 'Uiautomator2' } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # Wait for the main page activity to appear driver.wait_activity(".base.ui.MainActivity", 10) driver.back() # Point return # Locate toast element toast_loc = ("xpath", ".//*[contains(@text, 'press exit again')] ") t = WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(toast_loc)) #Note that the waiting must be the existence of the element, and the element will report an error if it is visible print t
3. For the printed result, the following message appears, indicating that the toast is located
<appium.webdriver.webelement.WebElement (session="02813cce-9aaf-4754-a532-07ef7aebeb88", element="339f72c4-d2e0-4d98-8db0-69be741a3d1b")>
Package toast judgment
1. Write a function to encapsulate and judge whether there is a toast message. If there is a toast message, it will return True. If there is a toast message, it will return False
def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5): '''is toast exist, return True or False :Agrs: - driver - pass driver - text - Text content seen on page - timeout - Maximum timeout, default 30 s - poll_frequency - Interval query time, default 0.5s Query once :Usage: is_toast_exist(driver, "What you see") ''' 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
Reference code
# coding:utf-8 from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC desired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity', 'noReset': 'true', 'automationName': 'Uiautomator2' } def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5): '''is toast exist, return True or False :Agrs: - driver - pass driver - text - Text content seen on page - timeout - Maximum timeout, default 30 s - poll_frequency - Interval query time, default 0.5s Query once :Usage: is_toast_exist(driver, "What you see") ''' 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 if __name__ == "__main__": driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # Wait for the main page activity to appear driver.wait_activity(".base.ui.MainActivity", 10) driver.back() # Point return # Judge whether there is toast - 'press again to exit' print is_toast_exist(driver, "Press again to exit")