BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Google Open Sources Accessibility Test Framework for iOS

Google Open Sources Accessibility Test Framework for iOS

This item in japanese

Bookmarks

Google GTXiLib, an accessibility test automation framework for iOS, is now open source under the Apache License. GTXiLib is written in Objective-C and integrates with Xcode.

The way GTXiLib integrates with Xcode is through the XCTest unit testing framework. You can install GTXiLib for any of your test classes and register a number of accessibility checks you would like to be performed together with your unit tests. If any accessibility check fails, the corresponding unit test will also fail.

The accessibility checks that GTXiLib currently supports aim to ensure that labels are present on buttons, that labels are not punctuated, that tappable areas have a minimum area, and text contrast is high enough.

GTXiLib also provides the means for developers to build their own custom checks. This is possible by using the checkWithName:block: API. The following is a simplified version of an accessibility check to ensure that a label is present on an element:

  id<GTXChecking> check = [GTXCheckBlock GTXCheckWithName:"LabelMustBePresent"
                                   block:^BOOL(id element, GTXErrorRefType errorOrNil) {

    NSError *error;
    id accessibilityLabel = [element accessibilityLabel];
    if (![accessibilityLabel isKindOfClass:[NSString class]]) {
        *errorOrNil = // set error;
      }
      // Fail
      return NO;
    }
    return NO;
  }];

As it turns out, low-level accessibility checks are made possible by Apple’s own UIAccessibility framework, as exemplified by the use of the accessibilityLabel property in the above example. GTXiLib tests are not limited to using only UIAccessibility methods, though, and may use any available framework for their inner workings.

To install GTXiLib for one of your test classes, just add the following line of code to its +setup method:

+ (void)setUp {
  [super setUp];

  // ... your other setup code (if any) comes here.

  // Install GTX on all tests in *this* test class.
  [GTXiLib installOnTestSuite:[GTXTestSuite suiteWithAllTestsInClass:self]
                       checks:[GTXChecksCollection allGTXChecks]
            elementBlacklists:@[]];
}

By doing this, all accessibility checks will be run for each of the unit tests defined in that test class. If you want to exclude a number of UI elements from accessibility checks, you can list them in the elementBlackLists array. This is a useful resource when you are trying to retrofit accessibility tests to an existing codebase, so you can exclude elements that you cannot control or those that will be fixed at some later point in time.

Being based on XCTest makes GTXiLib compatible with any XCTest-based framework, such as Google’s own UI Automation test framework EarlGrey.

GTXiLib can be easily added to a project using cocoapods.

Rate this Article

Adoption
Style

BT