Skip to content

Commit 996f442

Browse files
committed
refactor: move waitForEventsAfterAction from McpContext to McpPage, update all callers
1 parent a7e1f20 commit 996f442

4 files changed

Lines changed: 22 additions & 47 deletions

File tree

src/McpContext.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
type InstalledExtension,
4848
} from './utils/ExtensionRegistry.js';
4949
import {saveTemporaryFile} from './utils/files.js';
50-
import {getNetworkMultiplierFromString, WaitForHelper} from './WaitForHelper.js';
50+
import {getNetworkMultiplierFromString} from './WaitForHelper.js';
5151

5252
interface McpContextOptions {
5353
// Whether the DevTools windows are exposed as pages for debugging of DevTools.
@@ -824,31 +824,6 @@ export class McpContext implements Context {
824824
return this.#traceResults;
825825
}
826826

827-
getWaitForHelper(
828-
page: Page,
829-
cpuMultiplier: number,
830-
networkMultiplier: number,
831-
) {
832-
return new WaitForHelper(page, cpuMultiplier, networkMultiplier);
833-
}
834-
835-
waitForEventsAfterAction(
836-
action: () => Promise<unknown>,
837-
options?: {timeout?: number},
838-
): Promise<void> {
839-
const page = this.#getSelectedMcpPage();
840-
const cpuMultiplier = page.cpuThrottlingRate;
841-
const networkMultiplier = getNetworkMultiplierFromString(
842-
page.networkConditions,
843-
);
844-
const waitForHelper = this.getWaitForHelper(
845-
page.pptrPage,
846-
cpuMultiplier,
847-
networkMultiplier,
848-
);
849-
return waitForHelper.waitForEventsAfterAction(action, options);
850-
}
851-
852827
getNetworkRequestStableId(request: HTTPRequest): number {
853828
return this.#networkCollector.getIdForResource(request);
854829
}

src/tools/input.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export const click = definePageTool({
5858
dblClick: dblClickSchema,
5959
includeSnapshot: includeSnapshotSchema,
6060
},
61-
handler: async (request, response, context) => {
61+
handler: async (request, response, _context) => {
6262
const uid = request.params.uid;
6363
const handle = await request.page.getElementByUid(uid);
6464
try {
65-
await context.waitForEventsAfterAction(async () => {
65+
await request.page.waitForEventsAfterAction(async () => {
6666
await handle.asLocator().click({
6767
count: request.params.dblClick ? 2 : 1,
6868
});
@@ -97,9 +97,9 @@ export const clickAt = definePageTool({
9797
dblClick: dblClickSchema,
9898
includeSnapshot: includeSnapshotSchema,
9999
},
100-
handler: async (request, response, context) => {
100+
handler: async (request, response) => {
101101
const page = request.page;
102-
await context.waitForEventsAfterAction(async () => {
102+
await page.waitForEventsAfterAction(async () => {
103103
await page.pptrPage.mouse.click(request.params.x, request.params.y, {
104104
clickCount: request.params.dblClick ? 2 : 1,
105105
});
@@ -130,11 +130,11 @@ export const hover = definePageTool({
130130
),
131131
includeSnapshot: includeSnapshotSchema,
132132
},
133-
handler: async (request, response, context) => {
133+
handler: async (request, response, _context) => {
134134
const uid = request.params.uid;
135135
const handle = await request.page.getElementByUid(uid);
136136
try {
137-
await context.waitForEventsAfterAction(async () => {
137+
await request.page.waitForEventsAfterAction(async () => {
138138
await handle.asLocator().hover();
139139
});
140140
response.appendResponseLine(`Successfully hovered over the element`);
@@ -235,7 +235,7 @@ export const fill = definePageTool({
235235
},
236236
handler: async (request, response, context) => {
237237
const page = request.page;
238-
await context.waitForEventsAfterAction(async () => {
238+
await page.waitForEventsAfterAction(async () => {
239239
await fillFormElement(
240240
request.params.uid,
241241
request.params.value,
@@ -261,9 +261,9 @@ export const typeText = definePageTool({
261261
text: zod.string().describe('The text to type'),
262262
submitKey: submitKeySchema,
263263
},
264-
handler: async (request, response, context) => {
264+
handler: async (request, response) => {
265265
const page = request.page;
266-
await context.waitForEventsAfterAction(async () => {
266+
await page.waitForEventsAfterAction(async () => {
267267
await page.pptrPage.keyboard.type(request.params.text);
268268
if (request.params.submitKey) {
269269
await page.pptrPage.keyboard.press(
@@ -289,13 +289,13 @@ export const drag = definePageTool({
289289
to_uid: zod.string().describe('The uid of the element to drop into'),
290290
includeSnapshot: includeSnapshotSchema,
291291
},
292-
handler: async (request, response, context) => {
292+
handler: async (request, response) => {
293293
const fromHandle = await request.page.getElementByUid(
294294
request.params.from_uid,
295295
);
296296
const toHandle = await request.page.getElementByUid(request.params.to_uid);
297297
try {
298-
await context.waitForEventsAfterAction(async () => {
298+
await request.page.waitForEventsAfterAction(async () => {
299299
await fromHandle.drag(toHandle);
300300
await new Promise(resolve => setTimeout(resolve, 50));
301301
await toHandle.drop(fromHandle);
@@ -332,7 +332,7 @@ export const fillForm = definePageTool({
332332
handler: async (request, response, context) => {
333333
const page = request.page;
334334
for (const element of request.params.elements) {
335-
await context.waitForEventsAfterAction(async () => {
335+
await page.waitForEventsAfterAction(async () => {
336336
await fillFormElement(
337337
element.uid,
338338
element.value,
@@ -413,12 +413,12 @@ export const pressKey = definePageTool({
413413
),
414414
includeSnapshot: includeSnapshotSchema,
415415
},
416-
handler: async (request, response, context) => {
416+
handler: async (request, response) => {
417417
const page = request.page;
418418
const tokens = parseKey(request.params.key);
419419
const [key, ...modifiers] = tokens;
420420

421-
await context.waitForEventsAfterAction(async () => {
421+
await page.waitForEventsAfterAction(async () => {
422422
for (const modifier of modifiers) {
423423
await page.pptrPage.keyboard.down(modifier);
424424
}

src/tools/pages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const newPage = defineTool({
119119
request.params.isolatedContext,
120120
);
121121

122-
await context.waitForEventsAfterAction(
122+
await page.waitForEventsAfterAction(
123123
async () => {
124124
await page.pptrPage.goto(request.params.url, {
125125
timeout: request.params.timeout,
@@ -206,7 +206,7 @@ export const navigatePage = definePageTool({
206206
page.pptrPage.on('dialog', dialogHandler);
207207

208208
try {
209-
await context.waitForEventsAfterAction(
209+
await page.waitForEventsAfterAction(
210210
async () => {
211211
switch (request.params.type) {
212212
case 'url':

src/tools/script.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {Frame, JSHandle, Page, WebWorker} from '../third_party/index.js';
99
import type {ExtensionServiceWorker} from '../types.js';
1010

1111
import {ToolCategory} from './categories.js';
12-
import type {Context, Response} from './ToolDefinition.js';
12+
import type {Context, ContextPage, Response} from './ToolDefinition.js';
1313
import {defineTool, pageIdSchema} from './ToolDefinition.js';
1414

1515
export type Evaluatable = Page | Frame | WebWorker;
@@ -77,7 +77,7 @@ Example with arguments: \`(el) => {
7777
}
7878

7979
const worker = await getWebWorker(context, serviceWorkerId);
80-
await performEvaluation(worker, fnString, [], response, context);
80+
await performEvaluation(worker, fnString, [], response, context.getSelectedMcpPage());
8181
return;
8282
}
8383

@@ -97,7 +97,7 @@ Example with arguments: \`(el) => {
9797

9898
const evaluatable = await getPageOrFrame(page, frames);
9999

100-
await performEvaluation(evaluatable, fnString, args, response, context);
100+
await performEvaluation(evaluatable, fnString, args, response, mcpPage);
101101
} finally {
102102
void Promise.allSettled(args.map(arg => arg.dispose()));
103103
}
@@ -110,11 +110,11 @@ const performEvaluation = async (
110110
fnString: string,
111111
args: Array<JSHandle<unknown>>,
112112
response: Response,
113-
context: Context,
113+
page: ContextPage,
114114
) => {
115115
const fn = await evaluatable.evaluateHandle(`(${fnString})`);
116116
try {
117-
await context.waitForEventsAfterAction(async () => {
117+
await page.waitForEventsAfterAction(async () => {
118118
const result = await evaluatable.evaluate(
119119
async (fn, ...args) => {
120120
// @ts-expect-error no types for function fn

0 commit comments

Comments
 (0)