appium+python自動化29-toast
Supported Platforms
1.查看appium v1.7版本官方文檔
Supported Platforms
Appium supports app automation across a variety of platforms, like iOS, Android, and Windows. Each platform is supported by one or more "drivers", which know how to automate that particular platform. Choose a driver below for specific information about how that driver works and how to set it up:
- iOS
- The XCUITest Driver
- (DEPRECATED) The UIAutomation Driver
- Android
- (BETA) The Espresso Driver
- The UiAutomator2 Driver
- (DEPRECATED) The UiAutomator Driver
- (DEPRECATED) The Selendroid Driver
- The Windows Driver (for Windows Desktop apps)
- The Mac Driver (for Mac Desktop apps)
2.從上面的信息可以看出目前1.7的android版可以支持:Espresso、UiAutomator2、UiAutomator、Selendroid四種驅動模式,後面兩個不推薦用了,太老了,Espresso這個是最新支持的處於beta階段,UiAutomator2是目前最穩的。
3.appium最新版本還能支持windows和mac的桌面app程序了,這個是否穩定,拭目以待!
toast定位
1.先看下toast長什麽樣,如下圖,像這種彈出來的消息"再按一次退出",這種就是toast了。
2.想定位toast元素,這裏一定要註意automationName的參數必須是Uiautomator2才能定位到。
‘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) # 等主頁面activity出現 driver.wait_activity(".base.ui.MainActivity", 10) driver.back() # 點返回 # 定位toast元素 toast_loc = ("xpath", ".//*[contains(@text,'再按一次退出')]") t = WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(toast_loc)) print t
3.打印出來的結果,出現如下信息,說明定位到toast了
<appium.webdriver.webelement.WebElement (session="02813cce-9aaf-4754-a532-07ef7aebeb88", element="339f72c4-d2e0-4d98-8db0-69be741a3d1b")>
封裝toast判斷
1.單獨寫一個函數來封裝判斷是否存在toast消息,存在返回True,不存在返回False
def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):
'''is toast exist, return True or False
:Agrs:
- driver - 傳driver
- text - 頁面上看到的文本內容
- timeout - 最大超時時間,默認30s
- poll_frequency - 間隔查詢時間,默認0.5s查詢一次
:Usage:
is_toast_exist(driver, "看到的內容")
'''
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
參考代碼
# 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 - 傳driver
- text - 頁面上看到的文本內容
- timeout - 最大超時時間,默認30s
- poll_frequency - 間隔查詢時間,默認0.5s查詢一次
:Usage:
is_toast_exist(driver, "看到的內容")
'''
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)
# 等主頁面activity出現
driver.wait_activity(".base.ui.MainActivity", 10)
driver.back() # 點返回
# 判斷是否存在toast-'再按一次退出'
print is_toast_exist(driver, "再按一次退出")
appium+python自動化29-toast