AccessibilitySnapshot

CI Status Version License Platform

AccessibilitySnapshot 让在 iOS 应用中添加辅助功能回归测试变得简单。该框架基于快照测试的思想,提供辅助功能层级的快照。

入门

默认情况下,AccessibilitySnapshot 使用 SnapshotTesting 来记录快照并执行比较。该框架还支持使用 iOSSnapshotTestCase 作为快照引擎。在设置辅助功能快照测试之前,请确保您的项目已设置为标准快照测试。辅助功能快照测试要求测试目标具有宿主应用程序。 请参阅下面的扩展部分,获取可用快照选项的列表。

CocoaPods

通过将以下内容添加到您的 Podfile 中,使用 CocoaPods 安装:

pod 'AccessibilitySnapshot'

要仅使用核心辅助功能解析器,请仅添加对 Core 子规范的依赖项:

pod 'AccessibilitySnapshot/Core'

或者,如果您希望使用 iOSSnapshotTestCase 执行图像比较,则可以添加对 iOSSnapshotTestCase 子规范的依赖项(或者同时添加 - 您可以在同一个项目中使用两者):

pod 'AccessibilitySnapshot/iOSSnapshotTestCase'
Swift Package Manager

通过将以下内容添加到您的 Package.swift 中,使用 Swift Package Manager 安装:

dependencies: [
    .package(name: "AccessibilitySnapshot", url: "https://github.com/cashapp/AccessibilitySnapshot.git", from: "0.4.1"),
]

接下来,将 AccessibilitySnapshot 作为依赖项添加到您的测试目标:

targets: [
    .target(name: "MyApp"),
    .testTarget(name: "MyAppTests", dependencies: ["MyApp", "AccessibilitySnapshot"])
]

要仅使用核心辅助功能解析器,请仅添加对 Core 库的依赖项:

targets: [
    .target(name: "MyApp"),
    .testTarget(name: "MyAppTests", dependencies: ["MyApp", "AccessibilitySnapshotCore"])
]

要使用 iOSSnapshotTestCase 执行图像比较,请为 Swift 测试添加对 FBSnapshotTestCase+Accessibility 的依赖项,或者为 Objective-C 添加对 FBSnapshotTestCase+Accessibility-ObjC 的依赖项。

targets: [
    .target(name: "MyApp"),
    .testTarget(name: "MyAppTests", dependencies: ["MyApp", "FBSnapshotTestCase+Accessibility"])
]
Carthage

只有核心辅助功能解析器(而不是快照集成层)可以通过 Carthage 安装。 要通过 Carthage 安装 AccessibilitySnapshotCore,请将以下内容添加到您的 Cartfile 中:

github "cashapp/AccessibilitySnapshot"

用法

AccessibilitySnapshot 构建在现有快照框架之上,以添加对快照应用程序辅助功能的的支持。 默认情况下,它使用 SnapshotTesting 框架进行快照,但也可以切换到 iOSSnapshotTestCase

SnapshotTesting 入门

AccessibilitySnapshot 提供了一个 .accessibilityImage 快照策略,可以与 SnapshotTesting 的快照断言一起使用。

func testAccessibility() {
    let view = MyView()
    // Configure the view...

    assertSnapshot(matching: view, as: .accessibilityImage)
}

快照也可以通过几种方式进行自定义,例如控制何时包含每个元素的辅助功能激活点的指示器。 默认情况下,当激活点与该视图的默认激活点不同时,将显示这些指示器。 您可以为每个快照覆盖此行为:

func testAccessibility() {
    let view = MyView()
    // Configure the view...

    // Show indicators for every element.
    assertSnapshot(matching: view, as: .accessibilityImage(showActivationPoints: .always))

    // Don't show any indicators.
    assertSnapshot(matching: view, as: .accessibilityImage(showActivationPoints: .never))
}

iOSSnapshotTestCase 入门

要运行快照测试,只需调用 SnapshotVerifyAccessibility 方法:

func testAccessibility() {
    let view = MyView()
    // Configure the view...

    SnapshotVerifyAccessibility(view)
}

由于 AccessibilitySnapshot 构建在 iOSSnapshotTestCase 之上,因此它使用相同的机制来记录快照(设置 self.recordMode 属性),并支持许多相同的功能,例如设备无关的文件名和为每个快照指定标识符:

func testAccessibility() {
    let view = MyView()
    // Configure the view...

    SnapshotVerifyAccessibility(view, identifier: "identifier")
}

快照还可以选择性地包括每个元素的辅助功能激活点的指示器。 默认情况下,当激活点与该视图的默认激活点不同时,将显示这些指示器。 您可以为每个快照覆盖此行为:

func testAccessibility() {
    let view = MyView()
    // Configure the view...

    // Show indicators for every element.
    SnapshotVerifyAccessibility(view, showActivationPoints: .always)

    // Don't show any indicators.
    SnapshotVerifyAccessibility(view, showActivationPoints: .never)
}

您还可以从 Objective-C 运行辅助功能快照测试:

- (void)testAccessibility;
{
    UIView *view = [UIView new];
    // Configure the view...

    SnapshotVerifyAccessibility(view, @"identifier");
}

要求

贡献

我们热爱我们的 贡献者! 在提交拉取请求之前,请阅读我们的 贡献指南

扩展

您是否编写了自己的扩展? 在此处添加并提交拉取请求!

许可证

Copyright 2020 Square Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.