Skip to content

Commit 28da065

Browse files
chore: extract duplicate test setup code into shared helper (#457)
Removed duplicate BrowserTool initialization code that was repeated across multiple test files by creating a shared createBrowserTool helper function. ## What changed - Created `test-helpers.ts` with `createBrowserTool` function that encapsulates the common setup - Updated 5 test files to use the shared helper instead of duplicating the initialization code ## The helper encapsulates: - BrowserTool instantiation with default dimensions (1920x1080) - Test context setup including page, browser, and testRun - Playwright object enhancement with custom request.newContext ## Impact - **test-browser.ts**: -25 lines - **test-github-login.ts**: -75 lines (had 3 duplicate occurrences!) - **test-keyboard.ts**: -25 lines - **test-email-rendering.ts**: -22 lines - **test-helpers.ts**: +55 lines (new file) **Net reduction: 92 lines removed** 🎉 This makes the test code more maintainable and reduces duplication significantly. --------- Co-authored-by: Sashank Thupukari <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6ad89cb commit 28da065

File tree

5 files changed

+66
-158
lines changed

5 files changed

+66
-158
lines changed

packages/shortest/tests/e2e/test-browser.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pc from "picocolors";
2-
import * as playwright from "playwright";
3-
import { request } from "playwright";
4-
import { BrowserTool } from "@/browser/core/browser-tool";
2+
import { createBrowserTool } from "./test-helpers";
53
import { BrowserManager } from "@/browser/manager";
64
import { createTestCase } from "@/core/runner/test-case";
75
import { TestRun } from "@/core/runner/test-run";
@@ -23,31 +21,7 @@ export const main = async () => {
2321
const context = await browserManager.launch();
2422
const page = context.pages()[0];
2523

26-
const browserTool = new BrowserTool(page, browserManager, {
27-
width: 1920,
28-
height: 1080,
29-
testContext: {
30-
page,
31-
browser: browserManager.getBrowser()!,
32-
testRun,
33-
currentStepIndex: 0,
34-
playwright: {
35-
...playwright,
36-
request: {
37-
...request,
38-
newContext: async (options?: {
39-
extraHTTPHeaders?: Record<string, string>;
40-
}) => {
41-
const requestContext = await request.newContext({
42-
baseURL: getConfig().baseUrl,
43-
...options,
44-
});
45-
return requestContext;
46-
},
47-
},
48-
},
49-
},
50-
});
24+
const browserTool = createBrowserTool(page, browserManager, testRun);
5125

5226
// Navigate to a page with a sign in button
5327
await page.goto("http://localhost:3000");

packages/shortest/tests/e2e/test-email-rendering.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Mailosaur from "mailosaur";
22
import pc from "picocolors";
3-
import * as playwright from "playwright";
4-
import { chromium, request } from "playwright";
5-
import { BrowserTool } from "@/browser/core/browser-tool";
3+
import { chromium } from "playwright";
4+
import { createBrowserTool } from "./test-helpers";
65
import { BrowserManager } from "@/browser/manager";
76
import { createTestCase } from "@/core/runner/test-case";
87
import { TestRun } from "@/core/runner/test-run";
@@ -49,29 +48,9 @@ export const main = async () => {
4948
const testRun = TestRun.create(testCase);
5049
testRun.markRunning();
5150

52-
const browserTool = new BrowserTool(page, browserManager, {
51+
const browserTool = createBrowserTool(page, browserManager, testRun, {
5352
width: 1280,
5453
height: 720,
55-
testContext: {
56-
page,
57-
browser: browserManager.getBrowser()!,
58-
playwright: {
59-
...playwright,
60-
request: {
61-
...request,
62-
newContext: async (options?: {
63-
extraHTTPHeaders?: Record<string, string>;
64-
}) => {
65-
const requestContext = await request.newContext({
66-
baseURL: config.baseUrl,
67-
...options,
68-
});
69-
return requestContext;
70-
},
71-
},
72-
},
73-
testRun,
74-
},
7554
});
7655

7756
// 3. Test render_email tool

packages/shortest/tests/e2e/test-github-login.ts

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pc from "picocolors";
2-
import * as playwright from "playwright";
3-
import { request } from "playwright";
4-
import { BrowserTool } from "@/browser/core/browser-tool";
2+
import { createBrowserTool } from "./test-helpers";
53
import { GitHubTool } from "@/browser/integrations/github";
64
import { BrowserManager } from "@/browser/manager";
75
import { createTestCase } from "@/core/runner/test-case";
@@ -25,31 +23,7 @@ export const main = async () => {
2523
const testRun = TestRun.create(testCase);
2624
testRun.markRunning();
2725

28-
let browserTool = new BrowserTool(page, browserManager, {
29-
width: 1920,
30-
height: 1080,
31-
testContext: {
32-
page,
33-
browser: browserManager.getBrowser()!,
34-
testRun,
35-
currentStepIndex: 0,
36-
playwright: {
37-
...playwright,
38-
request: {
39-
...request,
40-
newContext: async (options?: {
41-
extraHTTPHeaders?: Record<string, string>;
42-
}) => {
43-
const requestContext = await request.newContext({
44-
baseURL: getConfig().baseUrl,
45-
...options,
46-
});
47-
return requestContext;
48-
},
49-
},
50-
},
51-
},
52-
});
26+
let browserTool = createBrowserTool(page, browserManager, testRun);
5327

5428
console.log(pc.cyan("\n🧹 Clearing initial session..."));
5529
const result = await browserTool.execute({ action: "clear_session" });
@@ -61,31 +35,7 @@ export const main = async () => {
6135
page = context.pages()[0];
6236

6337
// Update browserTool with new page
64-
browserTool = new BrowserTool(page, browserManager, {
65-
width: 1920,
66-
height: 1080,
67-
testContext: {
68-
page,
69-
browser: browserManager.getBrowser()!,
70-
testRun,
71-
currentStepIndex: 0,
72-
playwright: {
73-
...playwright,
74-
request: {
75-
...request,
76-
newContext: async (options?: {
77-
extraHTTPHeaders?: Record<string, string>;
78-
}) => {
79-
const requestContext = await request.newContext({
80-
baseURL: getConfig().baseUrl,
81-
...options,
82-
});
83-
return requestContext;
84-
},
85-
},
86-
},
87-
},
88-
});
38+
browserTool = createBrowserTool(page, browserManager, testRun);
8939

9040
// Continue with fresh page reference
9141
await page.waitForSelector('button:has-text("Sign in")', {
@@ -116,31 +66,7 @@ export const main = async () => {
11666
const newPage = newContext.pages()[0];
11767

11868
// Create new browser tool instance
119-
browserTool = new BrowserTool(page, browserManager, {
120-
width: 1920,
121-
height: 1080,
122-
testContext: {
123-
page,
124-
browser: browserManager.getBrowser()!,
125-
testRun,
126-
currentStepIndex: 0,
127-
playwright: {
128-
...playwright,
129-
request: {
130-
...request,
131-
newContext: async (options?: {
132-
extraHTTPHeaders?: Record<string, string>;
133-
}) => {
134-
const requestContext = await request.newContext({
135-
baseURL: getConfig().baseUrl,
136-
...options,
137-
});
138-
return requestContext;
139-
},
140-
},
141-
},
142-
},
143-
});
69+
browserTool = createBrowserTool(page, browserManager, testRun);
14470

14571
console.log(pc.cyan("\n🔍 Checking login state..."));
14672
await newPage.goto("http://localhost:3000");
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as playwright from "playwright";
2+
import { request } from "playwright";
3+
import { BrowserTool } from "@/browser/core/browser-tool";
4+
import { BrowserManager } from "@/browser/manager";
5+
import { TestRun } from "@/core/runner/test-run";
6+
import { getConfig } from "@/index";
7+
8+
export interface TestContext {
9+
page: playwright.Page;
10+
browser: playwright.Browser;
11+
testRun: TestRun;
12+
currentStepIndex: number;
13+
playwright: typeof playwright & {
14+
request: typeof request & {
15+
newContext: (options?: {
16+
extraHTTPHeaders?: Record<string, string>;
17+
}) => Promise<playwright.APIRequestContext>;
18+
};
19+
};
20+
}
21+
22+
export const createBrowserTool = (
23+
page: playwright.Page,
24+
browserManager: BrowserManager,
25+
testRun: TestRun,
26+
options: { width?: number; height?: number } = {},
27+
): BrowserTool => {
28+
const { width = 1920, height = 1080 } = options;
29+
30+
return new BrowserTool(page, browserManager, {
31+
width,
32+
height,
33+
testContext: {
34+
page,
35+
browser: browserManager.getBrowser()!,
36+
testRun,
37+
currentStepIndex: 0,
38+
playwright: {
39+
...playwright,
40+
request: {
41+
...request,
42+
newContext: async (options?: {
43+
extraHTTPHeaders?: Record<string, string>;
44+
}) => {
45+
const requestContext = await request.newContext({
46+
baseURL: getConfig().baseUrl,
47+
...options,
48+
});
49+
return requestContext;
50+
},
51+
},
52+
},
53+
},
54+
});
55+
};

packages/shortest/tests/e2e/test-keyboard.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pc from "picocolors";
2-
import * as playwright from "playwright";
3-
import { request } from "playwright";
4-
import { BrowserTool } from "@/browser/core/browser-tool";
2+
import { createBrowserTool } from "./test-helpers";
53
import { BrowserManager } from "@/browser/manager";
64
import { createTestCase } from "@/core/runner/test-case";
75
import { TestRun } from "@/core/runner/test-run";
@@ -23,31 +21,7 @@ export const main = async () => {
2321
const testRun = TestRun.create(testCase);
2422
testRun.markRunning();
2523

26-
const browserTool = new BrowserTool(page, browserManager, {
27-
width: 1920,
28-
height: 1080,
29-
testContext: {
30-
page,
31-
browser: browserManager.getBrowser()!,
32-
testRun,
33-
currentStepIndex: 0,
34-
playwright: {
35-
...playwright,
36-
request: {
37-
...request,
38-
newContext: async (options?: {
39-
extraHTTPHeaders?: Record<string, string>;
40-
}) => {
41-
const requestContext = await request.newContext({
42-
baseURL: getConfig().baseUrl,
43-
...options,
44-
});
45-
return requestContext;
46-
},
47-
},
48-
},
49-
},
50-
});
24+
const browserTool = createBrowserTool(page, browserManager, testRun);
5125

5226
// Test 1: Test Page_Down key (exactly as AI sends it)
5327
console.log(pc.cyan("\nTest 1: Testing Page_Down key"));

0 commit comments

Comments
 (0)