mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2026-02-01 20:23:38 +00:00
chore: allow reusing tab over cdp (#170)
Fixes https://github.com/microsoft/playwright-mcp/issues/164
This commit is contained in:
@@ -54,7 +54,7 @@ export class Context {
|
||||
|
||||
currentTab(): Tab {
|
||||
if (!this._currentTab)
|
||||
throw new Error('Navigate to a location to create a tab');
|
||||
throw new Error('No current snapshot available. Capture a snapshot of navigate to a new location first.');
|
||||
return this._currentTab;
|
||||
}
|
||||
|
||||
@@ -236,8 +236,8 @@ class Tab {
|
||||
});
|
||||
}
|
||||
|
||||
async runAndWaitWithSnapshot(callback: (tab: Tab) => Promise<void>, options?: RunOptions): Promise<ToolResult> {
|
||||
return await this.run(callback, {
|
||||
async runAndWaitWithSnapshot(callback: (snapshot: PageSnapshot) => Promise<void>, options?: RunOptions): Promise<ToolResult> {
|
||||
return await this.run(tab => callback(tab.lastSnapshot()), {
|
||||
captureSnapshot: true,
|
||||
waitForCompletion: true,
|
||||
...options,
|
||||
|
||||
@@ -28,7 +28,7 @@ const screenshot: Tool = {
|
||||
},
|
||||
|
||||
handle: async context => {
|
||||
const tab = context.currentTab();
|
||||
const tab = await context.ensureTab();
|
||||
const screenshot = await tab.page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
|
||||
return {
|
||||
content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: 'image/jpeg' }],
|
||||
|
||||
@@ -29,7 +29,8 @@ const snapshot: Tool = {
|
||||
},
|
||||
|
||||
handle: async context => {
|
||||
return await context.currentTab().run(async () => {}, { captureSnapshot: true });
|
||||
const tab = await context.ensureTab();
|
||||
return await tab.run(async () => {}, { captureSnapshot: true });
|
||||
},
|
||||
};
|
||||
|
||||
@@ -48,8 +49,8 @@ const click: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = elementSchema.parse(params);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async tab => {
|
||||
const locator = tab.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async snapshot => {
|
||||
const locator = snapshot.refLocator(validatedParams.ref);
|
||||
await locator.click();
|
||||
}, {
|
||||
status: `Clicked "${validatedParams.element}"`,
|
||||
@@ -74,9 +75,9 @@ const drag: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = dragSchema.parse(params);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async tab => {
|
||||
const startLocator = tab.lastSnapshot().refLocator(validatedParams.startRef);
|
||||
const endLocator = tab.lastSnapshot().refLocator(validatedParams.endRef);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async snapshot => {
|
||||
const startLocator = snapshot.refLocator(validatedParams.startRef);
|
||||
const endLocator = snapshot.refLocator(validatedParams.endRef);
|
||||
await startLocator.dragTo(endLocator);
|
||||
}, {
|
||||
status: `Dragged "${validatedParams.startElement}" to "${validatedParams.endElement}"`,
|
||||
@@ -94,8 +95,8 @@ const hover: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = elementSchema.parse(params);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async tab => {
|
||||
const locator = tab.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async snapshot => {
|
||||
const locator = snapshot.refLocator(validatedParams.ref);
|
||||
await locator.hover();
|
||||
}, {
|
||||
status: `Hovered over "${validatedParams.element}"`,
|
||||
@@ -119,8 +120,8 @@ const type: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = typeSchema.parse(params);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async tab => {
|
||||
const locator = tab.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async snapshot => {
|
||||
const locator = snapshot.refLocator(validatedParams.ref);
|
||||
if (validatedParams.slowly)
|
||||
await locator.pressSequentially(validatedParams.text);
|
||||
else
|
||||
@@ -147,8 +148,8 @@ const selectOption: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = selectOptionSchema.parse(params);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async tab => {
|
||||
const locator = tab.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentTab().runAndWaitWithSnapshot(async snapshot => {
|
||||
const locator = snapshot.refLocator(validatedParams.ref);
|
||||
await locator.selectOption(validatedParams.values);
|
||||
}, {
|
||||
status: `Selected option in "${validatedParams.element}"`,
|
||||
|
||||
@@ -89,7 +89,7 @@ const closeTab: ToolFactory = captureSnapshot => ({
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = closeTabSchema.parse(params);
|
||||
await context.closeTab(validatedParams.index);
|
||||
const currentTab = await context.currentTab();
|
||||
const currentTab = context.currentTab();
|
||||
if (currentTab)
|
||||
return await currentTab.run(async () => {}, { captureSnapshot });
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user