@@ -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 landed. Otherwise, or when that leaves
331+ /// text, delete bursts run from the caret, and a value longer than one burst is read again after
332+ /// each burst until it reads empty or stops shortening.
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,81 @@ 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
345348#if !os( tvOS )
346349 moveCaretToEnd ( app : app , input : input )
347350#endif
348- let deletes = String ( repeating : XCUIKeyboardKey . delete . rawValue , count : count )
349- input . element . typeText ( deletes )
351+ #if os( iOS )
352+ // The caret tap aims at the end of what the field shows, which a long value has scrolled
353+ // past, so the bursts alone can delete from mid-text (#3851). The tap still focuses the field.
354+ if existing != nil , clearTextInputBySelectingAll ( input ) {
355+ return
356+ }
357+ #endif
358+ while true {
359+ let burst = Self . textClearBurst ( valueLength : valueLength )
360+ input . element . typeText ( String ( repeating : XCUIKeyboardKey . delete . rawValue , count : burst ) )
361+ // A burst that covers the value needs no read: the common clear costs what it did.
362+ guard burst < valueLength + Self . textClearSlack ,
363+ let remaining = ResolvedTextEntryInput ( reading : input . element )
364+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } ) ,
365+ Self . textClearContinues ( clearedLength : valueLength , remainingLength : remaining . count )
366+ else {
367+ return
368+ }
369+ valueLength = remaining . count
370+ }
371+ }
372+
373+ #if os( iOS )
374+ /// Selects all of a readable value and deletes it, returning whether it then reads empty. An app
375+ /// process can ignore the select all it receives in its first second or two, while the delete
376+ /// still lands, so a value left behind is selected again.
377+ private func clearTextInputBySelectingAll ( _ input : ResolvedTextEntryInput ) -> Bool {
378+ for _ in 0 ..< Self . maxSelectAllClearAttempts {
379+ input . element . typeKey ( " a " , modifierFlags : . command )
380+ input . element . typeText ( XCUIKeyboardKey . delete . rawValue )
381+ guard let remaining = ResolvedTextEntryInput ( reading : input . element )
382+ . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } )
383+ else {
384+ return false
385+ }
386+ if remaining . isEmpty {
387+ return true
388+ }
389+ }
390+ return false
391+ }
392+
393+ /// The most select-all clears one clear sends. On an iOS 26 simulator a fresh app process
394+ /// ignored up to two before taking one; each costs about 0.75s.
395+ private static let maxSelectAllClearAttempts = 4
396+ #endif
397+
398+ /// Extra deletes past a value's length, for whitespace its normalized read trimmed.
399+ private static let textClearSlack = 8
400+
401+ /// The most deletes one clear burst sends. A longer value takes more bursts.
402+ private static let maxTextClearBurst = 120
403+
404+ /// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
405+ /// that reads empty but may not be.
406+ static func textClearBurst ( valueLength : Int ) -> Int {
407+ let base = valueLength == 0 ? 24 : valueLength + textClearSlack
408+ return max ( 24 , min ( maxTextClearBurst , base ) )
409+ }
410+
411+ /// Whether a clear sends another burst after one sized from `clearedLength` characters left
412+ /// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
413+ /// rewrites its value ends the clear instead of looping; the fill's verification reports what
414+ /// remains.
415+ static func textClearContinues ( clearedLength : Int , remainingLength : Int ) -> Bool {
416+ remainingLength > 0 && remainingLength < clearedLength
350417 }
351418
352419 func isTextEntryElement ( _ element : XCUIElement ) -> Bool {
@@ -500,12 +567,6 @@ extension RunnerTests {
500567#endif
501568 }
502569
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-
509570 private func normalizedElementText ( _ value : Any ? ) -> String {
510571 String ( describing : value ?? " " )
511572 . trimmingCharacters ( in : . whitespacesAndNewlines )
0 commit comments