Swiftpack.co -  rwapp/A11yUITests as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
rwapp/A11yUITests
Accessibility tests for XCUI Testing.
.package(url: "https://github.com/rwapp/A11yUITests.git", from: "0.4.1")

A11yUITests

Version License Platform

A11yTests is an extension to XCTestCase that adds tests for common accessibility issues that can be run as part of an XCUI Test suite.

Tests can either be run separately or integrated into existing XCUI Tests.

Using These Tests

Good accessibility is not about ticking boxes and conforming to regulations and guidelines, but about how your app is experienced. You will only ever know if your app is actually accessible by letting real people use it. Consider these tests as hints for where you might be able to do better, and use them to detect regressions.

Failures for these tests should be seen as warnings for further investigation, not strict failures. As such i'd recommend always having continueAfterFailure = true set.

add import A11yUITests to the top of your test file.

Running tests

Tests can be run individually or in suites.

Running All Tests on All Elements

func test_allTests() {
    XCUIApplication().launch()
    a11yCheckAllOnScreen()
}

Specifying Tests/Elements

To specify elements and tests use a11y(tests: [A11yTests], on elements: [XCUIElement]) passing an array of tests to run and an array of elements to run them on. To run all interactive element tests on all buttons:

func test_buttons() {
    let buttons = XCUIApplication().buttons.allElementsBoundByIndex
    a11y(tests: a11yTestSuiteInteractive, on: buttons)
}

To run a single test on a single element pass arrays with the test and element. To check if a button has a valid accessibility label:

func test_individualTest_individualButton() {
    let button = XCUIApplication().buttons["My Button"]
    a11y(tests: [.buttonLabel], on: [button])
}

Test Suites

A11yUITests contains 4 pre-built test suites with tests suitable for different elements.

a11yTestSuiteAll Runs all tests.

a11yTestSuiteImages Runs tests suitable for images.

a11yTestSuiteInteractive runs tests suitable for interactive elements.

a11yTestSuiteLabels runs tests suitable for static text elements.

Alternatively you can create an array of A11yTests enum values for the tests you want to run.

Tests

Minimum Size

minimumSize or checks an element is at least 18px x 18px.

Note: 18px is arbitrary.

Minimum Interactive Size

minimumInteractiveSize checks tappable elements are a minimum of 44px x 44px. This satisfies WCAG 2.1 Success Criteria 2.5.5 Target Size Level AAA

Note: Many of Apple's controls fail this requirement. For this reason, when running a suite of tests with minimumInteractiveSize only buttons and cells are checked. This may still result in some failures for UITabBarButtons for example. For full compliance, you should run a11yCheckValidSizeFor(interactiveElement: XCUIElement) on any element that your user might interact with, eg. sliders, steppers, switches, segmented controls. But you will need to make your own subclass as Apple's are not strictly adherent to WCAG.

Label Presence

labelPresence checks the element has an accessibility label that is a minimum of 2 characters long. Pass a minMeaningfulLength argument to a11yCheckValidLabelFor(element: XCUIElement, minMeaningfulLength: Int ) to change the minimum length. This counts towards WCAG 2.1 Guideline 1.1 Text Alternatives but does not guarantee compliance.

Button Label

buttonLabel checks labels for interactive elements begin with a capital letter and don't contain a period or the word button. Checks the label is a minimum of 2 characters long. Pass a minMeaningfulLength argument to a11yCheckValidLabelFor(interactiveElement: XCUIElement, minMeaningfulLength: Int ) to change the minimum length. This follows Apple's guidance for writing accessibility labels.

Note: This test is not localised.

Image Label

imageLabel checks accessible images don't contain the words image, picture, graphic, or icon, and checks that the label isn't reusing the image filename. Checks the label is a minimum of 2 characters long. Pass a minMeaningfulLength argument to a11yCheckValidLabelFor(image: XCUIElement, minMeaningfulLength: Int ) to change the minimum length. This follows Apple's guidelines for writing accessibility labels. Care should be given when deciding whether to make images accessible to avoid creating unnecessary noise.

Note: This test is not localised.

Label Length

labelLength checks accessibility labels are <= 40 characters. This follows Apple's guidelines for writing accessibility labels. Ideally, labels should be as short as possible while retaining meaning. If you feel your element needs more context consider adding an accessibility hint.

Header

header checks the screen has at least one text element with a header trait. Headers are used by VoiceOver users to orientate and quickly navigate content.

Button Trait

buttonTrait checks that a button element has the Button or Link trait applied.

Image Trait

imageTrait checks that an image element has the Image trait applied.

Disabled Elements

disabled checks that elements aren't disabled. Disabled elements can be confusing if it is not clear why the element is disabled. Ideally keep the element enabled and clearly message if your app is not ready to process the action.

Duplicated Labels

duplicated checks all elements provided for duplication of accessibility labels. Duplicated accessibility labels are not an accessibility failure - but can make your screen confusing to navigate with VoiceOver, and make Voice Control fail. Ideally you should avoid duplication if possible.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

A11yUITests_ExampleUITests.swift contains example tests that show a fail for each test above.

Requirements

iOS 11

Swift 5

Installation

A11yUITests is available through CocoaPods. To install add the pod to your target's test target in your podfile. eg

target 'My_Application' do
    target 'My_Application_UITests' do
    pod 'A11yUITests'
    end
end

Note

  • This library accesses a private property in the iOS SDK, so care should be taken when adding it to your project to ensure you are not shipping this code. If you submit this code to app review you will likely receive a rejection from Apple.
  • This library uses method swizzling of the value(forUndefinedKey:) method on NSObject to guard against potential crashes if Apple changes their private API in future. Any calls to this function will return nil after running any tests which access accessibility traits. This affects your test suite only, not your app.

Known Issues

If two elements of the same type have the same identifier this will cause the tests to crash on iOS 13+. eg, two buttons both labeled 'Next'.

Author

Rob Whitaker, rw@rwapp.co.uk
https://mobilea11y.com

License

A11yUITests is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 89
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API