iOS UI Testing Accessibility Identifier


Example

When Accessibility enabled in Utilities

  • Select storyboard.
  • Expand the Utilities
  • Select Identity Inspector
  • Select your element on storyboard
  • Add new Accessibility Identifier (in example addButton)

enter image description here

When Accessibility disabled in Utilities

  • Select storyboard.
  • Expand the Utilities
  • Select Identity Inspector
  • Select your element on storyboard
  • Add attribute in User Defined Runtime Attributes
  • For Key Path type - accessibilityIdentifier
  • For Type - `String
  • For Value - new accessibility identifier for your element (in example view)

enter image description here

Setting up in UITest file

import XCTest

class StackOverFlowUITests: XCTestCase {

    private let app = XCUIApplication()

    //Views

    private var view: XCUIElement!

    //Buttons

    private var addButton: XCUIElement!


    override func setUp() {
        super.setUp()
    
        app.launch()
    
        //Views
    
        view = app.otherElements["view"]
    
        //Buttons
    
        addButton = app.buttons["addButton"]
    }

    func testMyApp() {

        addButton.tap()
        view.tap()
    }    
}

In [ ] add Accessibility Identifier for element.

UIView, UIImageView, UIScrollView

let imageView = app.images["imageView"]
let scrollView = app.scrollViews["scrollView"]
let view = app.otherElements["view"]

UILabel

let label = app.staticTexts["label"]

UIStackView

let stackView = app.otherElements["stackView"]

UITableView

let tableView = app.tables["tableView"]

UITableViewCell

let tableViewCell = tableView.cells["tableViewCell"]

UITableViewCell elements

let tableViewCellButton = tableView.cells.element(boundBy: 0).buttons["button"]

UICollectionView

let collectionView = app.collectionViews["collectionView"]

UIButton, UIBarButtonItem

let button = app.buttons["button"]
let barButtonItem = app.buttons["barButtonItem"]

UITextField

  • normal UITextField
let textField = app.textFields["textField"]
  • password UITextField
let passwordTextField = app.secureTextFields["passwordTextField"]

UITextView

let textView = app.textViews["textView"]

UISwitch

let switch = app.switches["switch"]

Alerts

let alert = app.alerts["About yourself"] // Title of presented alert