@auto-browse/auto-browse
Version:
AI-powered browser automation
66 lines (65 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.browser_page_assert = void 0;
const tools_1 = require("@langchain/core/tools");
const zod_1 = require("zod");
const test_1 = require("@playwright/test");
const utils_1 = require("./utils");
const context_1 = require("../browser/context");
/**
* Schema for page-level assertions
*/
const pageAssertSchema = zod_1.z.object({
assertion: zod_1.z
.string()
.describe('Type of assertion to perform (e.g., "hasTitle", "hasURL", "isOK")'),
expected: zod_1.z
.string()
.optional()
.describe("Expected value for title/URL assertions"),
});
exports.browser_page_assert = (0, tools_1.tool)(async ({ assertion, expected }) => {
try {
console.log(`[Page Assert Tool] Starting operation:`, {
assertion,
expected,
});
const result = await test_1.test.step(`Assert page ${assertion}${expected ? ` equals "${expected}"` : ""}`, async () => {
return await (0, utils_1.runAndWait)(context_1.context, `Asserted page ${assertion}${expected ? ` equals "${expected}"` : ""}`, async () => {
const page = context_1.context.existingPage();
console.log(`[Page Assert Tool] Performing assertion`);
// Create descriptive message for both success and error cases
const message = `${assertion} ${expected || ""}`;
switch (assertion.toLowerCase()) {
case "hastitle":
if (!expected)
throw new Error("Expected value required for hasTitle assertion");
await (0, test_1.expect)(page, message).toHaveTitle(expected);
return message;
case "hasurl":
if (!expected)
throw new Error("Expected value required for hasURL assertion");
await (0, test_1.expect)(page, message).toHaveURL(expected);
return message;
case "isok": {
// TODO: Implement response tracking in context
throw new Error("Response assertions not yet implemented");
}
default:
throw new Error(`Unsupported page assertion type: ${assertion}`);
}
}, true);
});
console.log(`[Page Assert Tool] Operation completed`);
return result;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
console.error(`[Page Assert Tool] Error: ${errorMessage}`);
return errorMessage;
}
}, {
name: "page_assert",
description: "Assert conditions on the page or response using Playwright's assertions",
schema: pageAssertSchema,
});