Unexpected behaviour of hardware keyboard focus in UITests

Hello!

I was faced with unexpected behavior of hardware keyboard focus in UITests.

A clear description of the problem

When running UITests on the iOS Simulator with both "Full Keyboard Access" and "Connect Hardware Keyboard" options enabled, there is a noticeable delay between keyboard actions for focus managing (like pressing Tab or arrow keys). The delay seems to increase with repeated input and suggests that events are being queued instead of processed immediately. I will describe why I have such an assumption later.

A step-by-step set of instructions to reproduce the problem

  1. Launch the iOS Simulator.
  2. Enable both "Full Keyboard Access" and "Connect Hardware Keyboard" in the Simulator settings.
  3. Run a UITest on a target application (ideally an endless or long-running test).
  4. Once the app is launched, press the Tab key several times.
  5. Observe the delay in focus movement.
  6. Optionally, press the Tab or arrow keys rapidly, then stop the UITest.
  7. After stopping, you’ll see a burst of rapid focus changes.

What results you expected

We expected keyboard actions (like Tab) to be handled immediately and the UI focus to update smoothly during UITests.

What results you saw

There was a 4–10 (end more) second delay between pressing keys and seeing a response. All stacked keyboard events (used for managing focus) are performed all at once after stopping the UITest.

The version of Xcode you are using

  • Xcode: Version 16.3 (16E140)
  • Simulator: iPhone 16 Pro (iOS 18.4 and 18.1)
  • Simulator: iPad Pro 11-inch (M4) (iPadOS 17.5)

Hey Anton.Ye, I faced something similar and found a reliable fix. The issue happens because “Full Keyboard Access” on macOS and “Connect Hardware Keyboard” in the iOS Simulator conflict with Xcode’s UI test system. They cause key events (like Tab or arrow keys) to queue up and only fire after the test ends. Here’s how you can fix it:

  1. Turn off Full Keyboard Access on macOS:

Go to System Settings > Accessibility > Keyboard > Full Keyboard Access, and turn it off.

  1. Turn off Hardware Keyboard in the iOS Simulator:

In the Simulator menu bar, go to I/O > Keyboard and uncheck “Connect Hardware Keyboard”.

  1. Use the Software Keyboard instead (mimics real iOS device behavior and avoids delayed input).

Also, in your UI test code, make sure to force focus before typing to avoid weird delays:

let textField = app.textFields["YourTextFieldIdentifier"]
XCTAssertTrue(textField.waitForExistence(timeout: 5))
textField.tap()
textField.typeText("Sample input")

If the keyboard or focus is still buggy:

XCUIDevice.shared.press(.keyboardDismiss)
textField.tap()
textField.typeText("Sample input")

This solved the delay and the “burst of focus changes” for me. Hope it helps!

Unexpected behaviour of hardware keyboard focus in UITests
 
 
Q