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.