Skip to content

jjrscott/XCTestBackChannel

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

This project demonstrates how to use NSDistributedNotificationCenter to provide simple bidirectional messaging between your iOS app and UI tests that are controlling it. This mechanism has been tested in the iOS Simulator and on an iOS device connected to Xcode.

You should NOT attempt to put this in the AppStore. Expect Apple to reject your app for the use of a private API if you do.

Usage

To send a message from your app to your test:

XCTestBackChannel.shared.sendMessage("Hello")

To receive a message from your app:

func testExample() throws {
    let app = XCUIApplication()
    XCTestBackChannel.shared.delegate = self
    XCTestBackChannel.shared.register(with: app)
    app.launch()
    ...
}

func testBackChannel(handleMessage message: String) {
    print(message)
}

Expectation example

The XCTestBackChannelDemo target and tests use this mechanism to fulfill an explectation.

In the app:

class ViewController: UIViewController {
    @IBAction func tappedButton(_ sender: UIButton) {
        XCTestBackChannel.shared.sendMessage("Hello")
    }
}

In the test:

var expectation : XCTestExpectation?

func testExample() throws {
	expectation = XCTestExpectation(description: "Wait for a message from the app")
    let app = XCUIApplication()
    XCTestBackChannel.shared.delegate = self
    XCTestBackChannel.shared.register(with: app)
    app.launch()
    app.buttons["Button"].tap()
    if let expectation = expectation {
        wait(for: [expectation], timeout: 10)
    }
}

func testBackChannel(handleMessage message: String) {
    print(message)
    expectation?.fulfill()
}

About

Bidirectional messaging between your iOS app and UI tests

Resources

License

Stars

Watchers

Forks