@@ -355,27 +355,95 @@ extension RunnerTests {
355355 }
356356 }
357357
358- /// Clears `input`, which must have been read with nothing dispatched since.
358+ /// Clears `input`, which must have been read with nothing dispatched since. On iOS a readable
359+ /// value is selected whole and deleted, wherever the caret is. Otherwise, or when text is left,
360+ /// delete bursts run from the caret, and a value longer than one burst is read again after each
361+ /// burst until it reads empty, stops shortening, or has had at least one burst more than it needs.
359362 func clearTextInput ( app : XCUIApplication , _ input : ResolvedTextEntryInput ) {
360- // Skip the clear (delete burst + moveCaretToEnd edge-tap ) ONLY when we can confirm the
363+ // Skip the clear (edge-tap, select all, delete bursts ) ONLY when we can confirm the
361364 // field is empty. Why skip: the edge-tap computes a point from the element frame, which can
362365 // be stale after the field repositions on focus (e.g. the Settings search bar jumps
363366 // bottom->top and reveals a "Suggestions" list) — tapping there navigates away instead of
364367 // clearing; and replacing into an already-empty field is a no-op anyway.
365368 // editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
366369 // emptiness — those must still be cleared, or replace would concatenate stale + new text.
367370 // So distinguish nil (clear) from "" (skip).
368- if let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true ) ,
369- existing . isEmpty {
371+ let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true )
372+ if existing ? . isEmpty == true {
370373 return
371374 }
372375 // Read before the caret tap, which dispatches to the app.
373- let count = estimatedDeleteCount ( for : input . attributes )
376+ var valueLength = normalizedElementText ( input . attributes . value ) . count
377+ // The tap also focuses the field, which a clear can reach with focus unconfirmed.
374378#if !os( tvOS )
375379 moveCaretToEnd ( app : app , input : input )
376380#endif
377- let deletes = String ( repeating : XCUIKeyboardKey . delete . rawValue , count : count )
378- input . element . typeText ( deletes )
381+ #if os( iOS )
382+ // The tap aims at the end of what the field shows, which a long value has scrolled past, so
383+ // bursts from there reach only what is before the caret (#3851). Select all reaches all of it, but
384+ // only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
385+ // bursts alone.
386+ if existing != nil {
387+ var passes = 0
388+ let outcome = Self . clearForSynthesizedReplacement (
389+ clearOnce : {
390+ passes += 1
391+ input . element . typeKey ( " a " , modifierFlags : . command )
392+ input . element . typeText ( XCUIKeyboardKey . delete . rawValue )
393+ return . posted
394+ } ,
395+ read : {
396+ textValueRead (
397+ for : ResolvedTextEntryInput ( reading : input . element ) ? . attributes ,
398+ treatingPlaceholderAsEmpty : true
399+ )
400+ }
401+ )
402+ NSLog ( " AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@ " , passes , String ( describing : outcome ) )
403+ // Text left after every pass goes to the bursts; a field that stopped resolving is not typed into.
404+ guard outcome == . notCleared ,
405+ let remaining = ResolvedTextEntryInput ( reading : input . element )
406+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } )
407+ else {
408+ return
409+ }
410+ valueLength = remaining . count
411+ }
412+ #endif
413+ for _ in 0 ... ( valueLength / Self . maxTextClearBurst + 1 ) {
414+ let burst = Self . textClearBurst ( valueLength : valueLength )
415+ input . element . typeText ( String ( repeating : XCUIKeyboardKey . delete . rawValue , count : burst ) )
416+ // A burst that covers the value needs no read.
417+ guard burst < valueLength + Self . textClearSlack ,
418+ let remaining = ResolvedTextEntryInput ( reading : input . element )
419+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } ) ,
420+ Self . textClearContinues ( clearedLength : valueLength , remainingLength : remaining . count )
421+ else {
422+ return
423+ }
424+ valueLength = remaining . count
425+ }
426+ }
427+
428+ /// Extra deletes past a value's length, for whitespace its normalized read trimmed.
429+ private static let textClearSlack = 8
430+
431+ /// The most deletes one clear burst sends. A longer value takes more bursts.
432+ private static let maxTextClearBurst = 120
433+
434+ /// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
435+ /// that reads empty but may not be.
436+ static func textClearBurst ( valueLength : Int ) -> Int {
437+ let base = valueLength == 0 ? 24 : valueLength + textClearSlack
438+ return max ( 24 , min ( maxTextClearBurst , base ) )
439+ }
440+
441+ /// Whether a clear sends another burst after one sized from `clearedLength` characters left
442+ /// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
443+ /// rewrites its value ends the clear instead of looping; the fill's verification reports what
444+ /// remains.
445+ static func textClearContinues ( clearedLength : Int , remainingLength : Int ) -> Bool {
446+ remainingLength > 0 && remainingLength < clearedLength
379447 }
380448
381449 func isTextEntryElement ( _ element : XCUIElement ) -> Bool {
@@ -529,12 +597,6 @@ extension RunnerTests {
529597#endif
530598 }
531599
532- private func estimatedDeleteCount ( for element : any XCUIElementAttributes ) -> Int {
533- let valueText = normalizedElementText ( element . value )
534- let base = valueText . isEmpty ? 24 : ( valueText . count + 8 )
535- return max ( 24 , min ( 120 , base ) )
536- }
537-
538600 private func normalizedElementText ( _ value : Any ? ) -> String {
539601 String ( describing : value ?? " " )
540602 . trimmingCharacters ( in : . whitespacesAndNewlines )
0 commit comments