Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Jun 11, 2019 12:34

By Dan Maor

Problem

Sometimes, you may encounter scenarios in which the desired objects you require for automating your application are not being exposed. More often than not, the issue lies with accessibility information not being correctly defined in the app’s code.

It should be noted that this issue does not stem from a Perfecto bug as it will affect other use cases, such as:

  • Automating with other automation tools which are based on Apple’s automation frameworks (XCTest and UIAutomation).
  • Turning on VoiceOver on the device and attempting to interact with the objects).
  • Using XCode’s Accessibility Inspector.

Solution

See the following Sample XCode project (contains compiled samples as well):

https://github.com/PerfectoCode/iosAccessibilitySample

The project is comprised of a single ViewController, containing a UICollectionView which is being populated by 30 custom UICollectionViewCell (called PerfectoCollectionViewCell) objects on runtime.

You may attempt to build the project and install it on a device, or use the compiled sample called “AccessibilitySampleDisabled.ipa”. After launching the app, open the Object Spy wizard; as you can see, while the cell labels are being recognized, the cell image is not.

 


A proposed solution for the issue with this app: in the ViewController implementation file (ViewController.m), observe the cellForItemAtIndexPath method which contains a possible solution for exposing the missing image objects by explicitly enabling accessibility for it and assigning it with an accessibility label. See the method below:

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"perCell";
    PerfectoCollectionViewCell *cell = (PerfectoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.cellTitle setText:[self.dataArray objectAtIndex:indexPath.row]];
 
  // The following section is responsible for exposing the Image Object to Automation
  // Without it the object will not be exposed

    UIImage * cellImage = (UIImage *)[cell viewWithTag:99];
	[cellImage setIsAccessibilityElement:YES];
 
  // To allow the usage of a simple XPath Expression the Image must have an accessibility label set.
  // Without it the object will either remain hidden or may not have a direct and simple XPath expression.
    [cellImage setAccessibilityLabel:[NSString stringWithFormat:@"Image #%ld", (long) indexPath.row]];

    return cell;
}


These lines are commented out in the XCode project; you may un-comment them, re-build and install the app or use the attached compiled sample called “AccessibilitySampleEnabled.ipa”. After launching the app, open the Object Spy wizard; as you can see, while the cell labels are now being properly recognized.

 

More Information

The following articles in Apple’s Documentation portal elaborate on the uses and implementation of accessibility in your application:

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Accessibility_on_iPhone/Accessibility_on_iPhone.html#//apple_ref/doc/uid/TP40008785-CH100-SW1

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW5