3

I am having trouble automating tapping on an UIButton that is embedded inside a UITableViewCell if that cell is the table's only one. This is in the context of UI automation with KIF.

Here my relavant call:

[tester tapViewWithAccessibilityLabel: @"LABEL"
                               traits: UIAccessibilityTraitButton];
                               // trait only specified for 2nd case below

Here is what I am observing:

  • If I put the accessibility label on the UITableViewCell KIF's KIFUITestActor - waitForAccessibilityElement:view:withLabel:traits:tappable: returns the UITableView, not the cell. Somehow the table seems to inherit its only child's accessibility label and lets KIF encounter it first during its recursive search.

  • If I put the accessibility label on the UIButton instead, KIF finds it but determines that is is not tappable (i.e. UIView-KIFAdditions -tappablePointInRect: returns NO), presumably because its mainly transparent between the thin font lines for the button's label (the tap goes to a UITableViewCellContentView instead).

A workaround might be tapping on the row by it's NSIndexPath but maybe there is still a better way to overcome the described hurdles I am facing. So how could I instruct KIF to tap a button like this with a call to tapView...?

Drux
  • 11,992
  • 13
  • 66
  • 116

3 Answers3

0

If you are making cell in code, make sure your button is added to cell.contentView.

If you are loading cell from xib, try to send contentView to back of view hierarchy (I have not found any downsides yet for doing it) with:

[self sendSubviewToBack:self.contentView];
jki
  • 4,617
  • 1
  • 34
  • 29
-1

This is just to confirm that this workaround is applicable:

[tester tapRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0] 
inTableViewWithAccessibilityIdentifier: @"IDENTIFIER"];
Drux
  • 11,992
  • 13
  • 66
  • 116
  • 2
    I don't think this qualifies as an answer because you're not tapping THE button inside the cell, just the cell itself and that was not the question. – user3099609 Jul 29 '15 at 13:42
-1

The "good" solution should be something like this:

UITableViewCell *cell = [tester waitForCellAtIndexPath:indexPath inTableViewWithAccessibilityIdentifier:tableAccessibilityIdentifier];
UIAccessibilityElement *element = [cell accessibilityElementWithLabel:accessibilityLabel traits:UIAccessibilityTraitButton];
[tester tapAccessibilityElement:element inView:cell];

Unfortunately it doesn't work as expected. Sometimes the element is a UIAccessibilityElementMockView and tapping it in step 3 leads means just tapping the underlying cell. Naturally the results will not be as expected.

I've managed to finally work around that by doing something more along the lines of:

MyCustomCellClass *cell = (MyCustomCellClass *)[self waitForCellAtIndexPath:indexPath inTableViewWithAccessibilityIdentifier:tableAccessibilityIdentifier];
[cell.buttonOutlet sendActionsForControlEvents:UIControlEventTouchUpInside];

Note that this means

  1. relying less on UI interaction
  2. having a custom class
  3. having an outlet to the button

But hey, KIF isn't supported by Apple so it sometimes, you know, breaks :)

user3099609
  • 2,318
  • 18
  • 20