Skip to content

Commit af38a20

Browse files
fix(webdriver): replace text in the confirmed focused element
1 parent 21f3f50 commit af38a20

9 files changed

Lines changed: 235 additions & 46 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'agent-device': patch
3+
---
4+
5+
Make hosted WebDriver `fill` replace existing text by clearing the confirmed focused element before sending replacement text to that same element. An empty replacement clears without typing. A failed clear or an unavailable focused-element reference prevents replacement text from being sent. Keep `type` append-only.

‎packages/provider-webdriver/src/webdriver-client.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,56 @@ test('activeElement bounds its two sequential requests by one shared budget', as
362362
);
363363
});
364364

365+
test('element clear and replacement address the same encoded element reference', async () => {
366+
const calls: Array<{ path: string; method: string | undefined; body: unknown }> = [];
367+
const client = await connectedClient(async (input, init) => {
368+
calls.push({
369+
path: new URL(String(input)).pathname,
370+
method: init?.method,
371+
body: init?.body === undefined ? undefined : JSON.parse(String(init.body)),
372+
});
373+
return new Response(JSON.stringify({ value: null }), { status: 200 });
374+
});
375+
376+
await client.clearElement('field/one');
377+
await client.sendElementKeys('field/one', 'a😀');
378+
379+
assert.deepEqual(calls, [
380+
{ path: '/wd/hub/session/wd-1/element/field%2Fone/clear', method: 'POST', body: {} },
381+
{
382+
path: '/wd/hub/session/wd-1/element/field%2Fone/value',
383+
method: 'POST',
384+
body: { text: 'a😀', value: ['a', '😀'] },
385+
},
386+
]);
387+
});
388+
389+
for (const operation of ['clear', 'replace'] as const) {
390+
test(`element ${operation} does not retry a provider failure`, async () => {
391+
let calls = 0;
392+
globalThis.fetch = webDriverResponse(200, { value: { sessionId: 'wd-1', capabilities: {} } });
393+
const client = new WebDriverClient({
394+
clientVersion: '0.0.0-test',
395+
endpoint: 'http://cloud-webdriver.test/wd/hub/',
396+
requestPolicy: { retryAttempts: 3 },
397+
});
398+
await client.createSession({ platformName: 'iOS' });
399+
globalThis.fetch = async () => {
400+
calls += 1;
401+
return new Response(JSON.stringify({ value: { message: 'provider unavailable' } }), {
402+
status: 503,
403+
});
404+
};
405+
406+
await assert.rejects(
407+
operation === 'clear'
408+
? client.clearElement('field')
409+
: client.sendElementKeys('field', 'replacement'),
410+
);
411+
assert.equal(calls, 1);
412+
});
413+
}
414+
365415
const W3C_ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf';
366416

367417
/** Dispatches by URL suffix, since `activeElement` makes two round trips. */

‎packages/provider-webdriver/src/webdriver-client.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export type WebDriverWindowRect = {
4040
};
4141

4242
export type WebDriverActiveElement = {
43-
/** Opaque W3C element reference; only ever compared for equality. */
43+
/** Opaque W3C element reference. */
4444
id: string;
4545
rect: WebDriverWindowRect;
4646
};
@@ -157,6 +157,17 @@ export class WebDriverClient {
157157
await this.sessionRequest('POST', '/keys', { value: Array.from(text) });
158158
}
159159

160+
async clearElement(elementId: string): Promise<void> {
161+
await this.sessionRequest('POST', `/element/${encodeURIComponent(elementId)}/clear`);
162+
}
163+
164+
async sendElementKeys(elementId: string, text: string): Promise<void> {
165+
await this.sessionRequest('POST', `/element/${encodeURIComponent(elementId)}/value`, {
166+
text,
167+
value: Array.from(text),
168+
});
169+
}
170+
160171
async hideKeyboard(): Promise<void> {
161172
await this.sessionRequest('POST', '/appium/device/hide_keyboard');
162173
}

‎packages/provider-webdriver/src/webdriver-interactor-mutation-resend.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ function routeOf(input: Parameters<typeof fetch>[0], init?: RequestInit): string
2424
const method = init?.method ?? 'GET';
2525
const pathname = url.pathname
2626
.replace(/^\/wd\/hub/, '')
27-
.replace(/^\/session\/wd-1/, '/session/:id');
27+
.replace(/^\/session\/wd-1/, '/session/:id')
28+
.replace(/\/element\/[^/]+(?=\/(?:clear|value)$)/, '/element/:element');
2829
if (pathname.endsWith('/execute/sync') && typeof init?.body === 'string') {
2930
return (JSON.parse(init.body) as { script: string }).script;
3031
}
@@ -131,6 +132,12 @@ const MUTATING_ROUTES: Record<string, MutatingRouteRow> = {
131132
'POST /session/:id/actions': { act: ({ interactor }) => interactor.tap(10, 20) },
132133
'DELETE /session/:id/actions': { act: ({ client }) => client.releaseActions() },
133134
'POST /session/:id/keys': { act: ({ client }) => client.sendKeys('hello') },
135+
'POST /session/:id/element/:element/clear': {
136+
act: ({ client }) => client.clearElement('field-1'),
137+
},
138+
'POST /session/:id/element/:element/value': {
139+
act: ({ client }) => client.sendElementKeys('field-1', 'hello'),
140+
},
134141
'POST /session/:id/back': { act: ({ interactor }) => interactor.back() },
135142
'POST /session/:id/rotation': { act: ({ client }) => client.setRotation(90) },
136143
'POST /session/:id/orientation': { act: ({ client }) => client.setOrientation('LANDSCAPE') },
@@ -188,7 +195,7 @@ function requestRoutesIn(source: string): string[] {
188195
),
189196
].map(([, helper, method, routePath]) => {
190197
const absolute = helper === 'sessionRequest' ? `/session/:id${routePath}` : routePath!;
191-
return `${method} ${absolute.replace('${sessionId}', ':id')}`;
198+
return `${method} ${absolute.replace('${sessionId}', ':id').replace('${encodeURIComponent(elementId)}', ':element')}`;
192199
});
193200
}
194201

