Lunch 是使用 Swift 进行 UI 测试的助手。
github "fromkk/Lunch"
添加到你的 Cartfile
文件中。carthage update
Carthage/Build
目录下的 Lunch.framework
链接。Carthage/Build
目录下的 LunchTest.framework
链接。在应用目标中,采纳 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")
}
}