In Xcode 6, Apple released a framework based on XCTest for UI testing called XCUITest. Not only can we write UI tests as we do for unit testing, but we can also record the keystrokes while using the app. To activate it, we need to open the UI test case and select a test, moving the cursor in the body of any test function. Doing that, the Red button at the left bottom, becomes selectable and we can record the session:
Let's record a simple addition, as follows:
class RpnCalculatorUITests: XCTestCase {
override func setUp() {
continueAfterFailure = false
XCUIApplication().launch()
}
func testAddition() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests
//produce the correct results.
let app = XCUIApplication()
app.buttons["2"].tap()
app.buttons["e"].tap()
app.buttons["3"].tap()
app.buttons["+"].tap()
}
}
The structure is the same as the normal unit test and in the setUp() method, the application is launched.
Running the test, the simulator will launch the app, as activated in setUp, and it will tap in the buttons as specified in the test. However, without an assertion phase, we are not testing anything. In the next section, we'll expand the test by adding the missing assert, and we'll see what else we can do with UI XCTest.