0

I've created UIBarButtonItem with customView set to BadgeButton (a UIButton that has badge label added).

I am setting accessibilityIdentifier = "properIDHere" and isAccessibilityElement = true for both: aBadgeButton and aBadgeButton.badgeLabel

When running UITests the badge button is visible and accessible by the ID, but the badgeLabel is not. Do you have ideas why?

final class BadgeButton: MyBaseButton {
   let badgeLabel = UILabel()

   override func addSubviews() {
    addSubview(badgeLabel)
   }

   override func setupConstraints() {
    badgeLabel.snp.makeConstraints { make in
        make.width.height.equalTo(badgeSize)
        make.centerX.equalToSuperview().offset(badgeXOffset)
        make.centerY.equalToSuperview().offset(badgeYOffset)
    }
}
Andriy
  • 2,767
  • 2
  • 21
  • 29
izik461
  • 1,131
  • 12
  • 32

2 Answers2

0

The runTime lable need a frame or constraint.

final class BadgeButton: UIButton {
        let badgeLabel = UILabel()
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupView()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupView()
        }

        func setupView() {
            self.addSubview(badgeLabel)
            badgeLabel.frame = CGRect(x: self.frame.width / 2, y: 0, width: 20, height: 20)
            //or you setup runTime Constraint
        }
    }
Pradip Patel
  • 304
  • 1
  • 8
  • Thanks for the suggestion, adding the badge as subview and layouting it seemed obvious to me. Badge is normally visible, but not through the XCTest accessibility inspector – izik461 Jan 24 '19 at 12:51
0

On the BadgeButton parent/custom view, try disabling accessibility. Leave it enabled for the UILabel.

After this, back in your XCTestCase, print out the XCUIApplication().debugDescription to see if you're now able to see the UILabel's accessibility information in the app's element hierarchy.

See this post as a possible duplicate: Xcode UI Tests can't find views that are added programatically

enter image description here

Blaine
  • 185
  • 1
  • 7