appium+python自動化29-toast

來源:互聯網
上載者:User

標籤:cross   cep   path   oss   文檔   查詢   auto   ppp   spec   

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-8from appium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom time import sleepdesired_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-8from appium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdesired_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 Falseif __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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.