Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-sides-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

Fix multiple click event dispatches on CDP and Anthropic CUA handling (double clicks)
10 changes: 7 additions & 3 deletions packages/core/lib/v3/agent/AnthropicCUAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,13 @@ export class AnthropicCUAClient extends AgentClient {
};
} else if (action === "double_click" || action === "doubleClick") {
return {
type: action,
x: input.x as number,
y: input.y as number,
type: "doubleClick",
x:
(input.x as number) ||
(input.coordinate ? (input.coordinate as number[])[0] : 0),
y:
(input.y as number) ||
(input.coordinate ? (input.coordinate as number[])[1] : 0),
...input,
};
} else if (action === "scroll") {
Expand Down
178 changes: 178 additions & 0 deletions packages/core/lib/v3/tests/click-count.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { test, expect } from "@playwright/test";
import { V3 } from "../v3";
import { v3TestConfig } from "./v3.config";

test.describe("Locator and Page click methods", () => {
let v3: V3;

test.beforeEach(async () => {
v3 = new V3(v3TestConfig);
await v3.init();
});

test.afterEach(async () => {
await v3?.close?.().catch(() => {});
});

test("locator.click() performs single click by default", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

// Get initial count
const countDisplay = page.locator("#count");
const initialCount = await countDisplay.inputValue();
expect(initialCount).toBe("0");

// Perform single click on the textarea (the clickable area)
const clickArea = page.locator("#textarea");
await clickArea.click();

// Verify count incremented by 1
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("1");
});

test("locator.click() with clickCount: 2 performs double-click", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

// Get initial counts
const countDisplay = page.locator("#count");
const dcCountDisplay = page.locator("#dcCount");

const initialCount = await countDisplay.inputValue();
const initialDcCount = await dcCountDisplay.inputValue();
expect(initialCount).toBe("0");
expect(initialDcCount).toBe("0");

// Perform double-click on the textarea
const clickArea = page.locator("#textarea");
await clickArea.click({ clickCount: 2 });

// Verify both counters incremented
// Regular count should be 2 (one for each click in the double-click)
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("2");

// Double-click count should be 1 (one double-click event detected)
const newDcCount = await dcCountDisplay.inputValue();
expect(newDcCount).toBe("1");
});

test("locator.click() with clickCount: 3 performs triple-click", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

const countDisplay = page.locator("#count");
const initialCount = await countDisplay.inputValue();
expect(initialCount).toBe("0");

// Perform triple-click on the textarea
const clickArea = page.locator("#textarea");
await clickArea.click({ clickCount: 3 });

// Verify count incremented by 3
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("3");
});

test("page.click() performs single click with coordinates", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

// Get initial count
const countDisplay = page.locator("#count");
const initialCount = await countDisplay.inputValue();
expect(initialCount).toBe("0");

// Get the centroid of the textarea to click
const clickArea = page.locator("#textarea");
const { x, y } = await clickArea.centroid();

// Perform single click using page.click() with coordinates
await page.click(x, y);

// Verify count incremented by 1
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("1");
});

test("page.click() with clickCount: 2 performs double-click", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

// Get initial counts
const countDisplay = page.locator("#count");
const dcCountDisplay = page.locator("#dcCount");

const initialCount = await countDisplay.inputValue();
const initialDcCount = await dcCountDisplay.inputValue();
expect(initialCount).toBe("0");
expect(initialDcCount).toBe("0");

// Get the centroid of the textarea to click
const clickArea = page.locator("#textarea");
const { x, y } = await clickArea.centroid();

// Perform double-click using page.click() with coordinates
await page.click(x, y, { clickCount: 2 });

// Verify both counters incremented
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("2");

// Double-click count should be 1
const newDcCount = await dcCountDisplay.inputValue();
expect(newDcCount).toBe("1");
});

test("page.click() with clickCount: 3 performs triple-click", async () => {
const page = v3.context.pages()[0];
await page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/click-test/",
);

// Wait for page to be fully loaded
await page.waitForLoadState("domcontentloaded");

const countDisplay = page.locator("#count");
const initialCount = await countDisplay.inputValue();
expect(initialCount).toBe("0");

// Get the centroid of the textarea to click
const clickArea = page.locator("#textarea");
const { x, y } = await clickArea.centroid();

// Perform triple-click using page.click() with coordinates
await page.click(x, y, { clickCount: 3 });

// Verify count incremented by 3
const newCount = await countDisplay.inputValue();
expect(newCount).toBe("3");
});
});
33 changes: 18 additions & 15 deletions packages/core/lib/v3/understudy/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,21 +407,24 @@ export class Locator {
button: "none",
} as Protocol.Input.DispatchMouseEventRequest);

await session.send<never>("Input.dispatchMouseEvent", {
type: "mousePressed",
x: cx,
y: cy,
button,
clickCount,
} as Protocol.Input.DispatchMouseEventRequest);

await session.send<never>("Input.dispatchMouseEvent", {
type: "mouseReleased",
x: cx,
y: cy,
button,
clickCount,
} as Protocol.Input.DispatchMouseEventRequest);
// Dispatch mouse pressed and released events for the given click count
for (let i = 1; i <= clickCount; i++) {
await session.send<never>("Input.dispatchMouseEvent", {
type: "mousePressed",
x: cx,
y: cy,
button,
clickCount: i,
} as Protocol.Input.DispatchMouseEventRequest);

await session.send<never>("Input.dispatchMouseEvent", {
type: "mouseReleased",
x: cx,
y: cy,
button,
clickCount: i,
} as Protocol.Input.DispatchMouseEventRequest);
}
} finally {
// release the element handle
try {
Expand Down
32 changes: 17 additions & 15 deletions packages/core/lib/v3/understudy/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1350,22 +1350,24 @@ export class Page {
button: "none",
} as Protocol.Input.DispatchMouseEventRequest);

await this.mainSession.send<never>("Input.dispatchMouseEvent", {
type: "mousePressed",
x,
y,
button,
clickCount,
} as Protocol.Input.DispatchMouseEventRequest);

await this.mainSession.send<never>("Input.dispatchMouseEvent", {
type: "mouseReleased",
x,
y,
button,
clickCount,
} as Protocol.Input.DispatchMouseEventRequest);
// Dispatch mouse pressed and released events for the given click count
for (let i = 1; i <= clickCount; i++) {
await this.mainSession.send<never>("Input.dispatchMouseEvent", {
type: "mousePressed",
x,
y,
button,
clickCount: i,
} as Protocol.Input.DispatchMouseEventRequest);

await this.mainSession.send<never>("Input.dispatchMouseEvent", {
type: "mouseReleased",
x,
y,
button,
clickCount: i,
} as Protocol.Input.DispatchMouseEventRequest);
}
if (options?.returnXpath) return xpathResult ?? "";
}

Expand Down