Build Status

午餐 (Lunch)

Lunch 是使用 Swift 进行 UI 测试的助手。

要求

安装

Carthage

用法

在应用目标中,采纳 Creatable 协议。

import Lunch

struct Creator: Creatable {
    func create<T>(_ identifier: String, userInfo: [AnyHashable : Any]?) -> T? {
        switch identifier {
        case "LunchViewController":
            return self.lunchViewController() as? T
        default:
            return nil
        }
    }
}

extension Creatable {
    func lunchViewController() -> LunchViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        return storyboard.instantiateInitialViewController() as! LunchViewController
    }
}
// AppDelegate.swift
import Lunch

let creator = Creator()
let rootViewController: UIViewController
#if DEBUG
if let viewController: UIViewController = Launcher(with: creator).launch() {
    rootViewController = viewController
} else {
    rootViewController = creator.lunchViewController()
}
#else
rootViewController = creator.lunchViewController()
#endif
window?.rootViewController = rootViewController

注意:如果你想在 Xcode Run 之后更改 rootViewController,请在你的 scheme 的 Environment Variables 中设置 LAUNCH_VIEW_CONTROLLER 键,并将 viewController 的名称设置为值。

在 UI 测试目标中。

1. 添加组件并采纳 PageObjectsRepresentable 协议。

import XCTest
import LunchTest

struct LunchViewControllerPage: PageObjectsRepresentable {
    var app: XCUIApplication
    init(app: XCUIApplication) {
        self.app = app
    }

    var lunchLabel: XCUIElement {
        return self.app.staticTexts["lunchLabel"]
    }
}

2. 添加你的测试并采纳 ViewControllerTestable 协议。

import XCTest
import LunchTest

class LunchViewControllerTests: XCTestCase, ViewControllerTestable {

    var viewControllerName: String {
        return "LunchViewController"
    }

    override func setUp() {
        super.setUp()

        continueAfterFailure = false
    }

    func testLunchLabel() {
        let launcher = Launcher(targetViewController: self)
        let app = launcher.launch()

        let page = LunchViewControllerPage(app: app)
        XCTAssertTrue(page.lunchLabel.exists)
        XCTAssertEqual(page.lunchLabel.label, "Lunch")
    }
}