@auto-browse/auto-browse
Version:
AI-powered browser automation
74 lines (73 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.browser_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 assertions with descriptions for the AI model
*/
const assertSchema = zod_1.z.object({
element: zod_1.z
.string()
.describe("Human-readable element description for the target element"),
ref: zod_1.z
.string()
.describe("Element reference from page snapshot to locate the element"),
assertion: zod_1.z
.string()
.describe('Type of assertion to perform (e.g., "isVisible", "hasText", "isEnabled", "isChecked")'),
expected: zod_1.z
.string()
.optional()
.describe("Expected value for text assertions"),
});
exports.browser_assert = (0, tools_1.tool)(async ({ element, ref, assertion, expected }) => {
try {
console.log(`[Assert Tool] Starting operation:`, {
element,
ref,
assertion,
expected,
});
const result = await test_1.test.step(`Assert "${element}" ${assertion}${expected ? ` equals "${expected}"` : ""}`, async () => {
return await (0, utils_1.runAndWait)(context_1.context, `Asserted "${element}" ${assertion}${expected ? ` equals "${expected}"` : ""}`, async () => {
const locator = context_1.context.refLocator(ref);
console.log(`[Assert Tool] Performing assertion`);
// Create descriptive message for both success and error cases
const message = `${element} ${assertion} ${expected || ""}`;
switch (assertion.toLowerCase()) {
case "isvisible":
await (0, test_1.expect)(locator, message).toBeVisible();
return message;
case "hastext":
if (!expected)
throw new Error("Expected value required for hasText assertion");
await (0, test_1.expect)(locator, message).toHaveText(expected);
return message;
case "isenabled":
await (0, test_1.expect)(locator, message).toBeEnabled();
return message;
case "ischecked":
await (0, test_1.expect)(locator, message).toBeChecked();
return message;
default:
throw new Error(`Unsupported assertion type: ${assertion}`);
}
}, true);
});
console.log(`[Assert Tool] Operation completed`);
return result;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
console.error(`[Assert Tool] Error: ${errorMessage}`);
return errorMessage;
}
}, {
name: "assert",
description: "Assert conditions on elements using Playwright's assertions",
schema: assertSchema,
});