iOS自動化測試:OC和Swift項目铆遭,猴子測試SwiftMonkey
前話
公司對項目的要求越來越來高屉来,崩潰率,卡頓等等都有明確的指標限制。 所以作為開發(fā)人員的自己也需要投入很大的精力去自測。但是即使這樣妄均,也很難保證涵蓋的所有的測試場景柱锹。 前段時間,了解到猴子測試丰包,年前正好有一點點空閑禁熏,有時間試驗一下。
所謂的猴子測試邑彪,就是模擬用戶隨機操作我們的app,以求暴露問題瞧毙。
最開始最有名的是ui-auto-monkey,執(zhí)行代碼是js寄症∩ぃ可惜在XCode7之后,XCode的Profile里瘸爽,移除了UI Automation
, 沒有了明確的入口铅忿。自己試了幾下剪决,沒有試出來。 不過不曉得放到'Run Script'里是否可行檀训。
基于ui-auto-monkey柑潦,后來的開發(fā)者們,也開發(fā)了很多monkey峻凫,功能也更加豐富渗鬼,這次我用的是SwiftMonkey。
話不多說荧琼,寫個demo實操起來譬胎。
一.OC項目運行SwiftMonkey
創(chuàng)建項目
把SwiftMonkey中的SwiftMonkeyPaws和SwiftMonkey拖入到工程
創(chuàng)建UI測試
記住選擇語言是:Swift
這一步很關鍵,引入庫
主工程中設置允許Swift運行
主工程導入SwiftMonkeyPaws,這是為了顯示出猴子的爪子
把顯示爪子的Swift混編進AppDelegate
最后把UI Test代碼替換SMonkeyUITestUITests.swift
文件中的代碼
```
import XCTest
import SwiftMonkey
class SMonkeyUITestUITests: XCTestCase {
override func setUp() {
super.setUp()
XCUIApplication().launch()
}
override func tearDown() {
super.tearDown()
}
func testMonkey() {
let application = XCUIApplication()
// Workaround for bug in Xcode 7.3. Snapshots are not properly updated
// when you initially call app.frame, resulting in a zero-sized rect.
// Doing a random query seems to update everything properly.
// TODO: Remove this when the Xcode bug is fixed!
_ = application.descendants(matching: .any).element(boundBy: 0).frame
// Initialise the monkey tester with the current device
// frame. Giving an explicit seed will make it generate
// the same sequence of events on each run, and leaving it
// out will generate a new sequence on each run.
let monkey = Monkey(frame: application.frame)
//let monkey = Monkey(seed: 123, frame: application.frame)
// Add actions for the monkey to perform. We just use a
// default set of actions for this, which is usually enough.
// Use either one of these but maybe not both.
// XCTest private actions seem to work better at the moment.
// UIAutomation actions seem to work only on the simulator.
monkey.addDefaultXCTestPrivateActions()
//monkey.addDefaultUIAutomationActions()
// Occasionally, use the regular XCTest functionality
// to check if an alert is shown, and click a random
// button on it.
monkey.addXCTestTapAlertAction(interval: 100, application: application)
// Run the monkey test indefinitely.
monkey.monkeyAround()
}
}
點擊這兩個地方開始錄制運行猴子測試
需要特別注意
-
第五步很關鍵,否則你會出現(xiàn)下邊這種報錯
``` 2019-01-30 14:06:28.435245+0800 SMonkey2UITests-Runner[19083:829278] [AXMediaCommon] Unable to look up screen scale 2019-01-30 14:06:28.435344+0800 SMonkey2UITests-Runner[19083:829278] [AXMediaCommon] Unexpected physical screen orientation 2019-01-30 14:06:28.467172+0800 SMonkey2UITests-Runner[19083:829278] [AXMediaCommon] Unable to look up screen scale 2019-01-30 14:06:28.483581+0800 SMonkey2UITests-Runner[19083:829278] [AXMediaCommon] Unable to look up screen scale 2019-01-30 14:06:28.483717+0800 SMonkey2UITests-Runner[19083:829278] [AXMediaCommon] Unexpected physical screen orientation 2019-01-30 14:06:28.513119+0800 SMonkey2UITests-Runner[19083:829278] Running tests... 2019-01-30 14:06:28.596410+0800 SMonkey2UITests-Runner[19083:829278] The bundle “SMonkey2UITests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle. 2019-01-30 14:06:28.596650+0800 SMonkey2UITests-Runner[19083:829278] (dlopen_preflight(/Users/yiche/Library/Developer/Xcode/DerivedData/SMonkey2-hkrzlpioecqhkkdqstkqwvygwggs/Build/Products/Debug-iphonesimulator/SMonkey2UITests-Runner.app/PlugIns/SMonkey2UITests.xctest/SMonkey2UITests): Library not loaded: @rpath/libswiftSwiftOnoneSupport.dylib Referenced from: /Users/yiche/Library/Developer/Xcode/DerivedData/SMonkey2-hkrzlpioecqhkkdqstkqwvygwggs/Build/Products/Debug-iphonesimulator/SwiftMonkey.framework/SwiftMonkey Reason: no suitable image found. Did find: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftSwiftOnoneSupport.dylib: mach-o, but not built for iOS simulator)
如果在
SMonkeyUITestUITests.swift
文件中出現(xiàn)提示No such module 'SwiftMonkey'
之類的提示, 點擊錄制按鈕, 運行一下測試文件, 可以消除。
二.Swift項目運行SwiftMonkey
Swift項目運行SwiftMonkey更簡單了命锄,這里不做介紹了堰乔,步驟和OC項目基本一樣,除去OC專門混編的步驟即可脐恩。
后話
除了SwiftMonkey這個猴子測試項目外镐侯, 還有其他的具有測試步驟截圖、測試結果上傳等功能更加豐富的MonkeyTest驶冒。感興趣苟翻,大家可以自己多研究一下。
比如這個:《基于 XCTestWD骗污,swiftmonkey 二次開發(fā)崇猫,實現(xiàn)無需插樁的 iOS monkey 自動化工具 fastmonkey》
參考文章
《OC-測試:monkey For OC(iOS 猴子測試)》
交流
希望能和大家交流技術
Blog:http://www.lilongcnc.cc