diff --git a/README.md b/README.md index bb17c69..3630d78 100644 --- a/README.md +++ b/README.md @@ -43,12 +43,37 @@ test("auto Playwright example", async ({ page }) => { ## Usage -At minimum, the `auto` function requires a _plain text prompt_ and an _argument_ that contains your `page` and `test` objects. +At minimum, the `auto` function requires a _plain text prompt_ and an _argument_ that contains your `page` and `test` (optional) objects. ```ts auto("", { page, test }); ``` +### Browser automation + +Running without the `test` parameter: + +```ts +import { chromium } from "playwright"; +import { auto } from "auto-playwright"; + +(async () => { + const browser = await chromium.launch({ headless: true }); + const context = await browser.newContext(); + const page = await context.newPage(); + // Navigate to a website + await page.goto("https://www.example.com"); + + // `auto` can query data + // In this case, the result is plain-text contents of the header + const res = await auto("get the header text", { page }); + + // use res.query to get a query result. + console.log(res); + await page.close(); +})(); +``` + ### Debug You may pass a `debug` attribute as the third parameter to the `auto` function. This will print the prompt and the commands executed by OpenAI. diff --git a/src/auto.ts b/src/auto.ts index 1f1f838..2b9bc06 100644 --- a/src/auto.ts +++ b/src/auto.ts @@ -6,34 +6,23 @@ import { getSnapshot } from "./getSnapshot"; export const auto = async ( task: string, - config: { page: Page; test: Test }, + config: { page: Page; test?: Test }, options?: StepOptions ): Promise => { - if (!config || !config.page || !config.test) { + if (!config || !config.page) { throw Error( - "The auto() function is missing the required `{ page, test }` argument." + "The auto() function is missing the required `{ page }` argument." ); } - const { test, page } = config as { page: Page; test: Test }; + const { test, page } = config as { page: Page; test?: Test }; - return test.step(`auto-playwright.ai '${task}'`, async () => { - if (task.length > MAX_TASK_CHARS) { - throw new Error( - `Provided task string is too long, max length is ${MAX_TASK_CHARS} chars.` - ); - } + if (!test) { + return await runTask(task, page, options); + } - const result = await completeTask(page, { - task, - snapshot: await getSnapshot(page), - options: options - ? { - model: options.model ?? "gpt-4-1106-preview", - debug: options.debug ?? false, - } - : undefined, - }); + return test.step(`auto-playwright.ai '${task}'`, async () => { + const result = await runTask(task, page, options); if (result.errorMessage) { throw new UnimplementedError(result.errorMessage); @@ -50,3 +39,27 @@ export const auto = async ( return undefined; }); }; + +async function runTask( + task: string, + page: Page, + options: StepOptions | undefined +) { + if (task.length > MAX_TASK_CHARS) { + throw new Error( + `Provided task string is too long, max length is ${MAX_TASK_CHARS} chars.` + ); + } + + const result = await completeTask(page, { + task, + snapshot: await getSnapshot(page), + options: options + ? { + model: options.model ?? "gpt-4-1106-preview", + debug: options.debug ?? false, + } + : undefined, + }); + return result; +} diff --git a/tests/auto.spec.ts b/tests/auto.spec.ts index 807cb20..51dd338 100644 --- a/tests/auto.spec.ts +++ b/tests/auto.spec.ts @@ -78,3 +78,11 @@ test("executes query, action and assertion", async ({ page }) => { expect(searchInputHasHeaderText).toBe(true); }); + +test("runs without test parameter", async ({ page }) => { + await page.goto("/"); + + const headerText = await auto("get the header text", { page }); + + expect(headerText.query).toBe("Hello, Rayrun!"); +});