Skip to content

Commit

Permalink
chore: implement BiDi set files
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Feb 26, 2024
1 parent 414f433 commit 4a88094
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
8 changes: 5 additions & 3 deletions packages/puppeteer-core/src/bidi/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';

import {ElementHandle, type AutofillData} from '../api/ElementHandle.js';
import {UnsupportedOperation} from '../common/Errors.js';
import {throwIfDisposed} from '../util/decorators.js';

import type {BidiFrame} from './Frame.js';
Expand Down Expand Up @@ -90,7 +89,10 @@ export class BidiElementHandle<
return null;
}

override uploadFile(this: ElementHandle<HTMLInputElement>): never {
throw new UnsupportedOperation();
override async uploadFile(
this: BidiElementHandle<HTMLInputElement>,
...filePaths: string[]
): Promise<void> {
await this.frame.setFiles(this, filePaths);
}
}
10 changes: 10 additions & 0 deletions packages/puppeteer-core/src/bidi/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {BidiCdpSession} from './CDPSession.js';
import type {BrowsingContext} from './core/BrowsingContext.js';
import {BidiDeserializer} from './Deserializer.js';
import {BidiDialog} from './Dialog.js';
import type {BidiElementHandle} from './ElementHandle.js';
import {ExposeableFunction} from './ExposedFunction.js';
import {BidiHTTPRequest, requests} from './HTTPRequest.js';
import type {BidiHTTPResponse} from './HTTPResponse.js';
Expand Down Expand Up @@ -519,6 +520,15 @@ export class BidiFrame extends Frame {
concurrency,
});
}

@throwIfDetached
async setFiles(element: BidiElementHandle, files: string[]): Promise<void> {
await this.browsingContext.setFiles(
// SAFETY: ElementHandles are always remote references.
element.remoteValue() as Bidi.Script.SharedReference,
files
);
}
}

function isConsoleLogEntry(
Expand Down
15 changes: 15 additions & 0 deletions packages/puppeteer-core/src/bidi/core/BrowsingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,21 @@ export class BrowsingContext extends EventEmitter<{
});
}

@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async setFiles(
element: Bidi.Script.SharedReference,
files: string[]
): Promise<void> {
await this.#session.send('input.setFiles', {
context: this.id,
element,
files,
});
}

[disposeSymbol](): void {
this.#reason ??=
'Browsing context already closed, probably because the user context closed.';
Expand Down
4 changes: 4 additions & 0 deletions packages/puppeteer-core/src/bidi/core/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export interface Commands {
params: Bidi.Input.ReleaseActionsParameters;
returnType: Bidi.EmptyResult;
};
'input.setFiles': {
params: Bidi.Input.SetFilesParameters;
returnType: Bidi.EmptyResult;
};

'permissions.setPermission': {
params: Bidi.Permissions.SetPermissionParameters;
Expand Down
6 changes: 0 additions & 6 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -1728,12 +1728,6 @@
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[input.spec] input tests input should upload the file",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[input.spec] input tests input should upload the file",
"platforms": ["darwin", "linux", "win32"],
Expand Down

0 comments on commit 4a88094

Please sign in to comment.