UITestHelpers 0.3.2

UITestHelpers 0.3.2

Martin Straub 维护。



  • Martin Straub

UITestHelpers

CocoaPods Platforms License

一组有用的辅助函数,使编写 UI 测试至少减少一点痛苦。

安装

CocoaPods

您可以通过将它们添加到 Podfile 中的 UI 测试部分来安装 UITestHelpers

platform :ios, '10.0'
use_frameworks!

target '<name-of-your-UI-tests-target>' do
  pod 'UITestHelpers'
end

然后运行 pod install.

手动安装

当然,您可以直接将源文件拖入您的项目中,但使用 CocoaPods 更为推荐使用 UITestHelpers。

使用/示例

class MyUITests: XCUITestCase {
    override func setUp() {
        super.setUp()
        self.continueAfterFailure = false
        self.addAlertsHandler(for: ["Allow", "OK"])
        self.app.launchEnvironment = ["AutoCorrection": "Disabled"]
        self.app.launch()
    }

    func testSomething() {
        // tap an element (it's important to wait for an element to exist, before tapping it)
        element.waitAndTap()

        // tap a button
        self.tapButton("buttonAccessibilityIdentifier")

        // tap a cell in a collectionView (scroll to the right, if needed)
        self.tapCollectionViewCell("collectionViewCellAccessibilityIdentifier", in: "collectionViewAccessibilityIdentifier", scrollDirection: .right(100))

        // handle (dismiss) permission alerts
        self.addAlertsHandler(for: ["Allow", "OK"])

        // tap the "Continue" button on the next alert dialog
        self.tapAlertButton(name: "Continue")

        // show keyboard
        self.app.showKeyboard(for: self)

        // really type on the keyboard (sometimes this is needed in contrast to `XCUIElement.typeText`)
        self.app.typeOnKeyboard(text: "1337")

        // hide keyboard
        self.app.hideKeyboard()
    }
}

可靠性

使用 accessibilityIdentifier

为确保测试结果可靠性,建议在可能的情况下使用 accessibilityIdentifier。对于静态内容,您可以直接在 Interface Builder 中设置它们。

如果元素没有 Accessibility 部分,可以使用 User Defined Runtime Attributes

但是,对于动态内容,您需要在代码中设置。有些人可能认为在代码库中添加仅用于测试的元素是一种杂乱无章,但与使用索引等不太可靠的 UI 测试相比,这只是例如在 UITableViewCell 中的一行代码。我宁愿选择这种“杂乱”,也不愿选择可靠性较低的 UI 测试。

let dynamic = "somethingSpecificToThisCell"
self.accessibilityIdentifier = "your\(dynamic)IdentifierHere"

如何确保每个测试的干净应用程序状态

由于 Apple 没有提供一种真正为每个测试清理应用程序状态的方法,我们将必须手动提供自己的“主函数”。为此,我们将使用一个名为 --Reset 的启动参数,并将它传递给测试设置中的 XCUIApplication(参见 XCUITestCase 基类)。

  • 从您的 AppDelegate 中删除 @UIApplicationMain 注释。
  • 在您的项目中创建一个包含以下内容的 main.swift 文件
_ = autoreleasepool {
    if ProcessInfo().arguments.contains("--Reset") {
        // Delete files, whatever...
    }

    UIApplicationMain(
        CommandLine.argc,
        UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to:
            UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)),
        nil,
        NSStringFromClass(AppDelegate.self)
    )
}

另请参阅重置 iOS 模拟器以进行 UI 测试

故障排除

软件键盘没有显示

Xcode 10+

func disableHardwareKeyboard() {
    let setHardwareLayout = NSSelectorFromString("setHardwareLayout:")
    UITextInputMode.activeInputModes
        .filter({ $0.responds(to: setHardwareLayout) })
        .forEach { $0.perform(setHardwareLayout, with: nil) }
}

Xcode 9-

请确保硬件键盘已断开连接。

  • 在模拟器中按下 cmd + shift + k 可以切换硬件键盘。
  • 更好:在 构建阶段 脚本中禁用它,在你的 UI 测试目标中,如下所示,以确保每次测试运行都这样做
defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool NO

调试

Xcode 的 调试视图层次结构 功能

在这里您可以查看元素的所有可访问性信息。

可访问性检查器

使用这个良好的应用(包含在 Xcode 的开发者工具中),以检查模拟器中的应用程序,并获得可访问性信息以及元素的层次结构。

致谢