Use swift test unit (v) of the tripartite UI test framework KIF

1, KIF Introduction

         KIF stands for Keep it functional. It is a UI based on the test frame XCTest to control by accessibility - target specific, proprietary API to reuse UI operation. Because it is built on XCTest, so you can be the perfect test means XCode tools (including command-line script

         KIF framework mandatory use your code to support accessibility. If you have not previously come into contact with, you can take a look at Apple's documentation Accessibility Programming Guide for iOS .

         In simple terms, accessibility allows visually impaired people to use your App. Each control has a description AccessibilityLabel. When VoiceOver is turned on, you can select and click controls hear the corresponding description. Typically UIKit control accessibility of support, self-defined by the control code or set Storyboard.

 

2, KIF use

1) cocopod introduced KIF frame

pod 'KIF', '~> 3.0'

2) In the UI test files created in import, test class inherits class into  KIFTestCase

import KIF

3) is configured in the project

(1)“Build Phases”:

设置 Target Dependencies , UI 自动化测试固然要依赖应用程序的 App 产物,所以需保证应用程序 Target 被添加在 Test Target 的 Target Dependencies 中。

(2)“Build Settings”:     

设置 “Bundle loader” 为:$(BUILT_PRODUCTS_DIR)/MyApp.app/MyApp;     

设置 “Test Host” 为:$(BUILT_PRODUCTS_DIR);     设置 “Wrapper Extensions” 为:xctest。

4) AccessibilityLabel attribute configuration control

There are three configurations

(1) is provided by the Runtime Attributes (KVC)

(2) set by the GUI, select control, to find the position shown in the figure, fill properties AccessibilityLabel

(3) set by the code

kifBtn.accessibilityLabel = "KIF"

5) the specific use

(1) When writing KIF OC, Swift can not directly use the key method tester, system (defined by the macro), reference GitHub the README , achieved by extension

Writing in the UI test files

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) KIF use

Steps:

点击button按钮-->跳转至登录界面-->输入用户名、密码,点击login-->跳转至tab导航界面,切换不同tab的操作流程



class LoginTestCase: KIFTestCase {
    /// 所有测试进行前调用,可以做一些必要或者共有的初始化操作
    override func beforeAll() {

    }

    /// 测试点击登陆后进入其他View
    func test00Login() {
        tester().tapView(withAccessibilityLabel: "button")
        tester().waitForView(withAccessibilityLabel: "login")
        
        let nameTextField = tester().waitForView(withAccessibilityLabel: "UserName") as! UITextField
        let pwdTextField = tester().waitForView(withAccessibilityLabel: "Password") as! UITextField
        tester().enterText("User", intoViewWithAccessibilityLabel: "UserName")
        tester().enterText("123456", intoViewWithAccessibilityLabel: "Password")
        // 点击
        tester().tapView(withAccessibilityLabel: "login")
        XCTAssertTrue(!nameTextField.text!.isEmpty, "User name can't be nil")
        XCTAssertTrue(!pwdTextField.text!.isEmpty, "Password can't be nil")

        // 等待某个View出现,也可以通过该函数得到该View的实例
        tester().waitForView(withAccessibilityLabel: "Scene1")
    }

    /// 测试切换不同的View
    func test01TabButtons() {
        tester().tapView(withAccessibilityLabel: "Tab2")
        tester().waitForView(withAccessibilityLabel: "Scene2")

        tester().tapView(withAccessibilityLabel: "Tab3")
        tester().waitForView(withAccessibilityLabel: "Scene3")
    }

    /// 测试输入
    func test02Input() {
        tester().tapView(withAccessibilityLabel: "Tab1")
        tester().waitForView(withAccessibilityLabel: "InputTextField")
        // 输入文字
        tester().enterText("Hello KIF", intoViewWithAccessibilityLabel: "InputTextField")

        // TODO: 如何确定键盘弹出的done按钮的accessibilityLabel?
        tester().tapView(withAccessibilityLabel: "Scene1View")

        // 得到View实例
        let textfield = tester().waitForView(withAccessibilityLabel: "InputTextField") as! UITextField
        // 判断是否相等
        XCTAssertEqual(textfield.text, "Hello KIF")

        // 清除当前文字重新输入新的
        tester().clearText(fromAndThenEnterText: "Test Geometry", intoViewWithAccessibilityLabel: "InputTextField")
        tester().tapView(withAccessibilityLabel: "Scene1View")
    }

    /// 有的时候无法确定accessibilityLabel或者是自定义的空间没有accessibilityLabel,可以使用Geometry计算位置进行
    func test03Geometry() {
        tester().wait(forTimeInterval: 2)

        let stepper = tester().waitForView(withAccessibilityLabel: "stepper") as! UIStepper
        let stepCenter = stepper.window?.convert(stepper.center, from: stepper.superview)
        // 算出-和+的位置
        var minusButton = stepCenter
        minusButton?.x -= stepper.frame.width / 4
        var plusButton = stepCenter
        plusButton?.x += stepper.frame.width / 4

        for _ in 0 ..< 10 {
            tester().wait(forTimeInterval: 1)
            tester().tapScreen(at: minusButton!)
        }

        // 延时1秒
        tester().wait(forTimeInterval: 1)
        tester().tapScreen(at: plusButton!)
        tester().wait(forTimeInterval: 1)
        tester().tapScreen(at: plusButton!)
        tester().wait(forTimeInterval: 1)

        // 设置超时时间(默认为10秒)
        KIFUITestActor.setDefaultTimeout(60)

        tester().tapView(withAccessibilityLabel: "Tab2")

        KIFUITestActor.setDefaultTimeout(10)
    }

}

 

github demo地址

 

参考地址:

KIF API中文翻译(一)

基于 KIF 的 iOS UI 自动化测试和持续集成

iOS自动化测试的那些干货

Guess you like

Origin blog.csdn.net/lin1109221208/article/details/93050723