71

GitHub - fromkk/Lunch: Lunch is helper of UI Test with Swift.

 5 years ago
source link: https://github.com/fromkk/Lunch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Build Status

Lunch

31062950-9f8bf88c-a769-11e7-962a-2f228c4b2de7.png

Lunch is helper of UI Test with Swift.

Requirements

  • Swift 4.0 or later
  • iOS 9 or later

Installation

Carthage

  • Insert github "fromkk/Lunch" to your Cartfile .
  • Run carthage update
  • Link your app with Lunch.framework in Carthage/Build
  • Link your UI test target with LunchTest.framework in Carthage/Build

Usage

In App target adopt protocol 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

NOTE: If you want change rootViewController after Run Xcode, set LAUNCH_VIEW_CONTROLLER key and viewController name to value in Environment Variables of your scheme.

In UI Test target.

1 Add component and adopt protocol 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 Add your tests and adopt protocol 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")
    }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK