Skip to content

Commit bfee1b8

Browse files
[v4]: update stubs to return selector obj (browserbase#1865)
1 parent f517608 commit bfee1b8

6 files changed

Lines changed: 54 additions & 37 deletions

File tree

packages/server-v4/src/routes/v4/page/click.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PageClickActionSchema,
77
PageClickRequestSchema,
88
PageClickResponseSchema,
9-
PageXPathResultSchema,
9+
PageClickResultSchema,
1010
} from "../../../schemas/v4/page.js";
1111
import { createPageActionHandler, pageErrorResponses } from "./shared.js";
1212

@@ -26,11 +26,8 @@ const clickRoute: RouteOptions = {
2626
handler: createPageActionHandler({
2727
method: "click",
2828
actionSchema: PageClickActionSchema,
29-
execute: async ({ params }) => {
30-
const sel = params.selector;
31-
return PageXPathResultSchema.parse({
32-
xpath: "xpath" in sel ? sel.xpath : "xpath=//stub-click",
33-
});
29+
execute: async () => {
30+
return PageClickResultSchema.parse({ selector: {} });
3431
},
3532
}),
3633
};

packages/server-v4/src/routes/v4/page/dragAndDrop.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ const dragAndDropRoute: RouteOptions = {
2626
handler: createPageActionHandler({
2727
method: "dragAndDrop",
2828
actionSchema: PageDragAndDropActionSchema,
29-
execute: async ({ params }) => {
29+
execute: async () => {
3030
return PageDragAndDropResultSchema.parse({
31-
fromXpath:
32-
"xpath" in params.from ? params.from.xpath : "xpath=//stub-from",
33-
toXpath: "xpath" in params.to ? params.to.xpath : "xpath=//stub-to",
31+
startSelector: {},
32+
endSelector: {},
3433
});
3534
},
3635
}),

packages/server-v4/src/routes/v4/page/hover.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PageHoverActionSchema,
77
PageHoverRequestSchema,
88
PageHoverResponseSchema,
9-
PageXPathResultSchema,
9+
PageHoverResultSchema,
1010
} from "../../../schemas/v4/page.js";
1111
import { createPageActionHandler, pageErrorResponses } from "./shared.js";
1212

@@ -26,11 +26,8 @@ const hoverRoute: RouteOptions = {
2626
handler: createPageActionHandler({
2727
method: "hover",
2828
actionSchema: PageHoverActionSchema,
29-
execute: async ({ params }) => {
30-
const sel = params.selector;
31-
return PageXPathResultSchema.parse({
32-
xpath: "xpath" in sel ? sel.xpath : "xpath=//stub-hover",
33-
});
29+
execute: async () => {
30+
return PageHoverResultSchema.parse({ selector: {} });
3431
},
3532
}),
3633
};

packages/server-v4/src/routes/v4/page/scroll.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PageScrollActionSchema,
77
PageScrollRequestSchema,
88
PageScrollResponseSchema,
9-
PageXPathResultSchema,
9+
PageScrollResultSchema,
1010
} from "../../../schemas/v4/page.js";
1111
import { createPageActionHandler, pageErrorResponses } from "./shared.js";
1212

@@ -28,9 +28,9 @@ const scrollRoute: RouteOptions = {
2828
actionSchema: PageScrollActionSchema,
2929
execute: async ({ params }) => {
3030
const sel = params.selector;
31-
return PageXPathResultSchema.parse({
32-
xpath: "xpath" in sel ? sel.xpath : "xpath=//stub-scroll",
33-
});
31+
const x = "x" in sel ? sel.x : 0;
32+
const y = "y" in sel ? sel.y : 0;
33+
return PageScrollResultSchema.parse({ x, y });
3434
},
3535
}),
3636
};

packages/server-v4/src/routes/v4/page/shared.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ type PageActionHandlerContext<TAction extends PageAction> = {
3737
sessionId: string;
3838
};
3939

40-
// Selector is a discriminated union of xpath, css, text, or coordinate types.
41-
// Only xpath is fully resolved today; other types fall back to a stub xpath.
42-
function normalizeXPath(xpath: string): string {
43-
return xpath.startsWith("xpath=") || xpath.startsWith("/")
44-
? xpath
45-
: `xpath=${xpath}`;
46-
}
47-
4840
export function getPageId(params: unknown): string | undefined {
4941
if (
5042
typeof params === "object" &&
@@ -190,5 +182,3 @@ export const pageActionListHandler: RouteHandlerMethod = async (
190182
] as PageAction[],
191183
});
192184
};
193-
194-
export { normalizeXPath };

