Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions packages/web/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
targetPage,
type ActionTarget,
} from './support.ts';
import type { ConnectionBudget } from './operation-budget.ts';
import { connectionAbort, type ConnectionBudget } from './operation-budget.ts';

/** How long a long press holds the button when the action names no duration. */
const DEFAULT_LONG_PRESS_MS = 500;
Expand Down Expand Up @@ -107,16 +107,25 @@ function isRadio(element: ElementHandle<Element>): Promise<boolean> {
async function uploadFiles(target: ActionTarget, paths: readonly string[], operation: ConnectionBudget): Promise<void> {
const deadline = new Deadline(operation.timeoutMs);
const remaining = (): number => Math.max(1, deadline.remaining());
// The outer operation guard can abandon the chooser wait. Never upload
// files after a cancelled or timed-out step releases the worker.
const ensureActive = (): void => {
if (operation.signal.aborted) throw connectionAbort(operation.signal, 'upload');
};
const element =
target.kind === 'element' ? target.element : await target.locator.elementHandle({ timeout: operation.timeoutMs });
try {
if (await isFileInput(element)) {
const direct = await isFileInput(element);
ensureActive();
if (direct) {
await element.setInputFiles([...paths], { timeout: remaining() });
return;
}
const page = await targetPage(target);
ensureActive();
const opened = page.waitForEvent('filechooser', { timeout: remaining() });
opened.catch(() => undefined);
ensureActive();
await element.click({ timeout: remaining() });
const chooser = await opened.catch((cause: unknown) => {
if (!isPwTimeout(cause)) throw cause;
Expand All @@ -125,6 +134,7 @@ async function uploadFiles(target: ActionTarget, paths: readonly string[], opera
cause,
});
});
ensureActive();
await chooser.setFiles([...paths], { timeout: remaining() });
} finally {
if (target.kind === 'locator') void element.dispose().catch(() => undefined);
Expand Down
65 changes: 64 additions & 1 deletion packages/web/tests/unit/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function stubUpload(fileInput: boolean) {
}),
};
const element = {
evaluate: vi.fn(async () => fileInput),
evaluate: vi.fn(async (_predicate: (node: Element) => boolean) => fileInput),
setInputFiles: vi.fn(async (_paths: string[], _options: unknown) => undefined),
click: vi.fn(async (_options: unknown) => void calls.push('click')),
dispose: vi.fn(async () => undefined),
Expand Down Expand Up @@ -237,6 +237,33 @@ describe('dispatchLocatorAction', () => {
expect(page.waitForEvent).not.toHaveBeenCalled();
});

it('recognizes a label associated with a file input instead of opening a chooser', async () => {
// Execute isFileInput's actual evaluate callback. A stub that always
// answers true cannot protect the production label-control lookup.
class FileInput {
readonly type = 'file';
}
vi.stubGlobal('HTMLInputElement', FileInput);
try {
const { element, target, page } = stubUpload(false);
const label = {
closest: (selector: string) => selector === 'label' ? { control: new FileInput() } : null,
} as unknown as Element;
element.evaluate.mockImplementation(async (predicate) => predicate(label));
await dispatchLocatorAction(
target,
{ kind: 'setInputFiles', paths: ['/tmp/a.txt'] },
{ timeoutMs: 1_000, signal: new AbortController().signal },
lookup,
);
expect(element.setInputFiles).toHaveBeenCalledWith(['/tmp/a.txt'], { timeout: expect.any(Number) });
expect(element.click).not.toHaveBeenCalled();
expect(page.waitForEvent).not.toHaveBeenCalled();
} finally {
vi.unstubAllGlobals();
}
});

it('fails as NOT_ACTIONABLE when the click opens no file chooser', async () => {
const { target, page } = stubUpload(false);
page.waitForEvent.mockRejectedValue(Object.assign(new Error('Timeout 7ms exceeded'), { name: 'TimeoutError' }));
Expand All @@ -259,6 +286,42 @@ describe('dispatchLocatorAction', () => {
await new Promise((resolve) => setTimeout(resolve, 0));
});

it.each([true, false])('refuses cancelled file upload before dispatch (direct input: %s)', async (direct) => {
const controller = new AbortController();
const { target, element, page } = stubUpload(direct);
element.evaluate.mockImplementation(async () => {
controller.abort();
return direct;
});
await expect(dispatchLocatorAction(
target,
{ kind: 'setInputFiles', paths: ['/tmp/a.txt'] },
{ timeoutMs: 1_000, signal: controller.signal },
lookup,
)).rejects.toMatchObject({ code: 'CANCELLED' });
expect(element.setInputFiles).not.toHaveBeenCalled();
expect(element.click).not.toHaveBeenCalled();
expect(page.waitForEvent).not.toHaveBeenCalled();
});

it('does not upload files when a chooser resolves after cancellation', async () => {
const controller = new AbortController();
const { target, element, page, chooser } = stubUpload(false);
let resolveChooser: (value: unknown) => void = () => undefined;
page.waitForEvent.mockImplementation(() => new Promise((resolve) => { resolveChooser = resolve; }));
const pending = dispatchLocatorAction(
target,
{ kind: 'setInputFiles', paths: ['/tmp/a.txt'] },
{ timeoutMs: 1_000, signal: controller.signal },
lookup,
);
await vi.waitFor(() => { expect(element.click).toHaveBeenCalledOnce(); });
controller.abort();
resolveChooser(chooser);
await expect(pending).rejects.toMatchObject({ code: 'CANCELLED' });
expect(chooser.setFiles).not.toHaveBeenCalled();
});

it('scrolls a node with a wheel gesture sized by its own box: the agent node scroll', async () => {
const { locator, target, wheel, frame } = stubLocator({ x: 0, y: 0, width: 400, height: 300 });
await dispatchLocatorAction(target, { kind: 'swipe', direction: 'down' }, OPERATION, lookup);
Expand Down
Loading