Skip to content

Commit

Permalink
feat: add locator_evaluate (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucgagan committed Nov 14, 2023
1 parent 7e1e6ec commit 0fc3e36
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/createActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ export const createActions = (
},
},
},
locator_evaluate: {
function: async (args: { pageFunction: string; elementId: string }) => {
return {
result: await getLocator(args.elementId).evaluate(
args.pageFunction
),
};
},
description: 'Execute JavaScript code in the page, taking the matching element as an argument.',
name: "locator_evaluate",
parameters: {
type: 'object',
properties: {
elementId: {
type: "string",
},
pageFunction: {
type: 'string',
description: 'Function to be evaluated in the page context, e.g. node => node.innerText',
},
},
},
parse: (args: string) => {
return z
.object({
elementId: z.string(),
pageFunction: z.string(),
})
.parse(JSON.parse(args));
}
},
locator_getAttribute: {
function: async (args: { attributeName: string; elementId: string }) => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const prompt = (message: TaskMessage) => {
* When creating CSS selectors, ensure they are unique and specific enough to select only one element, even if there are multiple elements of the same type (like multiple h1 elements).
* Avoid using generic tags like 'h1' alone. Instead, combine them with other attributes or structural relationships to form a unique selector.
* You must not derive data from the page if you are able to do so by using one of the provided functions, e.g. locator_evaluate.
Webpage snapshot:
Expand Down
9 changes: 9 additions & 0 deletions tests/auto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ test("executes query", async ({ page }) => {
expect(headerText).toBe("Hello, Rayrun!");
});

test("executes query using locator_evaluate", async ({ page }) => {
await page.goto("/");

const headerText = await auto("get the first letter of the header text", { page, test });

// TODO assert that we are using locator_evaluate to get the first letter
expect(headerText).toBe("H");
});

test("executes action", async ({ page }) => {
await page.goto("/");

Expand Down

0 comments on commit 0fc3e36

Please sign in to comment.