Skip to content

Commit

Permalink
test: add tests for locateElement
Browse files Browse the repository at this point in the history
  • Loading branch information
lucgagan committed Nov 14, 2023
1 parent cbeed31 commit f8834a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/createActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { z } from "zod";

export const createActions = (
page: Page
): Record<string, RunnableFunctionWithoutParse | RunnableFunctionWithParse<any>> => {
): Record<string, RunnableFunctionWithParse<any>> => {
const locatorMap = new Map();

const getLocator = (elementId: string) => {
Expand All @@ -23,10 +23,7 @@ export const createActions = (

return {
locateElement: {
function: async (args: {
attributeName: string;
cssSelector: string;
}) => {
function: async (args: { cssSelector: string }) => {
const locator = await page.locator(args.cssSelector);

const elementId = randomUUID();
Expand Down Expand Up @@ -606,6 +603,9 @@ export const createActions = (
function: () => {
return null;
},
parse: (args: string) => {
return z.object({}).parse(JSON.parse(args));
},
description:
"This function is called at the end when the initial instructions asked to perform an action.",
name: "resultAction",
Expand Down
24 changes: 24 additions & 0 deletions tests/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from "@playwright/test";
import { createActions } from "../src/createActions";
import { ChatCompletionRunner } from "openai/lib/ChatCompletionRunner";

const runner = {} as ChatCompletionRunner;

test("finds element using a CSS locator and returns elementId", async ({
page,
}) => {
await page.goto("/");

const actions = createActions(page);

const result = await actions.locateElement.function(
{
cssSelector: "h1",
},
runner
);

expect(result).toStrictEqual({
elementId: expect.any(String),
});
});

0 comments on commit f8834a5

Please sign in to comment.