From 2f529967d93f3ab3d2b7fb5d3046929dfc54d30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 28 Oct 2026 17:23:49 +0100 Subject: [PATCH 1/3] fix(ios): keep the runner session when keyboard return finds nothing focused keyboard enter with nothing focused fell back to app.typeText, which XCTest answers by recording 'Neither element nor any descendant has keyboard focus' as an issue rather than throwing. The exception catcher never saw it, the issue invalidated the runner session, and the command failed as XCTEST_RECORDED_FAILURE. The fallback typing now runs with its issues held back: that refusal reads as a return key that was not pressed, the single text input fallback gets its turn, and every other issue is recorded as before, since the return may have gone out. --- .../RunnerTests+Keyboard.swift | 39 +++++++++++-------- .../RunnerTests+TextInputProbe.swift | 33 ++++++++++++---- .../UnitTests/RunnerTests+KeyboardTests.swift | 35 +++++++++++++++++ 3 files changed, 83 insertions(+), 24 deletions(-) diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift index eb8aad99d6..2f83206137 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift @@ -280,23 +280,14 @@ extension RunnerTests { return (wasVisible: wasVisible, pressed: true, visible: isKeyboardVisible(app: app)) } - var typed = false - let exceptionMessage = RunnerObjCExceptionCatcher.catchException({ - app.typeText(XCUIKeyboardKey.return.rawValue) - typed = true - }) - if let exceptionMessage { - NSLog( - "AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_IGNORED_EXCEPTION=%@", - exceptionMessage - ) + guard typeKeyboardReturn({ app.typeText(XCUIKeyboardKey.return.rawValue) }) else { if let singleTarget = singleTextEntryElement(app: app) { return pressKeyboardReturn(on: singleTarget, app: app, wasVisible: wasVisible) } return (wasVisible: wasVisible, pressed: false, visible: isKeyboardVisible(app: app)) } sleepFor(0.2) - return (wasVisible: wasVisible, pressed: typed, visible: isKeyboardVisible(app: app)) + return (wasVisible: wasVisible, pressed: true, visible: isKeyboardVisible(app: app)) #else return (wasVisible: false, pressed: false, visible: false) #endif @@ -322,15 +313,11 @@ extension RunnerTests { wasVisible: Bool ) -> (wasVisible: Bool, pressed: Bool, visible: Bool) { #if os(iOS) - let exceptionMessage = RunnerObjCExceptionCatcher.catchException({ + let typed = typeKeyboardReturn({ element.tap() element.typeText(XCUIKeyboardKey.return.rawValue) }) - if let exceptionMessage { - NSLog( - "AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_TARGET_IGNORED_EXCEPTION=%@", - exceptionMessage - ) + guard typed else { return (wasVisible: wasVisible, pressed: false, visible: isKeyboardVisible(app: app)) } sleepFor(0.2) @@ -340,6 +327,24 @@ extension RunnerTests { #endif } + /// Types a return, false when nothing was typed: an ObjC exception, or XCTest's refusal to type + /// with nothing focused. XCTest records that refusal as an issue, which would invalidate the runner + /// session; any other issue is recorded as usual, since the return may have gone out. + private func typeKeyboardReturn(_ type: () -> Void) -> Bool { + let (issues, exception) = holdingRecordedIssues(type) + if issues.contains(where: { Self.isNoKeyboardFocusIssue($0.compactDescription) }) { + NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_NO_FOCUS") + return false + } + issues.forEach { record($0) } + if exception { NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_IGNORED_EXCEPTION") } + return !exception + } + + static func isNoKeyboardFocusIssue(_ description: String) -> Bool { + description.contains("Neither element nor any descendant has keyboard focus") + } + private func singleTextEntryElement(app: XCUIApplication) -> XCUIElement? { #if os(iOS) let matches = safely("KEYBOARD_RETURN_TEXT_ENTRY_QUERY", []) { diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift index 78dc4c6708..9e2a24730f 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift @@ -4,6 +4,7 @@ import AgentDeviceSnapshotPresentation final class TextInputProbeIssues { let thread = Thread.current var count = 0 + var held: [XCTIssue] = [] } enum TextInputProbeFailure: String, Error { @@ -117,6 +118,28 @@ extension RunnerTests { fallback: T, _ probe: (_ shouldStop: () -> Bool) -> T ) -> Result { + withIssueScope { issues in + let (value, exception) = catchingObjCException(fallback: fallback) { + probe({ self.hasTextInputProbeIssues(issues) }) + } + if hasTextInputProbeIssues(issues) { return .failure(.recordedIssue) } + if exception != nil { return .failure(.exception) } + return .success(value) + } + } + + /// Runs `body` with its XCTest issues held back instead of recorded, and hands them to the caller, + /// which records the ones it does not explain. + func holdingRecordedIssues(_ body: () -> Void) -> (issues: [XCTIssue], exception: Bool) { + withIssueScope { issues in + let (_, exception) = catchingObjCException(fallback: ()) { body() } + suppressedIssueLock.lock() + defer { suppressedIssueLock.unlock() } + return (issues.held, exception != nil) + } + } + + private func withIssueScope(_ body: (TextInputProbeIssues) -> T) -> T { precondition(Thread.isMainThread) let issues = TextInputProbeIssues() suppressedIssueLock.lock() @@ -128,12 +151,7 @@ extension RunnerTests { textInputProbeIssues = previous suppressedIssueLock.unlock() } - let (value, exception) = catchingObjCException(fallback: fallback) { - probe({ self.hasTextInputProbeIssues(issues) }) - } - if hasTextInputProbeIssues(issues) { return .failure(.recordedIssue) } - if exception != nil { return .failure(.exception) } - return .success(value) + return body(issues) } private func hasTextInputProbeIssues(_ scope: TextInputProbeIssues) -> Bool { @@ -149,8 +167,9 @@ extension RunnerTests { return false } scope.count += 1 + scope.held.append(issue) suppressedIssueLock.unlock() - NSLog("AGENT_DEVICE_RUNNER_TEXT_INPUT_PROBE_UNAVAILABLE issue=%@", issue.compactDescription) + NSLog("AGENT_DEVICE_RUNNER_RECORDED_ISSUE_HELD issue=%@", issue.compactDescription) return true } diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift index ae79686351..a250329a7a 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift @@ -243,5 +243,40 @@ extension RunnerTests { runnerScreenshotStabilitySettled([Data([1])], requiredConsecutiveMatches: 1) ) } + + func testNoKeyboardFocusIssueIsOnlyXCTestsRefusalToTypeUnfocused() { + XCTAssertTrue(Self.isNoKeyboardFocusIssue( + "Failed to synthesize event: Neither element nor any descendant has keyboard focus. Event dispatch snapshot: Application" + )) + XCTAssertFalse(Self.isNoKeyboardFocusIssue("Timed out while evaluating UI query.")) + } + +#if os(iOS) + @MainActor + func testKeyboardReturnWithNothingFocusedFailsWithoutInvalidatingTheRunnerSession() throws { + app.launchArguments = ["--agent-device-text-entry-regression"] + app.launch() + defer { + invalidateCachedTarget(reason: "unit_test_cleanup") + app.terminate() + } + XCTAssertTrue( + app.textFields["agent-device-hardware-keyboard-input"].waitForFixtureExistence(timeout: appExistenceTimeout) + ) + mainOwned.app = app + mainOwned.bundleId = "com.callstack.agentdevice.runner" + mainOwned.processIdentifier = try XCTUnwrap(Self.processIdentifier(of: app)) + let failures = currentXCTestFailureCount() + let command = try runnerCommandFixture( + #"{"appBundleId":"com.callstack.agentdevice.runner","command":"keyboardReturn","commandId":"return-unfocused"}"# + ) + + let response = try execute(command: command) + let recordedFailures = currentXCTestFailureCount() - failures + + XCTAssertEqual(recordedFailures, 0) + XCTAssertEqual(response.error?.code, "UNSUPPORTED_OPERATION") + } +#endif } #endif From da54017da2018b67f70c395e3b1c4642b9022b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 28 Oct 2026 22:58:00 +0100 Subject: [PATCH 2/3] fix(ios): type the single-input fallback's return into the focused field, keep tap issues Review on #4032: record every held issue except the no-focus refusal, and pin both fallback branches with fixtures whose text-input count decides them. The single-input fallback typed into an index-bound element that the raised keyboard re-pointed at its own window, so it never submitted; it now types through the app into whatever the tap focused. --- .../RunnerTests+Keyboard.swift | 22 +++++---- .../RunnerTests+TextInputProbe.swift | 6 +-- .../UnitTests/RunnerTests+KeyboardTests.swift | 48 ++++++++++++++----- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift index 2f83206137..05f3b5b00d 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Keyboard.swift @@ -313,9 +313,11 @@ extension RunnerTests { wasVisible: Bool ) -> (wasVisible: Bool, pressed: Bool, visible: Bool) { #if os(iOS) + // `element` is bound by index, and the keyboard the tap raises shifts those indices, so the + // return goes to whatever the tap focused rather than to a re-resolved `element`. let typed = typeKeyboardReturn({ element.tap() - element.typeText(XCUIKeyboardKey.return.rawValue) + app.typeText(XCUIKeyboardKey.return.rawValue) }) guard typed else { return (wasVisible: wasVisible, pressed: false, visible: isKeyboardVisible(app: app)) @@ -329,18 +331,20 @@ extension RunnerTests { /// Types a return, false when nothing was typed: an ObjC exception, or XCTest's refusal to type /// with nothing focused. XCTest records that refusal as an issue, which would invalidate the runner - /// session; any other issue is recorded as usual, since the return may have gone out. + /// session, so it alone is dropped; every other issue is recorded as usual. private func typeKeyboardReturn(_ type: () -> Void) -> Bool { - let (issues, exception) = holdingRecordedIssues(type) - if issues.contains(where: { Self.isNoKeyboardFocusIssue($0.compactDescription) }) { - NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_NO_FOCUS") - return false + let (issues, exceptionMessage) = holdingRecordedIssues(type) + let noFocus = issues.filter { Self.isNoKeyboardFocusIssue($0.compactDescription) } + issues.filter { !Self.isNoKeyboardFocusIssue($0.compactDescription) }.forEach { record($0) } + if !noFocus.isEmpty { NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_NO_FOCUS") } + if let exceptionMessage { + NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_IGNORED_EXCEPTION=%@", exceptionMessage) } - issues.forEach { record($0) } - if exception { NSLog("AGENT_DEVICE_RUNNER_KEYBOARD_RETURN_IGNORED_EXCEPTION") } - return !exception + return noFocus.isEmpty && exceptionMessage == nil } + /// XCTest names this refusal only in its English issue text, as it does the AX failures that + /// `isSuppressedAxSnapshotIssueDescription` matches. static func isNoKeyboardFocusIssue(_ description: String) -> Bool { description.contains("Neither element nor any descendant has keyboard focus") } diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift index 9e2a24730f..82b0bf558c 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift @@ -130,12 +130,12 @@ extension RunnerTests { /// Runs `body` with its XCTest issues held back instead of recorded, and hands them to the caller, /// which records the ones it does not explain. - func holdingRecordedIssues(_ body: () -> Void) -> (issues: [XCTIssue], exception: Bool) { + func holdingRecordedIssues(_ body: () -> Void) -> (issues: [XCTIssue], exceptionMessage: String?) { withIssueScope { issues in - let (_, exception) = catchingObjCException(fallback: ()) { body() } + let (_, exceptionMessage) = catchingObjCException(fallback: ()) { body() } suppressedIssueLock.lock() defer { suppressedIssueLock.unlock() } - return (issues.held, exception != nil) + return (issues.held, exceptionMessage) } } diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift index a250329a7a..2e2d7d6d71 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift @@ -253,16 +253,40 @@ extension RunnerTests { #if os(iOS) @MainActor - func testKeyboardReturnWithNothingFocusedFailsWithoutInvalidatingTheRunnerSession() throws { - app.launchArguments = ["--agent-device-text-entry-regression"] + func testKeyboardReturnWithNothingFocusedSubmitsTheSoleTextInput() throws { + app.launchArguments = [ + "--agent-device-text-entry-regression", + "--agent-device-text-entry-soft-keyboard", + "--agent-device-text-entry-submit-clears", + "--agent-device-text-entry-long-prefill", + ] + let (response, recordedFailures) = try keyboardReturnOnLaunchedFixture() + let field = app.textFields["agent-device-hardware-keyboard-input"] + + XCTAssertEqual(textEntryInputCount(), 1) + XCTAssertEqual(recordedFailures, 0) + XCTAssertTrue(response.ok, String(describing: response.error)) + XCTAssertEqual(field.value as? String, "", "the field's submit clears it") + } + + @MainActor + func testKeyboardReturnWithNothingFocusedAndNoTextInputFailsWithoutInvalidatingTheRunnerSession() throws { + app.launchArguments = [] + let (response, recordedFailures) = try keyboardReturnOnLaunchedFixture() + + XCTAssertEqual(textEntryInputCount(), 0) + XCTAssertEqual(recordedFailures, 0) + XCTAssertEqual(response.error?.code, "UNSUPPORTED_OPERATION") + } + + @MainActor + private func keyboardReturnOnLaunchedFixture() throws -> (response: Response, recordedFailures: Int) { app.launch() - defer { - invalidateCachedTarget(reason: "unit_test_cleanup") - app.terminate() + addTeardownBlock { @MainActor in + self.invalidateCachedTarget(reason: "unit_test_cleanup") + self.app.terminate() } - XCTAssertTrue( - app.textFields["agent-device-hardware-keyboard-input"].waitForFixtureExistence(timeout: appExistenceTimeout) - ) + XCTAssertTrue(app.wait(for: .runningForeground, timeout: appExistenceTimeout)) mainOwned.app = app mainOwned.bundleId = "com.callstack.agentdevice.runner" mainOwned.processIdentifier = try XCTUnwrap(Self.processIdentifier(of: app)) @@ -270,12 +294,12 @@ extension RunnerTests { let command = try runnerCommandFixture( #"{"appBundleId":"com.callstack.agentdevice.runner","command":"keyboardReturn","commandId":"return-unfocused"}"# ) - let response = try execute(command: command) - let recordedFailures = currentXCTestFailureCount() - failures + return (response, currentXCTestFailureCount() - failures) + } - XCTAssertEqual(recordedFailures, 0) - XCTAssertEqual(response.error?.code, "UNSUPPORTED_OPERATION") + private func textEntryInputCount() -> Int { + app.textFields.count + app.secureTextFields.count + app.searchFields.count + app.textViews.count } #endif } From 0e74498663b500107439d2bd69207ed43ff6898a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 29 Oct 2026 14:05:32 +0100 Subject: [PATCH 3/3] test(ios): wait for the unfocused return fixture --- .../UnitTests/RunnerTests+KeyboardTests.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift index 2e2d7d6d71..7139ce0704 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift @@ -260,7 +260,9 @@ extension RunnerTests { "--agent-device-text-entry-submit-clears", "--agent-device-text-entry-long-prefill", ] - let (response, recordedFailures) = try keyboardReturnOnLaunchedFixture() + let (response, recordedFailures) = try keyboardReturnOnLaunchedFixture( + waitingForTextInput: "agent-device-hardware-keyboard-input" + ) let field = app.textFields["agent-device-hardware-keyboard-input"] XCTAssertEqual(textEntryInputCount(), 1) @@ -280,13 +282,18 @@ extension RunnerTests { } @MainActor - private func keyboardReturnOnLaunchedFixture() throws -> (response: Response, recordedFailures: Int) { + private func keyboardReturnOnLaunchedFixture( + waitingForTextInput identifier: String? = nil + ) throws -> (response: Response, recordedFailures: Int) { app.launch() addTeardownBlock { @MainActor in self.invalidateCachedTarget(reason: "unit_test_cleanup") self.app.terminate() } XCTAssertTrue(app.wait(for: .runningForeground, timeout: appExistenceTimeout)) + if let identifier { + XCTAssertTrue(app.textFields[identifier].waitForFixtureExistence(timeout: appExistenceTimeout)) + } mainOwned.app = app mainOwned.bundleId = "com.callstack.agentdevice.runner" mainOwned.processIdentifier = try XCTUnwrap(Self.processIdentifier(of: app))