簡介
常見的基于UI的測試框架有KIF, EarlGrey, WebDriverAgent, Frank, Calabash, Appium, UIAutomation(已被官方放棄), UITesting
- 其中一類是通過Apple私有API操作UI,例如tap, doubleTap, swipe等模擬用戶操作,好處是可以訪問到原項目中的所有代碼,可以做相關的驗證操作,比起完全獨立的UITest方便很多,常見的有KIF, EarlGrey
- 另一類框架是在App中注入一個Server(通常通過單元測試的項目注入), 通過Server對外通信完成 UI 操作指令的執行。常見的框架有WebDriveAgent, Appium, Calabash, Frank
- 系統自帶的UITesting在試用之后發現諸多不穩定的情況,例如,控件狀態更新不及時,甚至不更新,導致測試結果很不穩定,故放棄
KIF框架基于UnitTest項目,UI操作覆蓋較為全面,并且使用的就是xcode自帶的單元測試,使用起來較為方便,支持OC和Swift,作者維護較為頻繁,支持swift,能覆蓋到大部分的測試用例,穩定性相對于UITesting較為良好,故采用該方案
基本使用
1. 創建一個UnitTest項目,可以通過Pod導入KIF庫
pod 'KIF', '~> 3.5.1'
2. 使用KIFTestCase
作為用例類
import KIF
class KIFDemoTests: KIFTestCase {
override func beforeAll() {
}
override func beforeEach() {
}
override func afterAll() {
}
override func afterEach() {
}
func testExample() {
}
}
從字面上很好理解,上面重載的四個方法就是用例執行前后執行的,類似于XCTestCase
的tearDown
和setUp
,而用例的編寫與UnitTest一樣,使用test開頭,無返回值無參數
3. 說明
- 使用KIF測試的所有元素必須設置
accessibilityIdentifier
或accessibilityLabel
才能被KIF框架訪問到
- accessibilityIdentifier: 可以沒有意義,需要保證唯一
- accessibilityLabel:需要有意義,可以被VoiceOver訪問,故通常使用Identifier標識控件,而
accessibilityLabel
的值通常是控件上的文本
- 如果是swift項目,需要添加一個方法獲取
KIFUITestActor
對象,通常叫tester()
import KIF
extension XCTestCase {
func tester(file : String = #file, _ line : Int = #line) -> KIFUITestActor {
return KIFUITestActor(inFile: file, atLine: line, delegate: self)
}
func system(file : String = #file, _ line : Int = #line) -> KIFSystemTestActor {
return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
}
}
extension KIFTestActor {
func tester(file : String = #file, _ line : Int = #line) -> KIFUITestActor {
return KIFUITestActor(inFile: file, atLine: line, delegate: self)
}
func system(file : String = #file, _ line : Int = #line) -> KIFSystemTestActor {
return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
}
}
- KIF庫默認只支持
accessibilityLabel
,如果需要使用accessibilityIdentifier
需要添加擴展,擴展可以在這里找到
https://github.com/kif-framework/KIF/tree/bcb58d0419cfaefe286f3df1c8618bac43b45921/IdentifierTests
- 由于
waitForView
方法每次獲取到的都需要裝換,這里封裝一個泛型方法
extension KIFUITestActor {
func waitFor<T: UIView>(identifier: String) -> T {
return self.waitForView(withAccessibilityIdentifier: identifier) as! T
}
}
4. API
- 等待一個元素
let loginBtn = tester().waitForView(withAccessibilityIdentifier: "loginvc.loginbtn");
let loginBtn2 = tester().waitForView(withAccessibilityLabel: "Login")
- 設置超時
tester().usingTimeout(1).waitForView(withAccessibilityIdentifier: "loginvc.loginbtn")
- 等待元素消失
tester().usingTimeout(30).waitForAbsenceOfView(withAccessibilityIdentifier: "hud")
- 輸入文本
//默認enterText方法在輸完之后會進行驗證,只輸入不刪除會驗證不通過,故最好使用clearText方法
tester().enterText("hehe??哈哈\n", intoViewWithAccessibilityIdentifier: "textField")
tester().clearText(fromAndThenEnterText: "hehe??哈哈\n", intoViewWithAccessibilityIdentifier: "textField")
- 設置Slider
let slider = tester().waitForView(withAccessibilityIdentifier: "slider") as! UISlider
tester().setValue(0.5, for: slider)
tester().setValue(0.8, forSliderWithAccessibilityIdentifier: "slider")
- 設置Switch
tester().setOn(false, forSwitchWithAccessibilityIdentifier: "switch")
- 設置DatePicker
tester().tapView(withAccessibilityIdentifier: "datePicker")
let data = ["Sat Mar 4", "3", "32", "AM"]
tester().selectDatePickerValue(data)
tester().tapView(withAccessibilityIdentifier: "accessoryView.done")
- 設置Picker
tester().tapView(withAccessibilityIdentifier: "picker")
tester().selectPickerViewRow(withTitle: "c", inComponent: 0)
tester().selectPickerViewRow(withTitle: "2", inComponent: 1)
tester().tapView(withAccessibilityIdentifier: "accessoryView.done")
- 點擊狀態欄
tester().tapStatusBar()
- swipe滑動
tester().swipeView(withAccessibilityIdentifier: "tableView", in: .up)
tester().swipeView(withAccessibilityIdentifier: "tableView", in: .down)
- 拖動交換cell
tester().moveRow(at: IndexPath(row: 0, section: 0) , to: IndexPath(row: 3, section: 0), inTableViewWithAccessibilityIdentifier: "tableView")
- 點擊任意位置
tester().tapScreen(at: CGPoint(x: 200, y: 100))
- 監聽系統彈框
tester().acknowledgeSystemAlert()
由于自帶的acknowledgeSystemAlert只能處理一個,如果同時彈出多個系統框會無法處理,故封裝一個方法可以處理多個
extension KIFUITestActor {
/// handle multi system alerts
func handleSystemAlert() -> Bool {
var count = 0
while acknowledgeSystemAlert() {
count += 1
}
return count > 0
}
}
- 對于系統框或者框默認框可以使用accessibilityLabel處理,如
alertView
tester().tapView(withAccessibilityIdentifier: "alertView")
//按鈕沒有設置identifier,可以直接使用按鈕的文本
tester().tapView(withAccessibilityLabel: "好的")
- 獲取tableviewcell的個數,隨機點擊
KIF不像UITesting一樣可以直接獲取到tableview的cell的個數,我們可以通過獲取到控件,根據控件獲取到cell的個數,然后隨機點擊
let tableView = tester().waitForView(withAccessibilityIdentifier: "tableView") as! UITableView
let rowCount = tableView.numberOfRows(inSection: 0);
let rowIndex = Int(arc4random_uniform(UInt32(rowCount)))
tester().tapRow(at: IndexPath(row: rowIndex, section: 0), in: tableView)
關于API,官方的Demo中有大量的例子,這里只是舉出常用的,更多文檔參見官方文檔
例子
下面是我寫的一個登陸的用例
func testLogin() {
//進入登錄頁面
if tester().tryFindingView(withAccessibilityIdentifier: "guideview.logBtn") {
tester().tapView(withAccessibilityIdentifier: "guideview.logBtn")
}
//判斷是否是注冊頁面
if tester().tryFindingView(withAccessibilityIdentifier: "registevc.registebtn") {
tester().tapView(withAccessibilityIdentifier: "registevc.loginbtn")
}
//切換到手機號登錄
if !tester().tryFindingView(withAccessibilityIdentifier: "loginvc.phonearea") {
tester().tapView(withAccessibilityIdentifier: "loginvc.changemode")
}
//修改手機號的區號
let areaLabel: UILabel = tester().waitFor(identifier: "loginvc.phonearea")
if !areaLabel.text!.contains("86") {
tester().tapView(withAccessibilityIdentifier: "loginvc.phonearea")
tester().tapView(withAccessibilityIdentifier: "86")
}
//輸入賬號密碼
tester().clearText(fromAndThenEnterText: "18600**0785", intoViewWithAccessibilityIdentifier: "loginvc.phonetf")
tester().clearText(fromAndThenEnterText: "123456", intoViewWithAccessibilityIdentifier: "loginvc.phonepwdtf")
//登陸
tester().tapView(withAccessibilityIdentifier: "loginvc.loginbtn")
//等待登錄完成(TODO:重試)
tester().usingTimeout(30).waitForAbsenceOfView(withAccessibilityIdentifier: "loginvc.loginbtn")
//TODO:驗證進入主頁面
}
demo:https://github.com/zhengbomo/KIFDemo
參考
最后安利一下自己的博客:https://zhengbomo.github.io