playwright-selector-finder
Version:
A tool for finding and interacting with elements using Playwright's vision locators
232 lines (231 loc) • 7.59 kB
JavaScript
;
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloseTool = exports.SaveAsPDFTool = exports.WaitTool = exports.PressKeyTool = exports.TypeTool = exports.HoverTool = exports.ClickTool = exports.GoForwardTool = exports.GoBackTool = exports.NavigateTool = void 0;
const zod_1 = require("zod");
exports.NavigateTool = {
schema: {
name: 'browser_navigate',
description: 'Navigate to a URL',
inputSchema: zod_1.z.object({
url: zod_1.z.string().describe('The URL to navigate to'),
}),
},
async handle(context, params) {
const { url } = params;
const page = await context.getPage();
await page.goto(url);
const title = await page.title();
return {
content: [{
type: 'text',
text: `Navigated to ${url}\nPage title: ${title}`
}]
};
}
};
exports.GoBackTool = {
schema: {
name: 'browser_go_back',
description: 'Go back to the previous page',
inputSchema: zod_1.z.object({}),
},
async handle(context) {
const page = await context.getPage();
await page.goBack();
const title = await page.title();
return {
content: [{
type: 'text',
text: `Navigated back\nPage title: ${title}`
}]
};
}
};
exports.GoForwardTool = {
schema: {
name: 'browser_go_forward',
description: 'Go forward to the next page',
inputSchema: zod_1.z.object({}),
},
async handle(context) {
const page = await context.getPage();
await page.goForward();
const title = await page.title();
return {
content: [{
type: 'text',
text: `Navigated forward\nPage title: ${title}`
}]
};
}
};
exports.ClickTool = {
schema: {
name: 'browser_click',
description: 'Click on an element',
inputSchema: zod_1.z.object({
element: zod_1.z.string().describe('Human-readable element description'),
timeout: zod_1.z.number().optional().describe('Maximum time to wait for the element'),
force: zod_1.z.boolean().optional().describe('Force click even if element is not visible')
}),
},
async handle(context, params) {
const { element, timeout, force } = params;
const page = await context.getPage();
const options = context.getOptions();
const locator = page.getByRole('generic', { name: element });
await locator.click({
timeout: timeout ?? options.visionTimeout,
force
});
return {
content: [{
type: 'text',
text: `Clicked on "${element}"`
}]
};
}
};
exports.HoverTool = {
schema: {
name: 'browser_hover',
description: 'Hover over an element',
inputSchema: zod_1.z.object({
element: zod_1.z.string().describe('Human-readable element description'),
timeout: zod_1.z.number().optional().describe('Maximum time to wait for the element')
}),
},
async handle(context, params) {
const { element, timeout } = params;
const page = await context.getPage();
const options = context.getOptions();
const locator = page.getByRole('generic', { name: element });
await locator.hover({
timeout: timeout ?? options.visionTimeout
});
return {
content: [{
type: 'text',
text: `Hovered over "${element}"`
}]
};
}
};
exports.TypeTool = {
schema: {
name: 'browser_type',
description: 'Type text into an element',
inputSchema: zod_1.z.object({
element: zod_1.z.string().describe('Human-readable element description'),
text: zod_1.z.string().describe('Text to type'),
timeout: zod_1.z.number().optional().describe('Maximum time to wait for the element'),
submit: zod_1.z.boolean().optional().describe('Whether to submit after typing')
}),
},
async handle(context, params) {
const { element, text, timeout, submit } = params;
const page = await context.getPage();
const options = context.getOptions();
const locator = page.getByRole('textbox', { name: element });
await locator.fill(text, {
timeout: timeout ?? options.visionTimeout
});
if (submit) {
await locator.press('Enter');
}
return {
content: [{
type: 'text',
text: `Typed "${text}" into "${element}"${submit ? ' and submitted' : ''}`
}]
};
}
};
exports.PressKeyTool = {
schema: {
name: 'browser_press_key',
description: 'Press a key on the keyboard',
inputSchema: zod_1.z.object({
key: zod_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
}),
},
async handle(context, params) {
const { key } = params;
const page = await context.getPage();
await page.keyboard.press(key);
return {
content: [{
type: 'text',
text: `Pressed key "${key}"`
}]
};
}
};
exports.WaitTool = {
schema: {
name: 'browser_wait',
description: 'Wait for a specified time in seconds',
inputSchema: zod_1.z.object({
time: zod_1.z.number().describe('The time to wait in seconds'),
}),
},
async handle(context, params) {
const { time } = params;
const page = await context.getPage();
await page.waitForTimeout(time * 1000);
return {
content: [{
type: 'text',
text: `Waited for ${time} seconds`
}]
};
}
};
exports.SaveAsPDFTool = {
schema: {
name: 'browser_save_as_pdf',
description: 'Save page as PDF',
inputSchema: zod_1.z.object({}),
},
async handle(context) {
const page = await context.getPage();
const buffer = await page.pdf();
return {
content: [{
type: 'application/pdf',
buffer: buffer.toString('base64')
}]
};
}
};
exports.CloseTool = {
schema: {
name: 'browser_close',
description: 'Close the browser',
inputSchema: zod_1.z.object({}),
},
async handle(context) {
await context.close();
return {
content: [{
type: 'text',
text: 'Browser closed'
}]
};
}
};