@@ -361,27 +361,109 @@ extension RunnerTests {
361361 }
362362 }
363363
364- /// Clears `input`, which must have been read with nothing dispatched since.
365- func clearTextInput ( app : XCUIApplication , _ input : ResolvedTextEntryInput ) {
366- // Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
364+ /// Clears the reader's input. Every post goes through `reader.dispatch` and every read back through
365+ /// `reader.input()`, so a bound target's clear stops at an input that took its place. On iOS a
366+ /// readable value is selected whole and deleted, wherever the caret is. Otherwise, or when text is
367+ /// left, delete bursts run from the caret, and a value longer than one burst is read again after
368+ /// each burst until it reads empty, stops shortening, or has had at least one burst more than it
369+ /// needs.
370+ func clearTextInput ( app : XCUIApplication , reader : inout TextEntryReader ) {
371+ guard let input = reader . input ( ) else {
372+ return
373+ }
374+ // Skip the clear (edge-tap, select all, delete bursts) ONLY when we can confirm the
367375 // field is empty. Why skip: the edge-tap computes a point from the element frame, which can
368376 // be stale after the field repositions on focus (e.g. the Settings search bar jumps
369377 // bottom->top and reveals a "Suggestions" list) — tapping there navigates away instead of
370378 // clearing; and replacing into an already-empty field is a no-op anyway.
371379 // editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
372380 // emptiness — those must still be cleared, or replace would concatenate stale + new text.
373381 // So distinguish nil (clear) from "" (skip).
374- if let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true ) ,
375- existing . isEmpty {
382+ let existing = editableTextValue ( for : input . attributes , treatingPlaceholderAsEmpty : true )
383+ if existing ? . isEmpty == true {
376384 return
377385 }
378386 // Read before the caret tap, which dispatches to the app.
379- let count = estimatedDeleteCount ( for : input . attributes )
387+ var valueLength = normalizedElementText ( input . attributes . value ) . count
388+ // The tap also focuses the field, which a clear can reach with focus unconfirmed.
380389#if !os( tvOS )
381- moveCaretToEnd ( app : app , input : input )
390+ reader . dispatch { moveCaretToEnd ( app : app , input : input ) }
391+ #endif
392+ // An input that stops answering once focused (#3060) cannot be read back, so it gets one burst.
393+ guard reader . input ( ) != nil else {
394+ let burst = Self . textClearBurst ( valueLength : valueLength )
395+ reader . dispatch { input . element . typeText ( String ( repeating : XCUIKeyboardKey . delete . rawValue , count : burst ) ) }
396+ return
397+ }
398+ #if os( iOS )
399+ // The tap aims at the end of what the field shows, which a long value has scrolled past, so
400+ // bursts from there reach only what is before the caret (#3851). Select all reaches all of it, but
401+ // only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
402+ // bursts alone.
403+ if existing != nil {
404+ var passes = 0
405+ let outcome = Self . clearUntilEmpty (
406+ clearOnce : {
407+ guard let input = reader . input ( ) else {
408+ return . stop
409+ }
410+ passes += 1
411+ reader . dispatch {
412+ input . element . typeKey ( " a " , modifierFlags : . command )
413+ input . element . typeText ( XCUIKeyboardKey . delete . rawValue )
414+ }
415+ return . posted
416+ } ,
417+ read : {
418+ textValueRead ( for : reader . input ( ) ? . attributes , treatingPlaceholderAsEmpty : true )
419+ }
420+ )
421+ NSLog ( " AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@ " , passes , String ( describing : outcome ) )
422+ // Text left after every pass goes to the bursts; an input that no longer resolves is not typed into.
423+ guard outcome == . notCleared ,
424+ let remaining = reader . input ( ) . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } )
425+ else {
426+ return
427+ }
428+ valueLength = remaining . count
429+ }
382430#endif
383- let deletes = String ( repeating : XCUIKeyboardKey . delete . rawValue , count : count )
384- input . element . typeText ( deletes )
431+ for _ in 0 ... ( valueLength / Self . maxTextClearBurst + 1 ) {
432+ guard let input = reader . input ( ) else {
433+ return
434+ }
435+ let burst = Self . textClearBurst ( valueLength : valueLength )
436+ reader . dispatch { input . element . typeText ( String ( repeating : XCUIKeyboardKey . delete . rawValue , count : burst ) ) }
437+ // A burst that covers the value needs no read.
438+ guard burst < valueLength + Self . textClearSlack ,
439+ let remaining = reader . input ( ) . flatMap ( { editableTextValue ( for : $0 . attributes , treatingPlaceholderAsEmpty : true ) } ) ,
440+ Self . textClearContinues ( clearedLength : valueLength , remainingLength : remaining . count )
441+ else {
442+ return
443+ }
444+ valueLength = remaining . count
445+ }
446+ }
447+
448+ /// Extra deletes past a value's length, for whitespace its normalized read trimmed.
449+ private static let textClearSlack = 8
450+
451+ /// The most deletes one clear burst sends. A longer value takes more bursts.
452+ private static let maxTextClearBurst = 120
453+
454+ /// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
455+ /// that reads empty but may not be.
456+ static func textClearBurst ( valueLength : Int ) -> Int {
457+ let base = valueLength == 0 ? 24 : valueLength + textClearSlack
458+ return max ( 24 , min ( maxTextClearBurst , base ) )
459+ }
460+
461+ /// Whether a clear sends another burst after one sized from `clearedLength` characters left
462+ /// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
463+ /// rewrites its value ends the clear instead of looping; the fill's verification reports what
464+ /// remains.
465+ static func textClearContinues ( clearedLength : Int , remainingLength : Int ) -> Bool {
466+ remainingLength > 0 && remainingLength < clearedLength
385467 }
386468
387469 func isTextEntryElement ( _ element : XCUIElement ) -> Bool {
@@ -535,12 +617,6 @@ extension RunnerTests {
535617#endif
536618 }
537619
538- private func estimatedDeleteCount ( for element : any XCUIElementAttributes ) -> Int {
539- let valueText = normalizedElementText ( element . value )
540- let base = valueText . isEmpty ? 24 : ( valueText . count + 8 )
541- return max ( 24 , min ( 120 , base ) )
542- }
543-
544620 private func normalizedElementText ( _ value : Any ? ) -> String {
545621 String ( describing : value ?? " " )
546622 . trimmingCharacters ( in : . whitespacesAndNewlines )
0 commit comments