關鍵詞: Xcodebuild ,xcuitest
問題:模擬手動點擊 imessage 中的 https link,link 跳轉到被測試app 的特定頁面。
解決思路:
方法1,使用 url scheme link,即類似於
- 打開 Safari 瀏覽器
x-web-search:// - 備忘錄
mobilenotes:// - 打開地圖
map: // - 給某人發送短信:
sms://手機號
通過
ios_driver.execute_script('mobile: activateApp', {'bundleId': 'com.apple.MobileSMS'})
ios_driver.get(scheme_link_address)
結果,app 是native app,不能按照預期打開link,跳轉到指定頁面。
方法2,appium 測試 ios app,是借用了 xcuitest。那么appium 可以調用uiautomater2 來驅動android 的測試,大概率也可以調用 xcuitest 來操作 ios app。
第一步,創建 xcode project
打開Xcode New-> Project, 選擇 ios app;下一步輸入 product name,勾選 Include tests 選項
第二步,編寫相應 code
使用 swift 來編寫代碼,打開xxxxUITests,在 testExample 方法中添自己需要的代碼
或者可以使用 Xcode 底部的紅色圓點腳本錄制功能,選好了xcode運行的設備,或者模擬器后,xcode 會自動將屏幕的操作錄制為腳本。
第三步,配置可以靈活傳遞的 url 參數
上一步中的變量 meeting_link 是一個需要用戶靈活配置的可變參數,在project 中寫成hard code 后,project 被編譯后,就不能在實際測試中更改。所以最好是讓這一些可以在測試腳本中指定。
Manage scheme -> 添加 xxxUITests scheme。
Edit scheme,編輯 xxxUITests scheme,選擇 Test,添加 Environment variables, 取消 Use the run action‘s argument,選擇 Expand variable based on 為當前的 xxxUITests
此處的變量將由編譯時從 command line 指定
第四步,編譯成可供腳本中調用運行的package
首先確定寫的腳本沒有錯誤,可以使用Xcode 的 Product -Test 指定設備后,先跑一遍。下面將通過 xcode 的command line 來將 project 編譯為 .xctestrun 文件,類似於 “xxxxUITests_iphoneos15.0-arm64.xctestrun”這樣的文件,destination設置的不同,文件名也會有不同。
xcodebuild command line 需要使用 .xcworkspace, 首先新建新一個 workspace,通過 File -> add file, 選擇之前項目創建的 .xcodeproj 文件,將project 添加到workspace。
從terminal 中 編譯:
$ xcodebuild build-for-testing -scheme xcTestSMSUITests -destination name=iPhoneXSMAX -workspace xcTestSMS.xcworkspace -derivedDataPath '/xxx/Auto/xctest/' -allowProvisioningUpdates MEETING_LINK='https://helloworld/meeting'
指定 -scheme 為 xxxUITests 即剛剛設置過變量的;指定 -workspace 即自己創建的workspace名字;-derivedDataPath 指定產生的 .xctestrun 文件的保存位置 ;-destination 腳本要測試的設備或模擬器。
編譯后,在指定的 path 下,會生成 products -> “xxxxUITests_iphoneos15.0-arm64.xctestrun”這樣的文件,將會在下一步中使用。
第五步,在command line 運行測試
$ xcodebuild test-without-building -xctestrun '/Auto/xyyxctest/Build/Products/xxxxUITests_iphoneos15.0-arm64.xctestrun' -destination 'id=00008000-012345E'
指定測試的設備或模擬器 -destination
參考文檔:https://www.jianshu.com/p/183f506414a7
https://www.iplaysoft.com/p/ios-url-scheme