Skip to content

Commit b29ac55

Browse files
update tests to reflect live page proxy (#911)
# why - `stagehand.page` is now a proxy pointing to the most recently opened tab - this means that the below listener will fail because it closes the page while simultaneously trying to use the same page reference for `goto`: ```typescript page.on("popup", async (newPage) => { navigatePromise = Promise.allSettled([ newPage.close(), page.goto(newPage.url(), { waitUntil: "domcontentloaded" }), ]); }); ``` # what changed - removed irrelevant & broken tests - added new tests for the live page proxy # test plan - this is it
1 parent 88b50b8 commit b29ac55

File tree

2 files changed

+43
-70
lines changed

2 files changed

+43
-70
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect, test } from "@playwright/test";
2+
import { Stagehand } from "@browserbasehq/stagehand";
3+
import StagehandConfig from "@/evals/deterministic/stagehand.config";
4+
5+
test.describe("StagehandPage - live page proxy", () => {
6+
test("tests that the page URL reflects the URL of the newest opened tab", async () => {
7+
const stagehand = new Stagehand(StagehandConfig);
8+
await stagehand.init();
9+
10+
const page = stagehand.page;
11+
await page.goto("https://browserbase.github.io/stagehand-eval-sites/sites/five-tab/");
12+
await page.locator("body > button").click();
13+
await new Promise(resolve => setTimeout(resolve, 1000));
14+
await page.waitForURL('**/page2.html', {waitUntil: "commit"});
15+
// await new Promise(resolve => setTimeout(resolve, 1000));
16+
const currentURL = page.url();
17+
const expectedURL = "https://browserbase.github.io/stagehand-eval-sites/sites/five-tab/page2.html";
18+
19+
expect(currentURL).toBe(expectedURL);
20+
21+
await stagehand.close();
22+
});
23+
24+
test("tests that opening a new tab does not close the old tab", async () => {
25+
const stagehand = new Stagehand(StagehandConfig);
26+
await stagehand.init();
27+
28+
const page = stagehand.page;
29+
await page.goto("https://browserbase.github.io/stagehand-eval-sites/sites/five-tab/");
30+
await page.locator("body > button").click();
31+
await new Promise(resolve => setTimeout(resolve, 1000));
32+
33+
const expectedNumPages = 2;
34+
const actualNumPages = stagehand.context.pages().length;
35+
36+
expect(actualNumPages).toBe(expectedNumPages);
37+
38+
await stagehand.close();
39+
});
40+
});

evals/deterministic/tests/page/on.test.ts

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,6 @@ import { Stagehand } from "@browserbasehq/stagehand";
33
import StagehandConfig from "@/evals/deterministic/stagehand.config";
44

55
test.describe("StagehandPage - page.on()", () => {
6-
test("should click on the crewAI blog tab", async () => {
7-
const stagehand = new Stagehand(StagehandConfig);
8-
await stagehand.init();
9-
10-
const page = stagehand.page;
11-
await page.goto(
12-
"https://docs.browserbase.com/integrations/crew-ai/introduction",
13-
);
14-
15-
let clickPromise: Promise<void>;
16-
17-
page.on("popup", async (newPage) => {
18-
clickPromise = newPage.click(
19-
"body > div.page-wrapper > div.navbar-2.w-nav > div.padding-global.top-bot > div > div.navigation-left > nav > a:nth-child(7)",
20-
);
21-
});
22-
23-
await page.goto(
24-
"https://docs.browserbase.com/integrations/crew-ai/introduction",
25-
);
26-
27-
await page.click(
28-
"#content-area > div.relative.mt-8.prose.prose-gray.dark\\:prose-invert > p:nth-child(2) > a",
29-
);
30-
31-
await clickPromise;
32-
33-
await stagehand.close();
34-
});
35-
36-
test("should close the new tab and navigate to it on the existing page", async () => {
37-
const stagehand = new Stagehand(StagehandConfig);
38-
await stagehand.init();
39-
40-
const page = stagehand.page;
41-
await page.goto(
42-
"https://docs.browserbase.com/integrations/crew-ai/introduction",
43-
);
44-
45-
let navigatePromise: Promise<unknown>;
46-
47-
page.on("popup", async (newPage) => {
48-
navigatePromise = Promise.allSettled([
49-
newPage.close(),
50-
page.goto(newPage.url(), { waitUntil: "domcontentloaded" }),
51-
]);
52-
});
53-
54-
// Click on the crewAI blog tab
55-
await page.click(
56-
"#content-area > div.relative.mt-8.prose.prose-gray.dark\\:prose-invert > p:nth-child(2) > a",
57-
);
58-
59-
await navigatePromise;
60-
61-
await page.click(
62-
"body > div.page-wrapper > div.navbar-2.w-nav > div.padding-global.top-bot > div > div.navigation-left > nav > a:nth-child(3)",
63-
);
64-
65-
await page.waitForLoadState("domcontentloaded");
66-
67-
const currentUrl = page.url();
68-
expect(currentUrl).toBe("https://www.crewai.com/open-source");
69-
70-
await stagehand.close();
71-
});
72-
736
test("should handle console events", async () => {
747
const stagehand = new Stagehand(StagehandConfig);
758
await stagehand.init();
@@ -94,7 +27,7 @@ test.describe("StagehandPage - page.on()", () => {
9427
await stagehand.init();
9528

9629
const page = stagehand.page;
97-
await page.goto("https://example.com");
30+
await page.goto("https://example.com", { waitUntil: "commit" });
9831

9932
page.on("dialog", async (dialog) => {
10033
expect(dialog.message()).toBe("Test alert");
@@ -111,7 +44,7 @@ test.describe("StagehandPage - page.on()", () => {
11144
await stagehand.init();
11245

11346
const page = stagehand.page;
114-
await page.goto("https://example.com");
47+
await page.goto("https://example.com", { waitUntil: "commit" });
11548

11649
const requests: string[] = [];
11750
const responses: string[] = [];
@@ -124,7 +57,7 @@ test.describe("StagehandPage - page.on()", () => {
12457
responses.push(response.url());
12558
});
12659

127-
await page.goto("https://example.com");
60+
await page.goto("https://example.com", { waitUntil: "commit" });
12861

12962
expect(requests).toContain("https://example.com/");
13063
expect(responses).toContain("https://example.com/");

0 commit comments

Comments
 (0)