@@ -326,7 +326,10 @@ extension RunnerTests {
326326 }
327327 }
328328
329- /// Clears `input`, which must have been read with nothing dispatched since.
329+ /// Clears `input`, which must have been read with nothing dispatched since. On iOS a readable
330+ /// value is selected whole and deleted, wherever the caret is. Otherwise, or when text is left,
331+ /// delete bursts run from the caret, and a value longer than one burst is read again after each
332+ /// burst until it reads empty, stops shortening, or has had at least one burst more than it needs.
330333 func clearTextInput ( app : XCUIApplication , _ input : ResolvedTextEntryInput ) {
331334 // Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
332335 // field is empty. Why skip: the edge-tap computes a point from the element frame, which can
@@ -336,17 +339,82 @@ extension RunnerTests {
336339 // editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
337340 // emptiness — those must still be cleared, or replace would concatenate stale + new text.
338341 // So distinguish nil (clear) from "" (skip).
339- if let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true ) ,
340- existing . isEmpty {
342+ let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true )
343+ if existing ? . isEmpty == true {
341344 return
342345 }
343346 // Read before the caret tap, which dispatches to the app.
344- let count = estimatedDeleteCount ( for : input . attributes )
347+ var valueLength = normalizedElementText ( input . attributes . value ) . count
348+ // The tap also focuses the field: a clear of an input found under a point arrives unfocused.
345349#if !os( tvOS )
346350 moveCaretToEnd ( app : app , input : input )
347351#endif
348- let deletes = String ( repeating : XCUIKeyboardKey . delete . rawValue , count : count )
349- input . element . typeText ( deletes )
352+ #if os( iOS )
353+ // The tap aims at the end of what the field shows, which a long value has scrolled past, so
354+ // bursts from there leave the start of the value (#3851). Select all reaches all of it, but
355+ // only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
356+ // bursts alone.
357+ if existing != nil {
358+ var passes = 0
359+ let outcome = Self . clearForSynthesizedReplacement (
360+ clearOnce : {
361+ passes += 1
362+ input . element . typeKey ( " a " , modifierFlags : . command )
363+ input . element . typeText ( XCUIKeyboardKey . delete . rawValue )
364+ return . posted
365+ } ,
366+ read : {
367+ textValueRead (
368+ for : ResolvedTextEntryInput ( reading : input . element ) ? . attributes ,
369+ treatingPlaceholderAsEmpty : true
370+ )
371+ }
372+ )
373+ NSLog ( " AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@ " , passes , String ( describing : outcome ) )
374+ // Text left after every pass goes to the bursts; a field that stopped resolving is not typed into.
375+ guard outcome == . notCleared ,
376+ let remaining = ResolvedTextEntryInput ( reading : input . element )
377+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } )
378+ else {
379+ return
380+ }
381+ valueLength = remaining . count
382+ }
383+ #endif
384+ for _ in 0 ... ( valueLength / Self . maxTextClearBurst + 1 ) {
385+ let burst = Self . textClearBurst ( valueLength : valueLength )
386+ input . element . typeText ( String ( repeating : XCUIKeyboardKey . delete . rawValue , count : burst ) )
387+ // A burst that covers the value needs no read.
388+ guard burst < valueLength + Self . textClearSlack ,
389+ let remaining = ResolvedTextEntryInput ( reading : input . element )
390+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } ) ,
391+ Self . textClearContinues ( clearedLength : valueLength , remainingLength : remaining . count )
392+ else {
393+ return
394+ }
395+ valueLength = remaining . count
396+ }
397+ }
398+
399+ /// Extra deletes past a value's length, for whitespace its normalized read trimmed.
400+ private static let textClearSlack = 8
401+
402+ /// The most deletes one clear burst sends. A longer value takes more bursts.
403+ private static let maxTextClearBurst = 120
404+
405+ /// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
406+ /// that reads empty but may not be.
407+ static func textClearBurst ( valueLength : Int ) -> Int {
408+ let base = valueLength == 0 ? 24 : valueLength + textClearSlack
409+ return max ( 24 , min ( maxTextClearBurst , base ) )
410+ }
411+
412+ /// Whether a clear sends another burst after one sized from `clearedLength` characters left
413+ /// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
414+ /// rewrites its value ends the clear instead of looping; the fill's verification reports what
415+ /// remains.
416+ static func textClearContinues ( clearedLength : Int , remainingLength : Int ) -> Bool {
417+ remainingLength > 0 && remainingLength < clearedLength
350418 }
351419
352420 func isTextEntryElement ( _ element : XCUIElement ) -> Bool {
@@ -500,12 +568,6 @@ extension RunnerTests {
500568#endif
501569 }
502570
503- private func estimatedDeleteCount ( for element : any XCUIElementAttributes ) -> Int {
504- let valueText = normalizedElementText ( element . value )
505- let base = valueText . isEmpty ? 24 : ( valueText . count + 8 )
506- return max ( 24 , min ( 120 , base ) )
507- }
508-
509571 private func normalizedElementText ( _ value : Any ? ) -> String {
510572 String ( describing : value ?? " " )
511573 . trimmingCharacters ( in : . whitespacesAndNewlines )
0 commit comments