0
0
iOS Swiftmobile~20 mins

UI testing with XCUITest in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Simple Counter
A screen with a label showing a number and a button to increase the number by one each time it is tapped.
Target UI
---------------------
| Simple Counter     |
|-------------------|
|                   |
|       0           |
|                   |
|   [Increment]     |
---------------------
Display a label with initial number 0 centered on screen
Add a button labeled 'Increment' below the label
Tapping the button increases the number by 1 and updates the label
Write an XCUITest that launches the app, taps the Increment button 3 times, and verifies the label shows '3'
Starter Code
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 20) {
            Text("\(count)")
                .font(.largeTitle)
                .accessibilityIdentifier("countLabel")
            Button("Increment") {
                // TODO: Increase count by 1
            }
            .accessibilityIdentifier("incrementButton")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Task 1
Solution
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 20) {
            Text("\(count)")
                .font(.largeTitle)
                .accessibilityIdentifier("countLabel")
            Button("Increment") {
                count += 1
            }
            .accessibilityIdentifier("incrementButton")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

// XCUITest file
import XCTest

final class SimpleCounterUITests: XCTestCase {
    var app: XCUIApplication!

    override func setUpWithError() throws {
        continueAfterFailure = false
        app = XCUIApplication()
        app.launch()
    }

    func testIncrementButtonIncreasesCount() throws {
        let incrementButton = app.buttons["incrementButton"]
        let countLabel = app.staticTexts["countLabel"]

        XCTAssertEqual(countLabel.label, "0")

        incrementButton.tap()
        incrementButton.tap()
        incrementButton.tap()

        XCTAssertEqual(countLabel.label, "3")
    }
}

The SwiftUI view uses a @State variable count to hold the number. The Text shows the current count and has an accessibility identifier countLabel so the UI test can find it. The Button labeled "Increment" increases the count by 1 when tapped and has an accessibility identifier incrementButton.

The XCUITest launches the app, finds the button and label by their accessibility identifiers, taps the button 3 times, and checks that the label updates to "3". This confirms the UI behaves as expected.

Final Result
Completed Screen
---------------------
| Simple Counter     |
|-------------------|
|                   |
|       3           |
|                   |
|   [Increment]     |
---------------------
User taps the Increment button
The number in the label increases by 1 each tap
After 3 taps, the label shows '3'
Stretch Goal
Add a Reset button that sets the count back to 0
💡 Hint
Add another Button below Increment with label 'Reset' and set count = 0 in its action