chore: prep for multiple pages in context (#124)
This commit is contained in:
@@ -37,10 +37,10 @@ export const navigate: ToolFactory = captureSnapshot => ({
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = navigateSchema.parse(params);
|
||||
await context.createPage();
|
||||
return await context.run(async page => {
|
||||
await page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
|
||||
return await context.currentPage().run(async page => {
|
||||
await page.page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
|
||||
// Cap load event to 5 seconds, the page is operational at this point.
|
||||
await page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
|
||||
await page.page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
|
||||
}, {
|
||||
status: `Navigated to ${validatedParams.url}`,
|
||||
captureSnapshot,
|
||||
@@ -57,8 +57,8 @@ export const goBack: ToolFactory = snapshot => ({
|
||||
inputSchema: zodToJsonSchema(goBackSchema),
|
||||
},
|
||||
handle: async context => {
|
||||
return await context.runAndWait(async page => {
|
||||
await page.goBack();
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
await page.page.goBack();
|
||||
}, {
|
||||
status: 'Navigated back',
|
||||
captureSnapshot: snapshot,
|
||||
@@ -75,8 +75,8 @@ export const goForward: ToolFactory = snapshot => ({
|
||||
inputSchema: zodToJsonSchema(goForwardSchema),
|
||||
},
|
||||
handle: async context => {
|
||||
return await context.runAndWait(async page => {
|
||||
await page.goForward();
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
await page.page.goForward();
|
||||
}, {
|
||||
status: 'Navigated forward',
|
||||
captureSnapshot: snapshot,
|
||||
@@ -118,8 +118,8 @@ export const pressKey: (captureSnapshot: boolean) => Tool = captureSnapshot => (
|
||||
},
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = pressKeySchema.parse(params);
|
||||
return await context.runAndWait(async page => {
|
||||
await page.keyboard.press(validatedParams.key);
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
await page.page.keyboard.press(validatedParams.key);
|
||||
}, {
|
||||
status: `Pressed key ${validatedParams.key}`,
|
||||
captureSnapshot,
|
||||
@@ -136,9 +136,9 @@ export const pdf: Tool = {
|
||||
inputSchema: zodToJsonSchema(pdfSchema),
|
||||
},
|
||||
handle: async context => {
|
||||
const page = context.existingPage();
|
||||
const page = context.currentPage();
|
||||
const fileName = path.join(os.tmpdir(), sanitizeForFilePath(`page-${new Date().toISOString()}`)) + '.pdf';
|
||||
await page.pdf({ path: fileName });
|
||||
await page.page.pdf({ path: fileName });
|
||||
return {
|
||||
content: [{
|
||||
type: 'text',
|
||||
@@ -179,8 +179,9 @@ export const chooseFile: ToolFactory = captureSnapshot => ({
|
||||
},
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = chooseFileSchema.parse(params);
|
||||
return await context.runAndWait(async () => {
|
||||
await context.submitFileChooser(validatedParams.paths);
|
||||
const page = context.currentPage();
|
||||
return await page.runAndWait(async () => {
|
||||
await page.submitFileChooser(validatedParams.paths);
|
||||
}, {
|
||||
status: `Chose files ${validatedParams.paths.join(', ')}`,
|
||||
captureSnapshot,
|
||||
|
||||
@@ -27,8 +27,8 @@ export const screenshot: Tool = {
|
||||
},
|
||||
|
||||
handle: async context => {
|
||||
const page = context.existingPage();
|
||||
const screenshot = await page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
|
||||
const page = context.currentPage();
|
||||
const screenshot = await page.page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
|
||||
return {
|
||||
content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: 'image/jpeg' }],
|
||||
};
|
||||
@@ -53,8 +53,8 @@ export const moveMouse: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = moveMouseSchema.parse(params);
|
||||
const page = context.existingPage();
|
||||
await page.mouse.move(validatedParams.x, validatedParams.y);
|
||||
const page = context.currentPage();
|
||||
await page.page.mouse.move(validatedParams.x, validatedParams.y);
|
||||
return {
|
||||
content: [{ type: 'text', text: `Moved mouse to (${validatedParams.x}, ${validatedParams.y})` }],
|
||||
};
|
||||
@@ -74,11 +74,11 @@ export const click: Tool = {
|
||||
},
|
||||
|
||||
handle: async (context, params) => {
|
||||
return await context.runAndWait(async page => {
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
const validatedParams = clickSchema.parse(params);
|
||||
await page.mouse.move(validatedParams.x, validatedParams.y);
|
||||
await page.mouse.down();
|
||||
await page.mouse.up();
|
||||
await page.page.mouse.move(validatedParams.x, validatedParams.y);
|
||||
await page.page.mouse.down();
|
||||
await page.page.mouse.up();
|
||||
}, {
|
||||
status: 'Clicked mouse',
|
||||
});
|
||||
@@ -101,11 +101,11 @@ export const drag: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = dragSchema.parse(params);
|
||||
return await context.runAndWait(async page => {
|
||||
await page.mouse.move(validatedParams.startX, validatedParams.startY);
|
||||
await page.mouse.down();
|
||||
await page.mouse.move(validatedParams.endX, validatedParams.endY);
|
||||
await page.mouse.up();
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
await page.page.mouse.move(validatedParams.startX, validatedParams.startY);
|
||||
await page.page.mouse.down();
|
||||
await page.page.mouse.move(validatedParams.endX, validatedParams.endY);
|
||||
await page.page.mouse.up();
|
||||
}, {
|
||||
status: `Dragged mouse from (${validatedParams.startX}, ${validatedParams.startY}) to (${validatedParams.endX}, ${validatedParams.endY})`,
|
||||
});
|
||||
@@ -126,10 +126,10 @@ export const type: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = typeSchema.parse(params);
|
||||
return await context.runAndWait(async page => {
|
||||
await page.keyboard.type(validatedParams.text);
|
||||
return await context.currentPage().runAndWait(async page => {
|
||||
await page.page.keyboard.type(validatedParams.text);
|
||||
if (validatedParams.submit)
|
||||
await page.keyboard.press('Enter');
|
||||
await page.page.keyboard.press('Enter');
|
||||
}, {
|
||||
status: `Typed text "${validatedParams.text}"`,
|
||||
});
|
||||
|
||||
@@ -28,7 +28,7 @@ export const snapshot: Tool = {
|
||||
},
|
||||
|
||||
handle: async context => {
|
||||
return await context.run(async () => {}, { captureSnapshot: true });
|
||||
return await context.currentPage().run(async () => {}, { captureSnapshot: true });
|
||||
},
|
||||
};
|
||||
|
||||
@@ -46,8 +46,8 @@ export const click: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = elementSchema.parse(params);
|
||||
return await context.runAndWaitWithSnapshot(async () => {
|
||||
const locator = context.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentPage().runAndWaitWithSnapshot(async page => {
|
||||
const locator = page.lastSnapshot().refLocator(validatedParams.ref);
|
||||
await locator.click();
|
||||
}, {
|
||||
status: `Clicked "${validatedParams.element}"`,
|
||||
@@ -71,9 +71,9 @@ export const drag: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = dragSchema.parse(params);
|
||||
return await context.runAndWaitWithSnapshot(async () => {
|
||||
const startLocator = context.lastSnapshot().refLocator(validatedParams.startRef);
|
||||
const endLocator = context.lastSnapshot().refLocator(validatedParams.endRef);
|
||||
return await context.currentPage().runAndWaitWithSnapshot(async page => {
|
||||
const startLocator = page.lastSnapshot().refLocator(validatedParams.startRef);
|
||||
const endLocator = page.lastSnapshot().refLocator(validatedParams.endRef);
|
||||
await startLocator.dragTo(endLocator);
|
||||
}, {
|
||||
status: `Dragged "${validatedParams.startElement}" to "${validatedParams.endElement}"`,
|
||||
@@ -90,8 +90,8 @@ export const hover: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = elementSchema.parse(params);
|
||||
return context.runAndWaitWithSnapshot(async () => {
|
||||
const locator = context.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentPage().runAndWaitWithSnapshot(async page => {
|
||||
const locator = page.lastSnapshot().refLocator(validatedParams.ref);
|
||||
await locator.hover();
|
||||
}, {
|
||||
status: `Hovered over "${validatedParams.element}"`,
|
||||
@@ -114,8 +114,8 @@ export const type: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = typeSchema.parse(params);
|
||||
return await context.runAndWaitWithSnapshot(async () => {
|
||||
const locator = context.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentPage().runAndWaitWithSnapshot(async page => {
|
||||
const locator = page.lastSnapshot().refLocator(validatedParams.ref);
|
||||
if (validatedParams.slowly)
|
||||
await locator.pressSequentially(validatedParams.text);
|
||||
else
|
||||
@@ -141,8 +141,8 @@ export const selectOption: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = selectOptionSchema.parse(params);
|
||||
return await context.runAndWaitWithSnapshot(async () => {
|
||||
const locator = context.lastSnapshot().refLocator(validatedParams.ref);
|
||||
return await context.currentPage().runAndWaitWithSnapshot(async page => {
|
||||
const locator = page.lastSnapshot().refLocator(validatedParams.ref);
|
||||
await locator.selectOption(validatedParams.values);
|
||||
}, {
|
||||
status: `Selected option in "${validatedParams.element}"`,
|
||||
@@ -163,9 +163,9 @@ export const screenshot: Tool = {
|
||||
|
||||
handle: async (context, params) => {
|
||||
const validatedParams = screenshotSchema.parse(params);
|
||||
const page = context.existingPage();
|
||||
const page = context.currentPage();
|
||||
const options: playwright.PageScreenshotOptions = validatedParams.raw ? { type: 'png', scale: 'css' } : { type: 'jpeg', quality: 50, scale: 'css' };
|
||||
const screenshot = await page.screenshot(options);
|
||||
const screenshot = await page.page.screenshot(options);
|
||||
return {
|
||||
content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: validatedParams.raw ? 'image/png' : 'image/jpeg' }],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user