user13540254
user13540254

Reputation:

How to test on several devices with UITest in SwiftUI

I need to test on several iPhones and several iPads. I'm now switch the target device manually by the following menu.

enter image description here

This task consumes a lot of time. So I want to switch the target device automatically. I'm now using Xcode 11.5. Could you tell me how to do this?

import SwiftUI

struct ContentView: View {
    @State var flag = false

    var body: some View {
        VStack {
            Text(flag ? "Apple" : "Peach")
            .accessibility(identifier: "Text")
            Button(action: {
                self.flag.toggle()
            }) {
                Text("Update")
            }
            .accessibility(identifier: "Button")
        }
    }
}

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

class swiftui_playgroundUITests: XCTestCase {

    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = false

        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func addScreenshot(screenshot: XCUIScreenshot) {
        let attachment = XCTAttachment(screenshot: screenshot)
        attachment.lifetime = .keepAlways
        add(attachment)
    }

    func testExample() throws {
        // UI tests must launch the application that they test.
        let app = XCUIApplication()
        app.launch()

        // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        let text = app.staticTexts["Text"]
        XCTAssertEqual(text.label, "Peach")
        addScreenshot(screenshot: app.windows.firstMatch.screenshot())

        let button = app.buttons["Button"]
        button.tap()

        XCTAssertEqual(text.label, "Apple")
        addScreenshot(screenshot: app.windows.firstMatch.screenshot())
    }
}

Upvotes: 1

Views: 1161

Answers (1)

Volker88
Volker88

Reputation: 654

You can setup Xcode server for continuous integration. There you can specify on which devices your test should run and execute the tests manually, on commit or scheduled.

Or you can run the tests via terminal and specify multiple target devices.

Im using below code to run my UI testplan on multiple devices and create screenshots for App Store automatically

xcodebuild test -testPlan ScreenshotTests -project Sensor-App.xcodeproj -scheme Sensor-App \
-destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=13.4'  \
-destination 'platform=iOS Simulator,name=iPhone 11 Pro,OS=13.4' \
-destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.4'  \
-destination 'platform=iOS Simulator,name=iPad Pro (12.9-inch) (4th generation),OS=13.4'

Upvotes: 4

Related Questions