Macaca+python配置簡單入門

前提:已經安裝好了macaca和相應的驅動
在這裏插入圖片描述
轉載:https://blog.csdn.net/hekaiyou/article/details/79067788

執行Macaca自動化腳本時,首先需要啓動一個Macaca服務器:

$ macaca server --verbose

    參數設置

    啓動Macaca服務器時如果沒有指定端口號,服務器URL的配置參數如下:

    server_url = {
        'hostname': 'localhost',
        'port': 3456
    }

    Android設置

    變量desired_caps是用來在啓動時配置服務器的參數,使用模擬器,並安裝、啓動App時,常用的配置參數如下:

    apps_path = '/Users/xxx/test-macaca/apps/'
    
    desired_caps = {
        'platformName': 'android',
        'deviceName': 'Nexus4',
        'app': apps_path + 'app-debug.apk',
    }
    參數 類型 描述
    platformName String 當前用例運行的平臺
    deviceName String 模擬器的名稱
    app String .apk文件的絕對地址或者遠程地址

    使用真機測試,並直接啓動App測試時,常用的配置參數如下:

    desired_caps = {
        'platformName': 'android',
        'package': 'com.xxxx.Maps',
    }
    參數 類型 描述
    platformName String 當前用例運行的平臺
    package String Android app的package name

    iOS設置

    使用模擬器,並安裝、啓動App時,常用的配置參數如下:

    apps_path = '/Users/xxx/test-macaca/apps/'
    
    desired_caps = {
        'platformName': 'iOS',
        'deviceName': 'iPhone 6s',
        'app': apps_path + 'app-debug.app',
    }
    參數 類型 描述
    platformName String 當前用例運行的平臺
    deviceName String 模擬器的名稱
    app String .ipa、.app文件的絕對地址或者遠程地址

    需要注意一下,在真機下可能無法安裝App,最好提前將測試App安裝到真機,並直接啓動App測試,常用的配置參數如下:

    desired_caps = {
        'platformName': 'iOS',
        'udid': '4502df9f327096e14502df9f327096e1',
        'bundleId': 'com.apple.Maps',
    }
    參數 類型 描述
    platformName String 當前用例運行的平臺
    udid String 測試設備的唯一設備ID
    bundleId String 應用的Bundle ID

    Web設置

    直接啓動桌面瀏覽器,並打開Web網頁測試時,常用的配置參數如下:

    desired_caps = {
        'platformName': 'desktop',
        'browserName': 'electron',
    }
    參數 類型 描述
    platformName String 當前用例運行的平臺
    browserName String 當前測試的瀏覽器名稱

    編寫用例

    啓動Macaca測試用例需要導入WebDriver,它是一個爲Macaca而設計的Python WebDriver客戶端。還需要導入unittest這個單元測試框架,它能組織執行測試用例。最好再導入retry,正如它的名字,它可以用來實現重試功能,比如出現網絡問題導致失敗,這時就需要重試了:

    import unittest
    from macaca import WebDriver
    from retrying import retry

    使用unittest來組織執行測試用例,同時使用了它的幾個特殊方法:

    • setUpClass()在所有test運行前運行一次(必須使用@classmethod裝飾器)
    • tearDownClass()在所有test運行完後運行一次(必須使用@classmethod裝飾器)
    • initDriver()在運行測試用例前初始化(必須使用@classmethod裝飾器)

    首先在setUpClass()中,配置所需的功能和服務器URL的WebDriver,並初始化WebDriver服務器。然後在tearDownClass()中,刪除當前WebDriver服務器會話。最後在initDriver()中,創建一個新的WebDriver服務器會話:

    class MacacaTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = WebDriver(desired_caps, server_url)
            cls.initDriver()
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        @classmethod
        @retry
        def initDriver(cls):
            print('嘗試連接服務器...')
            cls.driver.init()
    
        def test_01(self):
            self......
    
        def test_02(self):
            self......
    
    if __name__ == '__main__':
        unittest.main()

    元素查看器

    app-inspecto是瀏覽器中的移動UI查看器,可以對當前界面進行抓取、分解界面可操作的基本元素,使用npm安裝app-inspecto:

    $ npm i app-inspector -g

    直接-u加設備的udid即可啓動元素查看器:

    $ app-inspector -u xxxxxxxx-xxxx-xxxx

    Macaca元素查看器

    Android

    在真機調試下,因爲app-inspectormacaca-android是兩個文件夾下的,安裝的apk是不一樣的,可能會有問題,最好把這com.maraca.android.testing.test、UiAutomator sample、android-unlock這三個apk從手機裏刪除下。打開Android模擬器或連接真機:

    $ emulator -avd Nexus4

    使用adb命令查看設備的UDID:

    $ adb devices

    啓動元素查看器:

    $ app-inspector -u emulator-5554

    iOS

    app-inspector安裝過程中可以將TEAM_ID通過環境變量傳入即可支持真機,同時,還需要重簽名app-inspecto模塊下的XCTestWD.xcodeproj,Xcode項目所在路徑:“xxx/node_modules/app-inspector/node_modules/xctestwd/XCTestWD/XCTestWD.xcodeproj”,雙擊打開Xcode項目,重簽名XCTestWD和XCTestWDUITests,並能正常執行即可。如果是個人開發者,iOS上要在“設置>通用>設備管理”中信任開發者。

    DEVELOPMENT_TEAM_ID=TEAM_ID npm i app-inspector -g

    打開iOS模擬器或連接真機:

    $ open -a Simulator

    從菜單中打開“Hardware>devices>manage devices”,然後會看到模擬器信息界面,裏面有個identifier,就是UDID,再啓動元素查看器:

    $ app-inspector -u B5393605-DCAF-4EA8-9B2C-4058449FA527

    爲什麼選擇?有的人喜歡創造世界,他們做了程序員。有的人喜歡拯救世界,他們做了測試員。

    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章