title: iOS UI Test: Wait for exist fails for dynamically added button
body: <p>My application does a SDK auto update and initialization when it launches. When it completed I am adding a button to check if auto update and initialization are completed in my UI test:</p>
  
  <pre><code>let hiddenButton = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
  hiddenButton.setTitle("databaseUpdated", for: .normal)
  hiddenButton.accessibilityIdentifier = "databaseUpdated"
  self.view.addSubview(hiddenButton)
  </code></pre>
  
  <p>I have tried following lines to check it but it always fails:</p>
  
  <pre><code>XCTAssertTrue(app.descendants(matching: .button)["databaseUpdated"].waitForExistence(timeout: 60))
  XCTAssertTrue(app.children(matching: .button)["databaseUpdated"].waitForExistence(timeout: 60))
  XCTAssertTrue(app.otherElements["databaseUpdated"].waitForExistence(timeout: 60))
  XCTAssertTrue(app.buttons["databaseUpdated"].waitForExistence(timeout: 60))
  </code></pre>
  
  <p>I have confirmed that my button is in view hierarchy using <code>Debug View Hierarchy</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/mT3yM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mT3yM.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have decided to add the button in <code>viewDidLoad</code> with different identifier and update it when SDK is initialized:</p>
  
  <pre><code>private let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
  
  override public func viewDidLoad() {
      super.viewDidLoad()
      button.isAccessibilityElement = true
      button.accessibilityTraits = .button
      button.accessibilityIdentifier = "initial"
      view.addSubview(button)
  }
  </code></pre>
  
  <p>when SDK is initialized it is set to:</p>
  
  <pre><code>button.accessibilityIdentifier = "databaseUpdated"
  </code></pre>
  
  <p>and here is what I see when I inspect the button in view debugger:</p>
  
  <p><a href="https://i.stack.imgur.com/rNdGa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rNdGa.png" alt="accessibility attributes"></a></p>
  
  <p>still test is failing to find it.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I am able to find the button using Accessibility Inspector:</p>
  
  <p><a href="https://i.stack.imgur.com/iREeE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iREeE.png" alt="inspector"></a></p>