用Appium進行android自動化測試

appium是開源的移動端自動化測試框架,能夠測試ios,android應用。appium讓移動端自動化測試沒必要限定在某種語言和某個具體的框架;也就是說任何人均可以使用本身最熟悉最順手的語言以及框架來作移動端自動化測試,支持java/python/Ruby等語言。html

appium的技術架構java

OS: Apple's UIAutomationnode

Android 4.2+: Google's UiAutomatorpython

Android 2.3+: Google's Instrumentationandroid

appium原理ios

Client/Server Architecturegit

appium的核心實際上是一個暴露了一系列REST API的server。web

這個server的功能其實很簡單:監聽一個端口,而後接收由client發送來的command。翻譯這些command,把這些command轉成移動設備能夠理解的形式發送給移動設備,而後移動設備執行完這些command後把執行結果返回給appium server,appium server再把執行結果返回給client。npm

在這裏client其實就是發起command的設備,通常來講就是咱們代碼執行的機器,執行appium測試代碼的機器。狹義點理解,能夠把client理解成是代碼,這些代碼能夠是java/ruby/python/js的,只要它實現了webdriver標準協議就能夠。json

這樣的設計思想帶來了一些好處:

  • 1,能夠帶來多語言的支持;
  • 2,能夠把server放在任意機器上,哪怕是雲服務器均可以;(是的,appium和webdriver天生適合雲測試)
  • Session

    session就是一個會話,在webdriver/appium,你的全部工做永遠都是在session start後才能夠進行的。通常來講,經過POST /session這個URL,而後傳入Desired Capabilities就能夠開啓session了。

    開啓session後,會返回一個全局惟一的session id,之後幾乎全部的請求都必須帶上這個session id,由於這個seesion id表明了你所打開的瀏覽器或者是移動設備的模擬器。

    進一步思考一下,因爲session id是全局惟一,那麼在同一臺機器上啓動多個session就變成了可能,這也就是selenium gird所依賴的具體理論根據。

    Appium Server

    這就是每次咱們在命令行用appium命令打開的東西。

    Appium Clients

    因爲原生的webdriver api是爲web端設計的,所以在移動端用起來會有點不三不四。appium官方提供了一套appium client,涵蓋多種語言ruby/java/python。在測試的時候,通常要使用這些client庫去替換原生的webdriver庫。這實際上不是替換,算是client對原生webdriver進行了一些移動端的擴展,加入了一些方便的方法,好比swipe之類,appium client讓咱們能夠更方便的寫出可讀性更好的測試用例。

    Desired Capabilities

    Desired Capabilities攜帶了一些配置信息。從本質上講,這個東東是key-value形式的對象。你能夠理解成是java裏的map,python裏的字典,ruby裏的hash以及js裏的json對象。實際上Desired Capabilities在傳輸時就是json對象。

    Desired Capabilities最重要的做用是告訴server本次測試的上下文。此次是要進行瀏覽器測試仍是移動端測試?若是是移動端測試的話是測試android仍是ios,若是測試android的話那麼咱們要測試哪一個app? server的這些疑問Desired Capabilities都必須給予解答,不然server不買帳,天然就沒法完成移動app或者是瀏覽器的啓動。

    python裏的Desired Capabilities配置:

    from appium import webdriver
    
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['deviceName'] = 'myAndroid' 
    desired_caps['appPackage'] = 'com.android.calculator2'
    desired_caps['appActivity'] = '.Calculator'
    
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    以上參考http://www.yangyanxing.com/article/1266.html

    appium安裝

    1.先安裝node.js。官網下載安裝:https://nodejs.org/

    2.安裝appium。

    1)能夠經過CMD窗口,執行npm install -g appium 命令來安裝Appium

    2)也能夠直接去appium官網下載安裝包來安裝。可是可能下不下來,這裏給出一個百度網盤的下載連接:http://pan.baidu.com/s/1jGvAISu

    安裝完成後,將appium可執行文件所在的目錄添加到環境變量Path。再經過命令行能夠啓動appium:

    $appium --session-override --no-reset

    --no-reset 便可避免執行用例的時候再次安裝app,--session-override 沒必要每次重啓session

    appium client安裝

    主要講一下python,推薦在線安裝:

    $pip install Appium-Python-Client

    appium API的使用

    以python爲例,直接上代碼

    #coding:gbk
    from appium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.touch_actions import TouchActions
     
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['deviceName'] = 'myphone'
    desired_caps['appPackage'] = 'com.android.calculator2'
    desired_caps['appActivity'] = '.Calculator'
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    
    element_id = "digit1"
    element_text = "2"
    #用find_element_by_id方式查找element,等待20s
    try:
        elm = WebDriverWait(driver, timeout=20, poll_frequency=0.5).until(lambda x: x.find_element_by_id(element_id))
    except Exception, e:
        print e
    #找到elm後點擊
    if elm:
        elm.click()
    
    #經過控件id獲取控件text
    Text = elm.get_attribute("text")
    if Text:
        print Text
    
    #用find_element_by_name方式查找element,等待20s
    try:
        elm1 = WebDriverWait(driver, timeout=20, poll_frequency=0.5).until(lambda x: x.find_element_by_name(element_text))
    except Exception, e:
        print e
    
    #得到當前activity
    act = driver.current_activity
    
    #經過TouchActions類繪製折線
    try:
        grid = WebDriverWait(driver, timeout=20, poll_frequency=0.5).until(lambda x: x.find_element_by_id('panelswitch'))
    except Exception, e:
        print e
    #控件grid的大小和位置
    width = grid.size['width']
    height = grid.size['height']
    x1 = grid.location['x']
    y1 = grid.location['y']
    #繪製折線
    action = TouchActions(driver)
    action.tap_and_hold(x1+width/8,y1+height/8).move(x1+width/8+width/3.4, y1+height/8).move(x1+width/8+width/3.4,y1+height/8+height*0.8).release(x1+width/8+width/3.4,y1+height/8+height*0.8)
    action.perform()
    
    #發送鍵盤事件,4表示返回鍵,更多見http://blog.csdn.net/wxlinwzl/article/details/41775333
    driver.keyevent(4)
    
    #獲取屏幕長寬
    width = driver.get_window_size()['width']
    height = driver.get_window_size()['height']
    #滑動
    driver.swipe(width/2, 10, width/2, height/2)
    相關文章
    相關標籤/搜索