‎packages/provider-webdriver/src/webdriver-interactor.test.ts‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test('fill withholds keys until the tapped point holds text-entry focus', async
2727
'active',
2828
'active',
2929
'active',
30+
'clear',
3031
'keys',
3132
]);
3233
});
@@ -44,7 +45,7 @@ test('fill accepts a keyboard-up second field once focus lands on the tapped poi
4445
const result = await runFill(world);
4546

4647
assert.equal(result?.textEntryReadiness, 'focused-element');
47-
assert.deepEqual(world.transcript, ['keyboard', 'active', 'tap', 'active', 'keys']);
48+
assert.deepEqual(world.transcript, ['keyboard', 'active', 'tap', 'active', 'clear', 'keys']);
4849
});
4950

5051
// P1, the two-field misdelivery: email filled, keyboard stays up, the password
@@ -93,22 +94,23 @@ test('fill accepts a re-fill of the already-focused field it tapped', async () =
9394
assert.equal(result?.textEntryReadiness, 'focused-element');
9495
});
9596

96-
test('fill falls back to the keyboard transition when there is no active-element route', async () => {
97+
test('fill refuses replacement when only the keyboard transition can confirm focus', async () => {
9798
const world = createTextEntryWorld();
9899
world.activeRoute = 'unimplemented';
99100
world.keyboardShown = [false, false, true];
100101

101-
const result = await runFill(world);
102-
103-
assert.equal(result?.textEntryReadiness, 'keyboard-shown');
102+
await assert.rejects(runFill(world), (error: AppError) => {
103+
assert.equal(error.code, 'UNSUPPORTED_OPERATION');
104+
assert.equal(error.details?.reason, 'text_entry_clear_target_unavailable');
105+
return true;
106+
});
104107
assert.deepEqual(world.transcript, [
105108
'keyboard',
106109
'active',
107110
'tap',
108111
'active',
109112
'keyboard',
110113
'keyboard',
111-
'keys',
112114
]);
113115
});
114116

@@ -231,25 +233,17 @@ test('fill caps each probe timeout by the readiness budget remaining', async ()
231233
* past the whole readiness budget is safe for every case — once readiness
232234
* resolves there are no pending timers left to fire.
233235
*/
234-
// `fill <target> ""` is the clear-field primitive (#2063). This backend's fill is tap +
235-
// sendKeys and owns no clear mechanism, so an empty fill must refuse up front — sending zero
236-
// keys and reporting success would claim a clear that never ran, before even touching the
237-
// device.
238-
test('fill refuses empty text as an unsupported clear rather than a vacuous success', async () => {
236+
test('fill clears an empty replacement without sending keys', async () => {
239237
const world = createTextEntryWorld();
238+
world.activeRects = [CONTAINS_TAP];
240239
const interactor = createWebDriverInteractor({
241240
client: world.client,
242241
backend: 'xctest',
243242
capabilities: createCloudWebDriverCapabilities({ provider: 'test', platform: 'ios' }),
244243
});
245244

246-
await assert.rejects(interactor.fill(12, 24, ''), (error: AppError) => {
247-
assert.equal(error.code, 'UNSUPPORTED_OPERATION');
248-
assert.match(error.message, /clear field/);
249-
return true;
250-
});
251-
// Fail-closed means untouched: no tap, no keys, no probes reached the device.
252-
assert.deepEqual(world.transcript, []);
245+
await interactor.fill(12, 24, '');
246+
assert.deepEqual(world.transcript, ['keyboard', 'active', 'tap', 'active', 'clear']);
253247
});
254248

255249
test('iOS WebDriver interactor routes snapshots through the acquisition adapter', async () => {
@@ -446,7 +440,10 @@ function createTextEntryWorld() {
446440
world.tapped = true;
447441
},
448442
releaseActions: async () => {},
449-
sendKeys: async () => {
443+
clearElement: async () => {
444+
world.transcript.push('clear');
445+
},
446+
sendElementKeys: async () => {
450447
world.transcript.push('keys');
451448
},
452449
activeElement: async (timeoutMs?: number) => {

‎packages/provider-webdriver/src/webdriver-interactor.ts‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { DeviceRotation, HardwareButton } from '@agent-device/contracts/device';
22
import type { BackMode } from '@agent-device/contracts/back-mode';
33
import 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

Comments
 (0)