UNPKG

play-ai

Version:

Automate Playwright tests using OpenAI integration

83 lines (78 loc) 3.49 kB
import { TestType, Page } from '@playwright/test'; type Test = TestType<any, any>; /** * Options for configuring a step. * * @typedef {Object} StepOptions * @property {boolean} [debug] - Whether to enable debug mode. * @property {string} [model] - The model to use. * @property {string} [openaiApiKey] - The OpenAI API key. * @property {string} [openaiBaseUrl] - The base URL for OpenAI API. * @property {Object} [openaiDefaultQuery] - The default query parameters for OpenAI API. * @property {Object} [openaiDefaultHeaders] - The default headers for OpenAI API. */ type StepOptions = { debug?: boolean; model?: string; openaiApiKey?: string; openaiBaseUrl?: string; openaiDefaultQuery?: {}; openaiDefaultHeaders?: {}; }; /** * Executes a task or a series of tasks using Playwright and OpenAI integration. * * This function allows you to execute a single task or an array of tasks on a Playwright `page` using OpenAI's capabilities. * It can be used within a Playwright test context to perform automated actions and assertions. * * @param task - A single task or an array of tasks to be executed. Each task is a string describing the action to be performed. * @param config - Configuration object containing the Playwright `page` and optional `test` context. * @param config.page - The Playwright `page` object where the tasks will be executed. * @param config.test - Optional Playwright `test` context for running the tasks within a test step. * @param options - Optional configuration for the task execution, including OpenAI settings. * @param options.model - The OpenAI model to be used for task execution (default is "gpt-4o"). * @param options.debug - Boolean flag to enable debugging mode (default is false). * @param options.openaiApiKey - The API key for accessing OpenAI services. * @param options.openaiBaseUrl - The base URL for OpenAI API requests. * @param options.openaiDefaultQuery - Default query parameters for OpenAI requests. * @param options.openaiDefaultHeaders - Default headers for OpenAI requests. * @returns A promise that resolves with the result of the task execution. The result can include assertions, queries, or other outputs. * @throws {UnimplementedError} If the required `page` argument is missing in the config. * @throws {Error} If the task length exceeds the maximum allowed characters. * * @example * ```typescript * import { play } from "./play"; * import { Page, Test } from "playwright"; * * const page: Page = ...; // Initialize Playwright page * const test: Test = ...; // Initialize Playwright test context * * await play("Type 'standard_user' in the Username field", { page, test }); * await play("Click the Login button", { page, test }); * ``` */ declare const play: (task: string | string[], config: { page: Page; test: Test; }, options?: StepOptions) => Promise<any>; /** * Base class for all custom errors in the Play AI framework. * * This abstract class serves as the base for all custom error types in the Play AI framework. It extends the built-in `Error` class * and provides a consistent structure for error handling within the framework. * * @export * @abstract * @class PlayAIError * @extends {Error} */ declare abstract class PlayAIError extends Error { /** * Creates an instance of PlayAIError. * * @param {string} [message] - The error message. */ constructor(message?: string); } export { PlayAIError, play };