feat: browser_choose_file (#52)
Resolves https://github.com/microsoft/playwright-mcp/issues/31. Prompt used for testing: ``` upload test.txt to dropbox ``` This won't work for asynchronously opened filechoosers, but let's start with the synchronous variant. I also tested with including the file chooser open state in the snapshot, but that didn't give better results. I also tested with OneDrive and WeTransfer, but somehow our ARIA snapshots for those pages are missing some elements that are crucial to locate the upload buttons.
This commit is contained in:
@@ -23,6 +23,7 @@ export class Context {
|
||||
private _page: playwright.Page | undefined;
|
||||
private _console: playwright.ConsoleMessage[] = [];
|
||||
private _createPagePromise: Promise<playwright.Page> | undefined;
|
||||
private _fileChooser: playwright.FileChooser | undefined;
|
||||
private _lastSnapshotFrames: playwright.FrameLocator[] = [];
|
||||
|
||||
constructor(userDataDir: string, launchOptions?: playwright.LaunchOptions) {
|
||||
@@ -41,6 +42,7 @@ export class Context {
|
||||
this._console.length = 0;
|
||||
});
|
||||
page.on('close', () => this._onPageClose());
|
||||
page.on('filechooser', chooser => this._fileChooser = chooser);
|
||||
page.setDefaultNavigationTimeout(60000);
|
||||
page.setDefaultTimeout(5000);
|
||||
this._page = page;
|
||||
@@ -58,6 +60,7 @@ export class Context {
|
||||
this._createPagePromise = undefined;
|
||||
this._browser = undefined;
|
||||
this._page = undefined;
|
||||
this._fileChooser = undefined;
|
||||
this._console.length = 0;
|
||||
}
|
||||
|
||||
@@ -77,6 +80,21 @@ export class Context {
|
||||
await this._page.close();
|
||||
}
|
||||
|
||||
async submitFileChooser(paths: string[]) {
|
||||
if (!this._fileChooser)
|
||||
throw new Error('No file chooser visible');
|
||||
await this._fileChooser.setFiles(paths);
|
||||
this._fileChooser = undefined;
|
||||
}
|
||||
|
||||
hasFileChooser() {
|
||||
return !!this._fileChooser;
|
||||
}
|
||||
|
||||
clearFileChooser() {
|
||||
this._fileChooser = undefined;
|
||||
}
|
||||
|
||||
private async _createPage(): Promise<{ browser?: playwright.Browser, page: playwright.Page }> {
|
||||
if (process.env.PLAYWRIGHT_WS_ENDPOINT) {
|
||||
const url = new URL(process.env.PLAYWRIGHT_WS_ENDPOINT);
|
||||
|
||||
Reference in New Issue
Block a user