Skip to content

Commit

Permalink
Add ability to run without test param
Browse files Browse the repository at this point in the history
  • Loading branch information
agamm committed Nov 15, 2023
1 parent 08d4539 commit 111c651
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
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 111c651

Please sign in to comment.