Skip to content

Commit

Permalink
feat: add ability to run without test param
Browse files Browse the repository at this point in the history
  • Loading branch information
lucgagan authored Nov 15, 2023
2 parents a91884b + b1b9f92 commit 62f41a3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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("<your prompt>", { 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.
Expand Down
53 changes: 33 additions & 20 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => {
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);
Expand All @@ -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;
}
8 changes: 8 additions & 0 deletions tests/auto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});

0 comments on commit 62f41a3

Please sign in to comment.