XCUITestHelper

XCUITestHelper 帮助您在 SwiftUI 中编写 UI 测试。它在 XCUIApplicationXCUIElementXCUIElementQuery 上提供了一系列有用的扩展,使您的测试更具可读性且更易于维护。

Swift Package Manager License

要求

安装 (Pakage.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/XCUITestHelper.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "XCUITestHelper", package: "XCUITestHelper"),
    ]),
]

安装 (Xcode)

  1. 在 Xcode 中,打开您的项目并导航到 FileSwift PackagesAdd Package Dependency...
  2. 粘贴仓库 URL (https://github.com/0xWDG/XCUITestHelper) 并点击 Next
  3. 确保您将其添加到 UITest target
  4. 点击 Finish

用法 / 示例

import XCTest
import XCUITestHelper

final class MyAppUITests: XCTestCase {
    override func setUpWithError() throws {
        continueAfterFailure = false
    }

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

        // * Set the app language to English.
        app.setLanguage(to: .english)
        // Do this before launching the app.
        app.launch()

        // * `Wait` for 1 second to continue
        app.wait(for: 1)

        // * Tap a `random` cell in a collection view.
        // Random works with any kind of element, not just buttons.
        app.collectionViews.buttons.random.tap()

        // * Go back to previous screen (NavigationView)
        app.navigateBack()

        // * Tap on the last button
        app.buttons.lastMatch.tap()

        // * Tap on the second button
        app.buttons[1].tap()

        // * Type something, and then clear it.
        let textfield = app.searchFields.firstMatch
        app.type(in: textfield, text: "a", action: .clear)
    }
}

联系方式

🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord

有兴趣了解更多关于 Swift 的信息吗?查看我的博客