Skip to content

Commit a734fca

Browse files
authored
fix: correctly pass in userDataDir to chrome launcher (#1462)
# why Stagehand's `userDataDir` was ignored b/c of `chrome-launcher`'s default automatic `--user-data-flag`. Fixes #1377 # what changed Directly pass in `userDataDir` via `launch()` options # test plan Tested that Chrome now correctly uses the custom `userDataDir` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix Chrome launch to respect the provided userDataDir by passing it directly to chrome-launcher via launch options. Previously, the flag was ignored due to chrome-launcher’s default user-data-dir. <sup>Written for commit dfc55fa. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
1 parent 54e4dc5 commit a734fca

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

.changeset/tall-icons-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
fix: correctly pass userDataDir to chrome launcher

packages/core/lib/v3/launch/local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export async function launchLocalChrome(
2020
"--no-default-browser-check",
2121
"--disable-dev-shm-usage",
2222
"--site-per-process",
23-
opts.userDataDir ? `--user-data-dir=${opts.userDataDir}` : undefined,
2423
...(opts.chromeFlags ?? []),
2524
].filter((f): f is string => typeof f === "string");
2625

2726
const chrome = await launch({
2827
chromePath: opts.chromePath,
2928
chromeFlags,
29+
userDataDir: opts.userDataDir,
3030
});
3131

3232
const ws = await waitForWebSocketDebuggerUrl(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { test, expect } from "@playwright/test";
2+
import { V3 } from "../v3";
3+
import { v3TestConfig } from "./v3.config";
4+
import * as fs from "fs";
5+
import * as path from "path";
6+
import * as os from "os";
7+
8+
test.describe("userDataDir persistence", () => {
9+
let v3: V3;
10+
let testDir: string;
11+
12+
test.beforeEach(() => {
13+
testDir = fs.mkdtempSync(
14+
path.join(os.tmpdir(), "stagehand-userdata-test-"),
15+
);
16+
});
17+
18+
test.afterEach(async () => {
19+
await v3?.close?.().catch(() => {});
20+
if (testDir && fs.existsSync(testDir)) {
21+
fs.rmSync(testDir, { recursive: true, force: true });
22+
}
23+
});
24+
25+
test("Chrome uses the specified userDataDir", async () => {
26+
v3 = new V3({
27+
...v3TestConfig,
28+
localBrowserLaunchOptions: {
29+
...v3TestConfig.localBrowserLaunchOptions,
30+
userDataDir: testDir,
31+
preserveUserDataDir: true,
32+
},
33+
});
34+
35+
await v3.init();
36+
37+
const page = v3.context.pages()[0];
38+
await page.goto("about:blank");
39+
40+
await expect
41+
.poll(() => fs.existsSync(path.join(testDir, "Default")), {
42+
timeout: 10_000,
43+
})
44+
.toBe(true);
45+
46+
expect(fs.existsSync(path.join(testDir, "Local State"))).toBe(true);
47+
});
48+
});

0 commit comments

Comments
 (0)