Skip to content

Commit 913266c

Browse files
fix(webdriver): refuse unsupported focus before tapping
1 parent 9d92f13 commit 913266c

7 files changed

Lines changed: 105 additions & 174 deletions

File tree

‎.changeset/webdriver-fill-replacement.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'agent-device': patch
33
---
44

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.
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 prevents replacement text from being sent. Unsupported focused-element lookup refuses before tapping; keyboard visibility alone never authorizes a fill. Keep `type` append-only.

‎packages/contracts/src/interactor-types.ts‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,21 @@ export type TypeTextBackendResult = {
151151
* - `focused-element`: after the tap, the element holding text-entry focus is the
152152
* one this fill aimed at — positive evidence that THIS field, not merely some
153153
* field, took focus. A field that focusing re-laid out still counts.
154-
* - `keyboard-shown`: legacy WebDriver evidence that the software keyboard went
155-
* from hidden to shown after the tap. Retained for released provider results;
156-
* current WebDriver fills require a focused element reference and do not emit it.
157154
* Current cloud fills report `focused-element`, including clear-only fills.
158155
* There is deliberately no value for "typed without evidence": nothing renders
159156
* this field, so such a value would reach a caller as an ordinary success and
160157
* recreate the #1658 silent false success it exists to remove. Every
161158
* unwitnessed case throws without typing instead —
162159
*
163-
* - `text_entry_focus_not_observed`: the tap focused nothing, or a keyboard
164-
* already up on a driver that cannot say which field owns it (keys would have
165-
* landed in the PREVIOUS field).
166-
* - `text_entry_focus_unobservable`: the driver implements neither route, so no
167-
* wait could establish anything. `press` + `type` remains the deliberate way
168-
* to enter text unwitnessed.
160+
* - `text_entry_focus_not_observed`: the tap focused nothing or left the
161+
* previous field focused outside the tapped point.
162+
* - `text_entry_clear_target_unavailable`: the driver cannot report a focused
163+
* element reference. An unsupported pre-tap lookup refuses before tapping.
169164
*
170-
* Closed set accepted by readFillBackendResult at the touch boundary. It keeps
171-
* the legacy result readable and drops values it cannot name.
165+
* Closed set accepted by readFillBackendResult at the touch boundary. It drops
166+
* values it cannot name.
172167
*/
173-
export const CLOUD_TEXT_ENTRY_READINESS = ['focused-element', 'keyboard-shown'] as const;
168+
export const CLOUD_TEXT_ENTRY_READINESS = ['focused-element'] as const;
174169

175170
export type CloudTextEntryReadiness = (typeof CLOUD_TEXT_ENTRY_READINESS)[number];
176171

‎packages/daemon/src/core/fill-backend-result.test.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { test } from 'vitest';
22
import assert from 'node:assert/strict';
33
import { readFillBackendResult } from './fill-backend-result.ts';
44

5-
test('readFillBackendResult preserves released keyboard-shown readiness', () => {
6-
assert.deepEqual(readFillBackendResult({ textEntryReadiness: 'keyboard-shown' }), {
7-
textEntryReadiness: 'keyboard-shown',
5+
test('readFillBackendResult preserves focused-element readiness', () => {
6+
assert.deepEqual(readFillBackendResult({ textEntryReadiness: 'focused-element' }), {
7+
textEntryReadiness: 'focused-element',
88
});
99
});
1010

11-
test('readFillBackendResult drops an unknown readiness value', () => {
12-
assert.deepEqual(readFillBackendResult({ textEntryReadiness: 'keyboard-visible' }), {});
13-
});
11+
test.each(['keyboard-shown', 'keyboard-visible'])(
12+
'readFillBackendResult drops an unsupported readiness value: %s',
13+
(textEntryReadiness) => {
14+
assert.deepEqual(readFillBackendResult({ textEntryReadiness }), {});
15+
},
16+
);
1417

1518
test('readFillBackendResult preserves a complete unconfirmed verification', () => {
1619
assert.deepEqual(

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

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ test('fill withholds keys until the tapped point holds text-entry focus', async
2424
['tapped', '[email protected]'],
2525
]);
2626
assert.deepEqual(world.transcript, [
27-
'keyboard',
2827
'active',
2928
'tap',
3029
'active',
@@ -39,24 +38,22 @@ test('fill withholds keys until the tapped point holds text-entry focus', async
3938
// whose keyboard is already up. The keyboard says "a field is focused" both
4039
// before and after, so only the focused element's own geometry can tell whether
4140
// OUR tap moved focus.
42-
test('fill accepts a keyboard-up second field once focus lands on the tapped point', async () => {
41+
test('fill accepts a second field once focus lands on the tapped point', async () => {
4342
const world = createTextEntryWorld();
44-
world.keyboardShown = [true];
4543
world.focusedBeforeTap = PREVIOUS_FIELD;
4644
world.activeRects = [CONTAINS_TAP];
4745

4846
const result = await runFill(world);
4947

5048
assert.equal(result?.textEntryReadiness, 'focused-element');
51-
assert.deepEqual(world.transcript, ['keyboard', 'active', 'tap', 'active', 'clear', 'keys']);
49+
assert.deepEqual(world.transcript, ['active', 'tap', 'active', 'clear', 'keys']);
5250
});
5351

5452
// P1, the two-field misdelivery: email filled, keyboard stays up, the password
5553
// tap misses. Focus never leaves the email field, so a global `/keys` would
5654
// append the password to the EMAIL field while reporting success.
57-
test('fill refuses when a keyboard-up tap misses and focus stays on the previous field', async () => {
55+
test('fill refuses when a tap misses and focus stays on the previous field', async () => {
5856
const world = createTextEntryWorld();
59-
world.keyboardShown = [true];
6057
world.focusedBeforeTap = PREVIOUS_FIELD;
6158
world.activeRects = [PREVIOUS_FIELD];
6259

@@ -96,60 +93,38 @@ test('fill accepts a re-fill of the already-focused field it tapped', async () =
9693
assert.equal(result?.textEntryReadiness, 'focused-element');
9794
});
9895

99-
test('fill refuses replacement when only the keyboard transition can confirm focus', async () => {
100-
const world = createTextEntryWorld();
101-
world.activeRoute = 'unimplemented';
102-
world.keyboardShown = [false, false, true];
103-
104-
await assert.rejects(runFill(world), (error: AppError) => {
105-
assert.equal(error.code, 'UNSUPPORTED_OPERATION');
106-
assert.equal(error.details?.reason, 'text_entry_clear_target_unavailable');
107-
return true;
108-
});
109-
assert.deepEqual(world.transcript, [
110-
'keyboard',
111-
'active',
112-
'tap',
113-
'active',
114-
'keyboard',
115-
'keyboard',
116-
]);
117-
});
96+
for (const keyboardRoute of ['ok', 'unimplemented', 'failing'] as const) {
97+
test(`fill refuses before tapping when focused-element lookup is unsupported (${keyboardRoute} keyboard)`, async () => {
98+
const world = createTextEntryWorld();
99+
world.activeRoute = 'unimplemented';
100+
world.keyboardRoute = keyboardRoute;
118101

119-
// Without an active-element route, a keyboard that was ALREADY up witnesses
120-
// nothing about the new field — the exact silent misdelivery P1 names.
121-
test('fill refuses a keyboard-up tap when the driver cannot report the focused field', async () => {
122-
const world = createTextEntryWorld();
123-
world.activeRoute = 'unimplemented';
124-
world.keyboardShown = [true];
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+
});
125107

126-
await assert.rejects(runFill(world), (error: AppError) => {
127-
assert.match(error.message, /cannot report which field holds focus/);
128-
assert.equal(error.details?.reason, 'text_entry_focus_not_observed');
129-
return true;
108+
assert.deepEqual(world.transcript, ['active']);
109+
assert.deepEqual(world.probeTimeouts, [1_500]);
130110
});
111+
}
131112

132-
assert.ok(!world.transcript.some((step) => step === 'clear' || step === 'keys'));
133-
});
134-
135-
// The last path that typed without evidence. It reported ordinary success, and
136-
// nothing renders `textEntryReadiness`, so it read to a caller exactly like a
137-
// fill that worked — the #1658 false success this change exists to remove.
138-
test('fill refuses when the driver implements neither focus route', async () => {
113+
test('fill refuses when focused-element lookup becomes unsupported after the tap', async () => {
139114
const world = createTextEntryWorld();
140115
world.activeRoute = 'unimplemented';
141-
world.keyboardRoute = 'unimplemented';
116+
world.client.activeElementReference = async () => {
117+
world.transcript.push('active');
118+
return { id: 'previous' };
119+
};
142120

143121
await assert.rejects(runFill(world), (error: AppError) => {
144-
assert.equal(error.code, 'COMMAND_FAILED');
145-
assert.match(error.message, /reports neither the focused element nor keyboard state/);
146-
// A capability gap, not a missed tap: the caller's next move differs.
147-
assert.equal(error.details?.reason, 'text_entry_focus_unobservable');
148-
assert.match(String(error.details?.hint), /press <target> followed by type <text>/);
122+
assert.equal(error.code, 'UNSUPPORTED_OPERATION');
123+
assert.equal(error.details?.reason, 'text_entry_clear_target_unavailable');
149124
return true;
150125
});
151126

152-
assert.ok(!world.transcript.some((step) => step === 'clear' || step === 'keys'));
127+
assert.deepEqual(world.transcript, ['active', 'tap', 'active']);
153128
});
154129

155130
// The #1658 report is "fill answered Filled N chars while the field kept its
@@ -171,17 +146,17 @@ test('fill refuses without typing when nothing ever takes focus', async () => {
171146

172147
// A probe that FAILED is not a driver without the feature. Swallowing a dead
173148
// session or a grid outage as "unsupported" would degrade it into a blind type.
174-
test('fill propagates a failing keyboard probe instead of typing blind', async () => {
149+
test('fill propagates a failing pre-tap focus probe instead of typing blind', async () => {
175150
const world = createTextEntryWorld();
176-
world.keyboardRoute = 'failing';
151+
world.activeRoute = 'failing';
177152

178153
await assert.rejects(runFill(world), (error: AppError) => {
179-
assert.match(error.message, /invalid session id/);
154+
assert.match(error.message, /active element exploded/);
180155
return true;
181156
});
182157

183158
// It threw on a pre-tap probe, so the device was never touched at all.
184-
assert.deepEqual(world.transcript, ['keyboard', 'active']);
159+
assert.deepEqual(world.transcript, ['active']);
185160
});
186161

187162
// Post-tap the device HAS been touched, so one transient grid error must not
@@ -198,12 +173,12 @@ test('fill tolerates a transient probe failure and still witnesses focus', async
198173

199174
// ...but a budget that expires without a single answered probe is a broken
200175
// session, and must surface as itself rather than as "the tap missed".
201-
test('fill rethrows the probe failure when no probe ever answers', async () => {
176+
test('fill rethrows the probe failure when no post-tap probe ever answers', async () => {
202177
const world = createTextEntryWorld();
203-
world.activeRoute = 'failing';
178+
world.activeFailuresAfterTap = Number.POSITIVE_INFINITY;
204179

205180
await assert.rejects(runFill(world), (error: AppError) => {
206-
assert.match(error.message, /active element exploded/);
181+
assert.match(error.message, /transient grid blip/);
207182
return true;
208183
});
209184

@@ -246,7 +221,7 @@ test('fill clears an empty replacement without sending keys', async () => {
246221
});
247222

248223
await interactor.fill(12, 24, '');
249-
assert.deepEqual(world.transcript, ['keyboard', 'active', 'tap', 'active', 'clear']);
224+
assert.deepEqual(world.transcript, ['active', 'tap', 'active', 'clear']);
250225
});
251226

252227
test('iOS WebDriver interactor routes snapshots through the acquisition adapter', async () => {
@@ -400,8 +375,8 @@ async function runFill(world: ReturnType<typeof createTextEntryWorld>) {
400375
}
401376

402377
/**
403-
* Records the request order `fill` produces. The ordering IS the fix: a `keys`
404-
* entry that follows `tap` with no keyboard evidence between them is the bug.
378+
* Records the request order `fill` produces. Clearing or typing before the
379+
* tapped field holds confirmed focus is the bug.
405380
*/
406381
type FocusedElement = {
407382
id: string;
@@ -424,7 +399,6 @@ function createTextEntryWorld() {
424399
const world = {
425400
transcript: [] as string[],
426401
probeTimeouts: [] as Array<number | undefined>,
427-
keyboardShown: [true] as boolean[],
428402
/** How the driver answers each route: normally, not at all, or with a real failure. */
429403
keyboardRoute: 'ok' as 'ok' | 'unimplemented' | 'failing',
430404
activeRoute: 'ok' as 'ok' | 'unimplemented' | 'failing',
@@ -484,10 +458,7 @@ function createTextEntryWorld() {
484458
// why the client classifies on the W3C error code instead.
485459
throw new AppError('COMMAND_FAILED', 'invalid session id', { status: 404 });
486460
}
487-
// The last programmed reading is what the keyboard stays at.
488-
return world.keyboardShown.length > 1
489-
? world.keyboardShown.shift()!
490-
: world.keyboardShown[0]!;
461+
return true;
491462
},
492463
} as unknown as WebDriverClient;
493464
return world;

0 commit comments

Comments
 (0)