Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/focused-identity-probe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'agent-device': patch
---

Read only the focused element identity before WebDriver fill taps its target. Avoid an unnecessary previous-field rectangle request that can exhaust the focus-probe budget; retain the post-tap identity/geometry checks and existing deadlines.
11 changes: 11 additions & 0 deletions packages/provider-webdriver/src/webdriver-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ test('createSession is bounded by the operation deadline when it is shorter', as
// The focused element is the only signal that can say WHICH field took focus,
// so `fill` fails closed when it is unavailable. Each wire shape it
// distinguishes is pinned here.
test('activeElementReference reads only the focused identity, not its geometry', async () => {
const paths: string[] = [];
const client = await connectedClient(async (input) => {
paths.push(new URL(String(input instanceof Request ? input.url : input)).pathname);
return Response.json({ value: { [W3C_ELEMENT_KEY]: 'el-1' } });
});

assert.deepEqual(await client.activeElementReference(), { id: 'el-1' });
assert.deepEqual(paths, ['/wd/hub/session/wd-1/element/active']);
});

test('activeElement returns the focused element id and rect', async () => {
const client = await connectedClient(
webDriverRoutes({
Expand Down
31 changes: 20 additions & 11 deletions packages/provider-webdriver/src/webdriver-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ export class WebDriverClient {
return typeof value === 'boolean' ? value : 'unsupported';
}

/** Focus identity without geometry, for comparisons that do not consume the rect. */
async activeElementReference(
timeoutMs?: number,
): Promise<Pick<WebDriverActiveElement, 'id'> | 'none' | 'unsupported'> {
const deadline = timeoutMs === undefined ? undefined : Date.now() + timeoutMs;
try {
const id = readW3CElementId(
await this.sessionRequest('GET', '/element/active', undefined, requestBudget(deadline)),
);
return id === undefined ? 'unsupported' : { id };
} catch (error) {
if (isWebDriverRouteUnsupported(error)) return 'unsupported';
if (isNoSuchElementError(error)) return 'none';
throw error;
}
}

/**
* Which element holds text-entry focus and where it sits, `none` when nothing
* does, or `unsupported` when this driver has no active-element route.
Expand Down Expand Up @@ -219,17 +236,9 @@ export class WebDriverClient {
// overrun the deadline it was derived from. One deadline, and the second
// request gets only what the first left.
const deadline = timeoutMs === undefined ? undefined : Date.now() + timeoutMs;
let elementId: string | undefined;
try {
elementId = readW3CElementId(
await this.sessionRequest('GET', '/element/active', undefined, requestBudget(deadline)),
);
} catch (error) {
if (isWebDriverRouteUnsupported(error)) return 'unsupported';
if (isNoSuchElementError(error)) return 'none';
throw error;
}
if (elementId === undefined) return 'unsupported';
const reference = await this.activeElementReference(requestBudget(deadline).timeoutMs);
if (typeof reference === 'string') return reference;
const elementId = reference.id;
try {
const value = await this.sessionRequest(
'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ function createTextEntryWorld() {
sendKeys: async () => {
world.transcript.push('keys');
},
activeElementReference: (timeoutMs?: number) => world.client.activeElement(timeoutMs),
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
activeElement: async (timeoutMs?: number) => {
world.transcript.push('active');
world.probeTimeouts.push(timeoutMs);
Expand Down
2 changes: 1 addition & 1 deletion packages/provider-webdriver/src/webdriver-interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ class WebDriverInteractor implements Interactor {
private async readTextEntryState(): Promise<TextEntryStateBeforeTap> {
const [keyboard, active] = await Promise.all([
this.client.isKeyboardShown(TEXT_ENTRY_PROBE_TIMEOUT_MS),
this.client.activeElement(TEXT_ENTRY_PROBE_TIMEOUT_MS),
this.client.activeElementReference(TEXT_ENTRY_PROBE_TIMEOUT_MS),
]);
return {
keyboard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ test('cloud iOS fill sends no keys when a keyboard-up tap misses the second fiel
});
}, 15_000);

test('cloud iOS fill does not read the previous field geometry before tapping its target', async () => {
await withProviderScenarioResource(createCloudIosWorld, async ({ daemon, server }) => {
const lease = await openCloudIosSession(daemon);
server.focusField('password', 'unchanged');
server.rectFailureField = 'password';

assertRpcOk(
await daemon.callCommand('fill', ['label=Email', 'replacement'], leaseFlags(lease.leaseId), {
meta: leaseMeta(lease.leaseId),
}),
);

assert.equal(server.fieldValues.email, 'replacement');
assert.equal(server.fieldValues.password, 'unchanged');
assert.equal(
server.calls.some((call) => call.path === '/wd/hub/session/wd-1/element/password/rect'),
false,
);
const tap = server.calls.findIndex(
(call) => call.method === 'POST' && call.path === '/wd/hub/session/wd-1/actions',
);
const rect = server.calls.findIndex(
(call) => call.path === '/wd/hub/session/wd-1/element/email/rect',
);
assert.ok(tap >= 0 && rect > tap, 'the target geometry must still be checked after the tap');
});
}, 15_000);

/** The tap/keys/keyboard requests in the order the driver received them. */
function textEntryTranscript(server: FakeIosWebDriverServer): string[] {
const labels: Record<string, string> = {
Expand Down Expand Up @@ -319,6 +347,7 @@ class FakeIosWebDriverServer extends CloudWebDriverTestServer {
fieldValues: Record<FieldName, string> = { email: '', password: '' };
focused: FieldName | undefined;
sourceOverride: string | undefined;
rectFailureField: FieldName | undefined;

private pollsRemaining = Number.POSITIVE_INFINITY;
private pendingFocus: FieldName | undefined;
Expand Down Expand Up @@ -393,6 +422,12 @@ class FakeIosWebDriverServer extends CloudWebDriverTestServer {
}

private elementRect(elementId: string): CloudWebDriverTestResponse {
if (elementId === this.rectFailureField) {
return cloudWebDriverTestJson(
{ value: { error: 'unknown error', message: 'previous field geometry unavailable' } },
503,
);
}
const rect = FIELDS[elementId as FieldName];
if (!rect) {
return cloudWebDriverTestJson(
Expand Down