From 111c651d895e7aff05bfacfd254e95fa818dd9a4 Mon Sep 17 00:00:00 2001 From: Agam More Date: Tue, 14 Nov 2023 21:56:31 -0600 Subject: [PATCH 1/3] Add ability to run without test param --- src/auto.ts | 53 +++++++++++++++++++++++++++++----------------- tests/auto.spec.ts | 8 +++++++ 2 files changed, 41 insertions(+), 20 deletions(-) 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!"); +}); From 4d9526c3b98c35ac0d4d741ef6a71781dcf71be7 Mon Sep 17 00:00:00 2001 From: Agam More Date: Tue, 14 Nov 2023 22:49:58 -0600 Subject: [PATCH 2/3] Add readme explanation --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bb17c69..af7d3d0 100644 --- a/README.md +++ b/README.md @@ -43,12 +43,36 @@ 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` object. ```ts auto("", { page, test }); ``` +### Browser automation + +Running without the `test` parameter: + +```ts +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. From b1b9f92e4d1dee57fb28aea0942884369e0f3f51 Mon Sep 17 00:00:00 2001 From: Agam More Date: Tue, 14 Nov 2023 22:51:44 -0600 Subject: [PATCH 3/3] Minor fixes to README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af7d3d0..3630d78 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ test("auto Playwright example", async ({ page }) => { ## Usage -At minimum, the `auto` function requires a _plain text prompt_ and an _argument_ that contains your `page` object. +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 }); @@ -54,6 +54,7 @@ auto("", { page, test }); Running without the `test` parameter: ```ts +import { chromium } from "playwright"; import { auto } from "auto-playwright"; (async () => {