packages/server-v4/src/schemas/v4/page.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,14 @@ export const PageClickParamsSchema = PageWithPageIdSchema.extend({
270270
selector: SelectorSchema,
271271
button: MouseButtonSchema.optional(),
272272
clickCount: z.number().int().min(1).optional(),
273+
returnSelector: z.boolean().optional(),
273274
})
274275
.strict()
275276
.meta({ id: "PageClickParams" });
276277

277278
export const PageHoverParamsSchema = PageWithPageIdSchema.extend({
278279
selector: SelectorSchema,
280+
returnSelector: z.boolean().optional(),
279281
})
280282
.strict()
281283
.meta({ id: "PageHoverParams" });
@@ -305,6 +307,7 @@ export const PageDragAndDropParamsSchema = PageWithPageIdSchema.extend({
305307
button: MouseButtonSchema.optional(),
306308
steps: z.number().int().positive().optional(),
307309
delay: z.number().int().min(0).optional(),
310+
returnSelector: z.boolean().optional(),
308311
})
309312
.strict()
310313
.meta({ id: "PageDragAndDropParams" });
@@ -622,17 +625,47 @@ export const PageCloseRequestSchema = createPageRequestSchema(
622625
PageCloseParamsSchema,
623626
);
624627

625-
export const PageXPathResultSchema = z
628+
export const PageScrollResultSchema = z
629+
.object({
630+
x: z.number(),
631+
y: z.number(),
632+
})
633+
.strict()
634+
.meta({ id: "PageScrollResult" });
635+
636+
export const ResultSelectorSchema = z
626637
.object({
627638
xpath: z.string().optional(),
639+
css: z.string().optional(),
640+
text: z.string().optional(),
641+
coordinates: z
642+
.object({
643+
x: z.number(),
644+
y: z.number(),
645+
})
646+
.optional(),
647+
})
648+
.strict()
649+
.meta({ id: "ResultSelector" });
650+
651+
export const PageClickResultSchema = z
652+
.object({
653+
selector: ResultSelectorSchema,
654+
})
655+
.strict()
656+
.meta({ id: "PageClickResult" });
657+
658+
export const PageHoverResultSchema = z
659+
.object({
660+
selector: ResultSelectorSchema,
628661
})
629662
.strict()
630-
.meta({ id: "PageXPathResult" });
663+
.meta({ id: "PageHoverResult" });
631664

632665
export const PageDragAndDropResultSchema = z
633666
.object({
634-
fromXpath: z.string().optional(),
635-
toXpath: z.string().optional(),
667+
startSelector: ResultSelectorSchema,
668+
endSelector: ResultSelectorSchema,
636669
})
637670
.strict()
638671
.meta({ id: "PageDragAndDropResult" });
@@ -821,21 +854,21 @@ export const PageClickActionSchema = createPageActionSchema(
821854
"PageClickAction",
822855
"click",
823856
PageClickParamsSchema,
824-
PageXPathResultSchema,
857+
PageClickResultSchema,
825858
);
826859

827860
export const PageHoverActionSchema = createPageActionSchema(
828861
"PageHoverAction",
829862
"hover",
830863
PageHoverParamsSchema,
831-
PageXPathResultSchema,
864+
PageHoverResultSchema,
832865
);
833866

834867
export const PageScrollActionSchema = createPageActionSchema(
835868
"PageScrollAction",
836869
"scroll",
837870
PageScrollParamsSchema,
838-
PageXPathResultSchema,
871+
PageScrollResultSchema,
839872
);
840873

841874
export const PageDragAndDropActionSchema = createPageActionSchema(
@@ -1280,6 +1313,7 @@ export const pageOpenApiComponents = {
12801313
CoordinateSelector: CoordinateSelectorSchema,
12811314
Selector: SelectorSchema,
12821315
ElementSelector: ElementSelectorSchema,
1316+
ResultSelector: ResultSelectorSchema,
12831317
PageHeaders: PageHeadersSchema,
12841318
PageInitScript: PageInitScriptSchema,
12851319
PageClip: PageClipSchema,

0 commit comments

Comments
 (0)