11import type { DeviceRotation , HardwareButton } from '@agent-device/contracts/device' ;
22import type { BackMode } from '@agent-device/contracts/back-mode' ;
33import type {
4- CloudTextEntryReadiness ,
54 Interactor ,
65 ScreenshotOptions ,
76 SnapshotOptions ,
@@ -239,18 +238,6 @@ class WebDriverInteractor implements Interactor {
239238 _delayMs ?: number ,
240239 ) : Promise < Record < string , unknown > > {
241240 this . requireSupport ( 'fill' ) ;
242- // This backend's fill is tap + sendKeys: it never clears the existing value, so the
243- // clear-field request (`fill <target> ""`, #2063) has no mechanism here — sending zero keys
244- // and reporting success would claim a clear that never ran.
245- if ( text . length === 0 ) {
246- throw new AppError (
247- 'UNSUPPORTED_OPERATION' ,
248- 'fill with empty text (clear field) is not supported on the webdriver backend' ,
249- {
250- hint : 'Use a visible clear/reset control in the app, or fill the field with the intended replacement text directly.' ,
251- } ,
252- ) ;
253- }
254241 // #1658: both readings are taken BEFORE the tap, because each is only
255242 // evidence as a change. A keyboard that was already up says nothing about
256243 // which field owns first responder now, and a focused element is only
@@ -259,9 +246,10 @@ class WebDriverInteractor implements Interactor {
259246 // fill that has not touched the device rather than typing into the dark.
260247 const beforeTap = await this . readTextEntryState ( ) ;
261248 await this . tap ( x , y ) ;
262- const textEntryReadiness = await this . awaitTextEntryReadiness ( beforeTap , x , y ) ;
263- await this . type ( text ) ;
264- return { backend : 'webdriver' , x, y, text, textEntryReadiness } ;
249+ const focusedElementId = await this . awaitTextEntryReadiness ( beforeTap , x , y ) ;
250+ await this . client . clearElement ( focusedElementId ) ;
251+ if ( text . length > 0 ) await this . client . sendElementKeys ( focusedElementId , text ) ;
252+ return { backend : 'webdriver' , x, y, text, textEntryReadiness : 'focused-element' } ;
265253 }
266254
267255 async scroll (
@@ -625,7 +613,7 @@ class WebDriverInteractor implements Interactor {
625613 beforeTap : TextEntryStateBeforeTap ,
626614 x : number ,
627615 y : number ,
628- ) : Promise < CloudTextEntryReadiness > {
616+ ) : Promise < string > {
629617 const keyboardBeforeTap = beforeTap . keyboard ;
630618 const deadline = Date . now ( ) + TEXT_ENTRY_READINESS_TIMEOUT_MS ;
631619 // Strongest evidence first: the focused element itself. Keyboard visibility
@@ -642,14 +630,17 @@ class WebDriverInteractor implements Interactor {
642630 // field that is ALREADY focused moves nothing, and only geometry can tell
643631 // that from a tap that missed everything.
644632 const focusedBeforeTap = beforeTap . focusedElementId ;
633+ let focusedElementId : string | undefined ;
645634 const focus = await this . pollWithinBudget ( deadline , async ( timeoutMs ) => {
646635 const active = await this . client . activeElement ( timeoutMs ) ;
647636 if ( active === 'unsupported' ) return 'abandon' ;
648637 if ( active === 'none' ) return 'retry' ;
649638 const moved = active . id !== focusedBeforeTap ;
650- return moved || rectContains ( active . rect , x , y ) ? 'satisfied' : 'retry' ;
639+ if ( ! moved && ! rectContains ( active . rect , x , y ) ) return 'retry' ;
640+ focusedElementId = active . id ;
641+ return 'satisfied' ;
651642 } ) ;
652- if ( focus === 'satisfied' ) return 'focused-element' ;
643+ if ( focusedElementId !== undefined ) return focusedElementId ;
653644 if ( focus === 'expired' ) {
654645 throw textEntryFocusNotObservedError (
655646 x ,
@@ -682,7 +673,13 @@ class WebDriverInteractor implements Interactor {
682673 const keyboard = await this . pollWithinBudget ( deadline , async ( timeoutMs ) =>
683674 ( await this . client . isKeyboardShown ( timeoutMs ) ) === true ? 'satisfied' : 'retry' ,
684675 ) ;
685- if ( keyboard === 'satisfied' ) return 'keyboard-shown' ;
676+ if ( keyboard === 'satisfied' ) {
677+ throw new AppError (
678+ 'UNSUPPORTED_OPERATION' ,
679+ 'fill requires a focused element reference to clear and replace the field' ,
680+ { reason : 'text_entry_clear_target_unavailable' } ,
681+ ) ;
682+ }
686683 throw textEntryFocusNotObservedError ( x , y , 'no keyboard appeared, so the text was not sent' ) ;
687684 }
688685
0 commit comments