swift下使用KIF进行UI测试

790 阅读3分钟

swift下使用KIF进行UI测试

最近看了一下Quick+Nimble测试框架,准备在看一下KIF这个测试UI库,奈何一直报错,提示: 'KIF/KIF.h' file not found,于是这篇文章就出来了

1.安装 Via Cocoapods

引用段落 我们在使用一些优秀的三方库时,最好看一下官方的安装以及使用说明,尽量自己先熟悉一遍流程,不然出错了就抓瞎了。注意一点:KIF要引入在你的工程对应的单元测试的Target下!KIF要引入在你的工程对应的单元测试的Target下!KIF要引入在你的工程对应的单元测试的Target下!,一般情况下,通过cocapods的方式引入KIF,其他不需要额外配置,比如framework路径啥的....

我的Podfile文件:

platform :ios, '9.0'
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

target "testDemo" do
  # Normal libraries
  use_frameworks!
end

def testing_pods
    pod 'Quick'
    pod 'Nimble'
    
end

target 'testDemoTests' do
    use_frameworks!
    pod 'KIF', :configurations => ['Debug']
    testing_pods
end

target 'testDemoUITests' do
    use_frameworks!
    testing_pods
end

这里还有一个问题就是网速问题,都知道最近墙加厚了加高了,有时真的很难拉下来,要么选择工具,这里可以替换一下gem源,设置方式在这又想起了,群里一个群友问,cocoapods下载下不下来怎么办啊,我说,移除Trunk源,替换gem源清华源,podfile加上这个source,我觉得做开发的基本知道我在说什么,自己知道改怎么去做或者自己知道去搜索自己想要的东西,结果那哥们说:你说半天,和没说一样,真的很无语..... ##2.swift项目之桥接文件 我在这卡住了大概2天 ,😿😭,可以说还是自己的理解不是很够。通常我们的桥接文件是建立在你的工程目录下的,这样你的target就可以通过桥接文件找到你的三方库但是你看它的这个podfile,KIF是在YourAppTests这个target下的,那是有妖啊那么我们需要在你的单元测试的target下建立桥接文件,这样你的这个单元测试的target就可以找到KIF了

截屏2020-03-10上午8.30.20.jpg

3. 简单的登录界面测试

需要一个示例简单的说一下这个东西怎么用(其实我也不会😀)

1.首先把这个扩展加上

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)
    }
}

2.Mainstoryboard 拖一下界面

设置AccessibilityLabel,类似id通过该值获取控件,其他设置keypath的方式我没用,觉得麻烦,

截屏2020-03-10上午9.52.25.png

3.测试代码

import KIF

@testable import testDemo
class LoginTestCase: KIFTestCase {
    override func beforeAll() {
    }
    
    func tester00Login() {
        let phoneTextField = tester().waitForView(withAccessibilityLabel: "phone") as! UITextField
        let passwordTextField = tester().waitForView(withAccessibilityLabel: "password") as! UITextField
        tester().enterText("15066668888", intoViewWithAccessibilityLabel: "phone")
        tester().enterText("123456", intoViewWithAccessibilityLabel: "password")
        
        //点击
        tester().tapView(withAccessibilityLabel: "login")
        XCTAssertTrue(!phoneTextField.text!.isEmpty, "phoneText should not be empty!")
        XCTAssertTrue(!passwordTextField.text!.isEmpty, "password should not be empty!")
        
    }
}
如果你走到这了,恭喜你就报错了 😀😀,为啥? what? WTF?为啥报错:

截屏2020-03-10上午9.56.29.png

4.错误解决(悄悄的笑两声就行了)

首先我们应该会检查控件上的这个AccessibilityLabel是否存在值,通过Xcode-> Open Develop Tool -> Accessibility Inspector,控件的这个值AccessibilityLabel存在的,为啥???等等,我们的App貌似没启动啊 😲,更正:2020/03/17 这个地方应该不需要启动App,暂时不需要这个方法。 正确代码如下

import KIF

@testable import testDemo
class LoginTestCase: KIFTestCase {

    func tester00Login() {
        let phoneTextField = tester().waitForView(withAccessibilityLabel: "phone") as! UITextField
        let passwordTextField = tester().waitForView(withAccessibilityLabel: "password") as! UITextField
        tester().enterText("15066668888", intoViewWithAccessibilityLabel: "phone")
        tester().enterText("123456", intoViewWithAccessibilityLabel: "password")
        
        //点击
        tester().tapView(withAccessibilityLabel: "login")
        XCTAssertTrue(!phoneTextField.text!.isEmpty, "phoneText should not be empty!")
        XCTAssertTrue(!passwordTextField.text!.isEmpty, "password should not be empty!")
        
    }
}