2018年3月5日 星期一

XCUITest -- 環境架設



安裝
1.官網下載和安裝Xcode (https://developer.apple.com/xcode/)
2.下載練習用專案 (https://github.com/Mokolea/FoodTracker,使用git clone)
3.使用Xcode開啟專案 (FoodTracker.xcodeproj)


如何顯示view hierarchy

step1
(1)Main.story
(2)執行














step2
(1)Debug view hierarchy,此時可以移動框架了解其架構(圖層法)
(2)如果想看新的頁面必須結束debug mode,點擊continue program execution
(3)此時可以操作app,點擊新增按鈕,換到新的頁面














step3
同理,點擊Debug view hierarchy,就可以反覆切換想要的頁面
但這樣還是不清楚,完整的view hierarchy架構,需要透過命令的方式抓取














如何抓取view hierarchy

step4
建立新的target,File >New >Target >搜尋UI >選擇iOS UI Testing Bundle >Next














step5
Organization Identifier,自行設定識別碼
Language,swift














step6
(1)點擊創建的Target
(2-1)點選行號,代表加入中斷點
(2-2)菱形,代表執行此測項(名字必須"test"開頭)
(3)執行測項後,會中斷在29行,此時輸入命令 po print(app.debugDescription)

PS. 記得宣告常數 let app=XCUIApplication()














執行完命令會得到一大串資料

    t =    23.29s Snapshot accessibility hierarchy for app with pid 21101
    t =    23.66s Snapshot accessibility hierarchy for app with pid 21101
Attributes: Application, 0x604000198600, pid: 21101, {{0.0, 0.0}, {414.0, 736.0}}, label: 'FoodTracker'
Element subtree:
 →Application, 0x604000198600, pid: 21101, {{0.0, 0.0}, {414.0, 736.0}}, label: 'FoodTracker'
    Window, 0x604000197f80, Main Window, {{0.0, 0.0}, {414.0, 736.0}}
      Other, 0x604000198460, traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
        NavigationBar, 0x604000199570, traits: 35192962023424, {{0.0, 20.0}, {414.0, 44.0}}, identifier: 'Your Meals'
          Button, 0x604000199640, traits: 8589934593, {{12.0, 20.0}, {40.7, 44.0}}, label: 'Edit'
          Other, 0x604000199710, traits: 8590000128, {{163.0, 32.0}, {88.0, 20.3}}, label: 'Your Meals'
          Button, 0x6040001997e0, traits: 8589934593, {{366.0, 20.0}, {36.0, 44.0}}, label: 'Add'
        Other, 0x6040001998b0, traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
          Other, 0x604000199980, traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
            Table, 0x604000199a50, traits: 35192962023424, {{0.0, 0.0}, {414.0, 736.0}}
              Cell, 0x604000199b20, traits: 8589934592, {{0.0, 64.0}, {414.0, 90.0}}
                Button, 0x604000199bf0, traits: 8589934601, {{97.0, 101.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000199cc0, traits: 8589934601, {{146.0, 101.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000199d90, traits: 8589934601, {{195.0, 101.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000199f30, traits: 8589934601, {{244.0, 101.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a000, traits: 8589934593, {{293.0, 101.0}, {44.0, 44.0}}, label: 'emptyStar'
                StaticText, 0x604000199e60, traits: 8589934656, {{97.0, 72.0}, {495.0, 21.0}}, label: 'Caprese Salad'
                Image, 0x60400019a0d0, traits: 8589934596, {{0.0, 64.0}, {89.0, 89.0}}, identifier: 'meal1'
                Other, 0x60400019a1a0, traits: 8589934592, {{15.0, 153.5}, {360.0, 0.5}}
                Other, 0x60400019a270, traits: 8589934592, {{20.0, 153.7}, {394.0, 0.3}}
              Cell, 0x60400019a340, traits: 8589934592, {{0.0, 154.0}, {414.0, 90.0}}
                Button, 0x60400019a410, traits: 8589934601, {{97.0, 191.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a4e0, traits: 8589934601, {{146.0, 191.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a5b0, traits: 8589934601, {{195.0, 191.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a680, traits: 8589934601, {{244.0, 191.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a750, traits: 8589934601, {{293.0, 191.0}, {44.0, 44.0}}, label: 'emptyStar'
                StaticText, 0x60400019a820, traits: 8589934656, {{97.0, 162.0}, {495.0, 21.0}}, label: 'Chicken and Potatoes'
                Image, 0x604000197900, traits: 8589934596, {{0.0, 154.0}, {89.0, 89.0}}, identifier: 'meal2'
                Other, 0x604000197830, traits: 8589934592, {{15.0, 243.5}, {360.0, 0.5}}
                Other, 0x6040001975c0, traits: 8589934592, {{20.0, 243.7}, {394.0, 0.3}}
              Cell, 0x60400019a8f0, traits: 8589934592, {{0.0, 244.0}, {414.0, 90.0}}
                Button, 0x6040001979d0, traits: 8589934601, {{97.0, 281.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x60400019a9c0, traits: 8589934601, {{146.0, 281.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000197d10, traits: 8589934601, {{195.0, 281.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000197c40, traits: 8589934593, {{244.0, 281.0}, {44.0, 44.0}}, label: 'emptyStar'
                Button, 0x604000197b70, traits: 8589934593, {{293.0, 281.0}, {44.0, 44.0}}, label: 'emptyStar'
                StaticText, 0x604000197aa0, traits: 8589934656, {{97.0, 252.0}, {495.0, 21.0}}, label: 'Pasta with Meatballs'
                Image, 0x60400019aa90, traits: 8589934596, {{0.0, 244.0}, {89.0, 89.0}}, identifier: 'meal3'
                Other, 0x60400019ab60, traits: 8589934592, {{15.0, 333.5}, {360.0, 0.5}}
                Other, 0x60400019ac30, traits: 8589934592, {{20.0, 333.7}, {394.0, 0.3}}
    Window, 0x60400019ad00, {{0.0, 0.0}, {414.0, 736.0}}
      StatusBar, 0x60400019add0, {{0.0, 0.0}, {414.0, 20.0}}
        Other, 0x60400019aea0, {{0.0, 0.0}, {414.0, 20.0}}
        Other, 0x60400019af70, {{0.0, 0.0}, {414.0, 20.0}}
          Other, 0x60400019b040, traits: 8388608, {{6.0, 0.0}, {39.0, 20.0}}
          Other, 0x60400019b110, traits: 8388608, {{50.0, 0.0}, {14.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
          Other, 0x60400019b1e0, traits: 8389120, {{181.0, 0.0}, {56.0, 20.0}}, label: '10:46 PM'
          Other, 0x60400019b2b0, traits: 8388608, {{383.0, 0.0}, {26.0, 20.0}}, label: '-100% battery power'
Path to element:
 →Application, 0x604000198600, pid: 21101, {{0.0, 0.0}, {414.0, 736.0}}, label: 'FoodTracker'
Query chain:
 →Find: Target Application 0x6040000be060
  Output: {
    Application, 0x604000198600, pid: 21101, {{0.0, 0.0}, {414.0, 736.0}}, label: 'FoodTracker'
  }


如果將不相關的數字資訊刪除

Attributes: Application | label: 'FoodTracker'
Element subtree:
 →Application | label: 'FoodTracker'
    Window | Main Window |
      Other |
        NavigationBar | identifier: 'Your Meals'
          Button | label: 'Edit'
          Other | label: 'Your Meals'
          Button | label: 'Add'
        Other |
          Other |
            Table |
              Cell |
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                StaticText | label: 'Caprese Salad'
                Image | identifier: 'meal1'
                Other |
                Other |
              Cell |
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                StaticText | label: 'Chicken and Potatoes'
                Image | identifier: 'meal2'
                Other |
                Other |
              Cell |
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                Button | label: 'emptyStar'
                StaticText | label: 'Pasta with Meatballs'
                Image | identifier: 'meal3'
                Other |
                Other |
    Window |
      StatusBar |
        Other |
        Other |
          Other |
          Other | label: '3 of 3 Wi-Fi bars' | value: SSID
          Other | label: '10:46 PM'
          Other | label: '-100% battery power'
Path to element:
 →Application | label: 'FoodTracker'
Query chain:


    Application | label: 'FoodTracker'



PS. 刪除不相關數字的程式(Python) [不見得正確,僅供參考]