Skip to content
Closed
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
2 changes: 1 addition & 1 deletion config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type * as playwright from 'playwright';

export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf';
export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf' | 'vscode';

export type Config = {
/**
Expand Down
21 changes: 21 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ export class Context {
return tab;
}

async connectToWindow(connectionString: string) {
await this.closeBrowserContext();

this._browserContextFactory = {
async createContext() {
const browser = await playwright.chromium.connect(connectionString);
const params = new URL(connectionString).searchParams;
const contextOptions = JSON.parse(params.get('context-options') ?? '{}');
const context = browser.contexts()[0] ?? await (browser as any)._newContextForReuse(contextOptions);
return {
browserContext: context,
close: async () => {
await browser.close();
}
};
},
};

await this._ensureBrowserContext();
}

async ensureTab(): Promise<Tab> {
const { browserContext } = await this._ensureBrowserContext();
if (!this._currentTab)
Expand Down
2 changes: 2 additions & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import tabs from './tools/tabs.js';
import screenshot from './tools/screenshot.js';
import wait from './tools/wait.js';
import mouse from './tools/mouse.js';
import vscode from './tools/vscode.js';

import type { Tool } from './tools/tool.js';
import type { FullConfig } from './config.js';
Expand All @@ -49,6 +50,7 @@ export const allTools: Tool<any>[] = [
...snapshot,
...tabs,
...wait,
...vscode, // TODO: detect "vscode" capability via clientInfo
];

export function filteredTools(config: FullConfig) {
Expand Down
45 changes: 45 additions & 0 deletions src/tools/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { z } from 'zod';
import { defineTool } from './tool.js';

const connect = defineTool({
capability: 'vscode',

schema: {
name: 'browser_connect',
title: 'Connect to a Browser',
description: 'Connect to an open browser window using a connection string.',
inputSchema: z.object({
connectionString: z.string().describe('Connection string for an existing Playwright browser window, as provided by the playwright_browser_window tool or as included in test reports.'),
}),
type: 'readOnly',
},

handle: async (context, params, response) => {
// TODO: this should probably open a new context instead of reconnecting an existing one.
// in the future, connectionString will contain a path to a Playwright installation that we can require,
// to ensure we're using the same version of Playwright.
await context.connectToWindow(params.connectionString);
response.setIncludeSnapshot();
response.addCode(`// Connect to existing window with connection string`);
},
});

export default [
connect,
];
Loading