playwriter 0.0.63 → 0.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a11y-client.js +18 -8
- package/dist/aria-snapshot.d.ts +41 -3
- package/dist/aria-snapshot.d.ts.map +1 -1
- package/dist/aria-snapshot.js +134 -55
- package/dist/aria-snapshot.js.map +1 -1
- package/dist/aria-snapshot.test.js +5 -2
- package/dist/aria-snapshot.test.js.map +1 -1
- package/dist/aria-snapshot.unit.test.js +83 -41
- package/dist/aria-snapshot.unit.test.js.map +1 -1
- package/dist/assets/cursors/screen-studio/pointer-macos-tahoe-data-url.d.ts +5 -0
- package/dist/assets/cursors/screen-studio/pointer-macos-tahoe-data-url.d.ts.map +1 -0
- package/dist/assets/cursors/screen-studio/pointer-macos-tahoe-data-url.js +5 -0
- package/dist/assets/cursors/screen-studio/pointer-macos-tahoe-data-url.js.map +1 -0
- package/dist/bippy.js +1 -1
- package/dist/cdp-log.d.ts +1 -1
- package/dist/cdp-log.d.ts.map +1 -1
- package/dist/cdp-log.js +1 -1
- package/dist/cdp-log.js.map +1 -1
- package/dist/cdp-relay.d.ts.map +1 -1
- package/dist/cdp-relay.js +492 -298
- package/dist/cdp-relay.js.map +1 -1
- package/dist/cdp-session.d.ts.map +1 -1
- package/dist/cdp-session.js.map +1 -1
- package/dist/cdp-types.d.ts.map +1 -1
- package/dist/cdp-types.js +7 -7
- package/dist/cdp-types.js.map +1 -1
- package/dist/clean-html.d.ts.map +1 -1
- package/dist/clean-html.js +4 -5
- package/dist/clean-html.js.map +1 -1
- package/dist/cli.js +45 -27
- package/dist/cli.js.map +1 -1
- package/dist/create-logger.d.ts.map +1 -1
- package/dist/create-logger.js +3 -1
- package/dist/create-logger.js.map +1 -1
- package/dist/debugger-examples-types.d.ts.map +1 -1
- package/dist/debugger.d.ts.map +1 -1
- package/dist/debugger.js +1 -3
- package/dist/debugger.js.map +1 -1
- package/dist/diff-utils.d.ts.map +1 -1
- package/dist/diff-utils.js +1 -4
- package/dist/diff-utils.js.map +1 -1
- package/dist/editor-api.md +12 -2
- package/dist/editor-examples.d.ts +1 -1
- package/dist/editor-examples.d.ts.map +1 -1
- package/dist/editor-examples.js +1 -1
- package/dist/editor-examples.js.map +1 -1
- package/dist/editor.d.ts +1 -1
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +1 -1
- package/dist/editor.js.map +1 -1
- package/dist/executor.d.ts +26 -3
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +297 -64
- package/dist/executor.js.map +1 -1
- package/dist/executor.unit.test.js +38 -1
- package/dist/executor.unit.test.js.map +1 -1
- package/dist/extension-connection.test.js +139 -36
- package/dist/extension-connection.test.js.map +1 -1
- package/dist/ffmpeg.d.ts +148 -0
- package/dist/ffmpeg.d.ts.map +1 -0
- package/dist/ffmpeg.js +523 -0
- package/dist/ffmpeg.js.map +1 -0
- package/dist/ghost-browser.d.ts.map +1 -1
- package/dist/ghost-browser.js.map +1 -1
- package/dist/ghost-cursor-client.js +287 -0
- package/dist/ghost-cursor.d.ts +27 -0
- package/dist/ghost-cursor.d.ts.map +1 -0
- package/dist/ghost-cursor.js +63 -0
- package/dist/ghost-cursor.js.map +1 -0
- package/dist/htmlrewrite.d.ts.map +1 -1
- package/dist/htmlrewrite.js +17 -55
- package/dist/htmlrewrite.js.map +1 -1
- package/dist/htmlrewrite.test.js.map +1 -1
- package/dist/kill-port.d.ts.map +1 -1
- package/dist/kill-port.js +1 -3
- package/dist/kill-port.js.map +1 -1
- package/dist/locator-selector.test.d.ts +2 -0
- package/dist/locator-selector.test.d.ts.map +1 -0
- package/dist/locator-selector.test.js +96 -0
- package/dist/locator-selector.test.js.map +1 -0
- package/dist/mcp-client.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +8 -3
- package/dist/mcp.js.map +1 -1
- package/dist/on-mouse-action.test.d.ts +2 -0
- package/dist/on-mouse-action.test.d.ts.map +1 -0
- package/dist/on-mouse-action.test.js +155 -0
- package/dist/on-mouse-action.test.js.map +1 -0
- package/dist/page-markdown.js +4 -4
- package/dist/page-markdown.js.map +1 -1
- package/dist/prompt.md +450 -377
- package/dist/protocol.d.ts +4 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/readability.js +16 -2
- package/dist/recording-ghost-cursor.d.ts +41 -0
- package/dist/recording-ghost-cursor.d.ts.map +1 -0
- package/dist/recording-ghost-cursor.js +79 -0
- package/dist/recording-ghost-cursor.js.map +1 -0
- package/dist/recording-relay.d.ts.map +1 -1
- package/dist/recording-relay.js +8 -8
- package/dist/recording-relay.js.map +1 -1
- package/dist/relay-client.d.ts +17 -4
- package/dist/relay-client.d.ts.map +1 -1
- package/dist/relay-client.js +45 -11
- package/dist/relay-client.js.map +1 -1
- package/dist/relay-core.test.d.ts.map +1 -1
- package/dist/relay-core.test.js +515 -26
- package/dist/relay-core.test.js.map +1 -1
- package/dist/relay-navigation.test.d.ts.map +1 -1
- package/dist/relay-navigation.test.js +169 -31
- package/dist/relay-navigation.test.js.map +1 -1
- package/dist/relay-session.test.d.ts.map +1 -1
- package/dist/relay-session.test.js +113 -65
- package/dist/relay-session.test.js.map +1 -1
- package/dist/relay-state.d.ts +158 -0
- package/dist/relay-state.d.ts.map +1 -0
- package/dist/relay-state.js +306 -0
- package/dist/relay-state.js.map +1 -0
- package/dist/relay-state.test.d.ts +2 -0
- package/dist/relay-state.test.d.ts.map +1 -0
- package/dist/relay-state.test.js +472 -0
- package/dist/relay-state.test.js.map +1 -0
- package/dist/scoped-fs.d.ts.map +1 -1
- package/dist/scoped-fs.js.map +1 -1
- package/dist/screen-recording.d.ts +66 -4
- package/dist/screen-recording.d.ts.map +1 -1
- package/dist/screen-recording.js +150 -13
- package/dist/screen-recording.js.map +1 -1
- package/dist/screen-recording.test.d.ts +2 -0
- package/dist/screen-recording.test.d.ts.map +1 -0
- package/dist/screen-recording.test.js +102 -0
- package/dist/screen-recording.test.js.map +1 -0
- package/dist/selector-generator.js +1 -1
- package/dist/snapshot-tools.test.js +71 -28
- package/dist/snapshot-tools.test.js.map +1 -1
- package/dist/start-relay-server.d.ts +1 -1
- package/dist/start-relay-server.d.ts.map +1 -1
- package/dist/start-relay-server.js +1 -1
- package/dist/start-relay-server.js.map +1 -1
- package/dist/styles-api.md +8 -1
- package/dist/styles-examples.d.ts +1 -1
- package/dist/styles-examples.d.ts.map +1 -1
- package/dist/styles-examples.js +1 -1
- package/dist/styles-examples.js.map +1 -1
- package/dist/styles.d.ts.map +1 -1
- package/dist/styles.js +1 -3
- package/dist/styles.js.map +1 -1
- package/dist/test-declarations.d.ts.map +1 -1
- package/dist/test-utils.d.ts +1 -1
- package/dist/test-utils.d.ts.map +1 -1
- package/dist/test-utils.js +7 -5
- package/dist/test-utils.js.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js.map +1 -1
- package/dist/wait-for-page-load.d.ts.map +1 -1
- package/dist/wait-for-page-load.js +1 -1
- package/dist/wait-for-page-load.js.map +1 -1
- package/package.json +4 -3
- package/src/a11y-client.ts +5 -4
- package/src/aria-snapshot.test.ts +5 -2
- package/src/aria-snapshot.ts +306 -117
- package/src/aria-snapshot.unit.test.ts +199 -141
- package/src/aria-snapshots/github-interactive.txt +2 -0
- package/src/aria-snapshots/github-raw.txt +5 -1
- package/src/aria-snapshots/hackernews-interactive.txt +238 -241
- package/src/aria-snapshots/hackernews-raw.txt +265 -269
- package/src/assets/aria-labels-example.png +0 -0
- package/src/assets/aria-labels-github.png +0 -0
- package/src/assets/aria-labels-hacker-news.png +0 -0
- package/src/assets/aria-labels-old-reddit.png +0 -0
- package/src/assets/cursors/screen-studio/pointer-macos-tahoe-data-url.ts +5 -0
- package/src/assets/cursors/screen-studio/pointer-macos-tahoe.svg +18 -0
- package/src/cdp-log.ts +4 -1
- package/src/cdp-relay.ts +1059 -737
- package/src/cdp-session.ts +12 -3
- package/src/cdp-types.ts +51 -51
- package/src/clean-html.ts +4 -5
- package/src/cli.ts +82 -55
- package/src/create-logger.ts +5 -3
- package/src/debugger-examples-types.ts +4 -1
- package/src/debugger.ts +1 -5
- package/src/diff-utils.ts +2 -5
- package/src/editor-examples.ts +11 -1
- package/src/editor.ts +10 -2
- package/src/executor.ts +374 -73
- package/src/executor.unit.test.ts +48 -1
- package/src/extension-connection.test.ts +612 -488
- package/src/ffmpeg.ts +769 -0
- package/src/ghost-browser.ts +4 -6
- package/src/ghost-cursor-client.ts +369 -0
- package/src/ghost-cursor.ts +110 -0
- package/src/htmlrewrite.test.ts +6 -2
- package/src/htmlrewrite.ts +348 -386
- package/src/kill-port.ts +1 -3
- package/src/locator-selector.test.ts +115 -0
- package/src/mcp-client.ts +1 -1
- package/src/mcp.ts +21 -15
- package/src/on-mouse-action.test.ts +196 -0
- package/src/page-markdown.ts +7 -7
- package/src/protocol.ts +73 -57
- package/src/recording-ghost-cursor.ts +113 -0
- package/src/recording-relay.ts +20 -12
- package/src/relay-client.ts +85 -18
- package/src/relay-core.test.ts +1117 -578
- package/src/relay-navigation.test.ts +648 -483
- package/src/relay-session.test.ts +984 -929
- package/src/relay-state.test.ts +570 -0
- package/src/relay-state.ts +497 -0
- package/src/resource.md +21 -49
- package/src/scoped-fs.ts +9 -3
- package/src/screen-recording.test.ts +111 -0
- package/src/screen-recording.ts +256 -31
- package/src/skill.md +476 -396
- package/src/snapshot-tools.test.ts +580 -528
- package/src/snapshots/shadcn-ui-accessibility-full.md +8 -8
- package/src/snapshots/shadcn-ui-accessibility-interactive.md +8 -8
- package/src/start-relay-server.ts +14 -11
- package/src/styles-examples.ts +8 -1
- package/src/styles.ts +20 -21
- package/src/test-declarations.ts +6 -6
- package/src/test-utils.ts +104 -91
- package/src/utils.ts +2 -1
- package/src/wait-for-page-load.ts +6 -1
|
@@ -1,194 +1,195 @@
|
|
|
1
1
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
2
|
import { chromium, type Page } from '@xmorse/playwright-core'
|
|
3
|
+
import WebSocket from 'ws'
|
|
3
4
|
import { getCdpUrl } from './utils.js'
|
|
4
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
setupTestContext,
|
|
7
|
+
cleanupTestContext,
|
|
8
|
+
getExtensionServiceWorker,
|
|
9
|
+
type TestContext,
|
|
10
|
+
withTimeout,
|
|
11
|
+
createSimpleServer,
|
|
12
|
+
} from './test-utils.js'
|
|
5
13
|
import './test-declarations.js'
|
|
6
14
|
|
|
7
15
|
const TEST_PORT = 19992
|
|
8
16
|
|
|
9
17
|
describe('Relay Navigation Tests', () => {
|
|
10
|
-
|
|
18
|
+
let testCtx: TestContext | null = null
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
beforeAll(async () => {
|
|
21
|
+
testCtx = await setupTestContext({ port: TEST_PORT, tempDirPrefix: 'pw-nav-test-', toggleExtension: true })
|
|
22
|
+
}, 600000)
|
|
15
23
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
afterAll(async () => {
|
|
25
|
+
await cleanupTestContext(testCtx)
|
|
26
|
+
testCtx = null
|
|
27
|
+
})
|
|
20
28
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
const getBrowserContext = () => {
|
|
30
|
+
if (!testCtx?.browserContext) throw new Error('Browser not initialized')
|
|
31
|
+
return testCtx.browserContext
|
|
32
|
+
}
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
timeoutMs,
|
|
29
|
-
}: {
|
|
30
|
-
page: Page
|
|
31
|
-
timeoutMs: number
|
|
32
|
-
}) => {
|
|
33
|
-
const startTime = Date.now()
|
|
34
|
-
|
|
35
|
-
while (Date.now() - startTime < timeoutMs) {
|
|
36
|
-
try {
|
|
37
|
-
const readyState = await page.evaluate(() => {
|
|
38
|
-
return document.readyState
|
|
39
|
-
})
|
|
40
|
-
if (readyState !== 'loading') {
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
} catch (e) {
|
|
44
|
-
if (!(e instanceof Error) || !e.message.includes('Execution context was destroyed')) {
|
|
45
|
-
throw new Error('Failed while waiting for stable document readyState', { cause: e })
|
|
46
|
-
}
|
|
47
|
-
}
|
|
34
|
+
const waitForStableDocumentReadyState = async ({ page, timeoutMs }: { page: Page; timeoutMs: number }) => {
|
|
35
|
+
const startTime = Date.now()
|
|
48
36
|
|
|
49
|
-
|
|
37
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
38
|
+
try {
|
|
39
|
+
const readyState = await page.evaluate(() => {
|
|
40
|
+
return document.readyState
|
|
41
|
+
})
|
|
42
|
+
if (readyState !== 'loading') {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
if (!(e instanceof Error) || !e.message.includes('Execution context was destroyed')) {
|
|
47
|
+
throw new Error('Failed while waiting for stable document readyState', { cause: e })
|
|
50
48
|
}
|
|
49
|
+
}
|
|
51
50
|
|
|
52
|
-
|
|
51
|
+
await page.waitForTimeout(100)
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
throw new Error(`Timed out waiting for stable document readyState after ${timeoutMs}ms`)
|
|
55
|
+
}
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
61
|
-
const context = browser.contexts()[0]
|
|
57
|
+
it('should be usable after toggle with valid URL', async () => {
|
|
58
|
+
// Validates the extension waits for a non-empty URL before attaching.
|
|
62
59
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
})
|
|
60
|
+
const browserContext = getBrowserContext()
|
|
61
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
62
|
+
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
63
|
+
const context = browser.contexts()[0]
|
|
68
64
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
const server = await createSimpleServer({
|
|
66
|
+
routes: {
|
|
67
|
+
'/': '<!doctype html><html><body>ok</body></html>',
|
|
68
|
+
},
|
|
69
|
+
})
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
const page = await browserContext.newPage()
|
|
72
|
+
try {
|
|
73
|
+
await page.goto(server.baseUrl, { waitUntil: 'domcontentloaded' })
|
|
74
|
+
await page.bringToFront()
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
await globalThis.toggleExtensionForActiveTab()
|
|
78
|
-
})
|
|
76
|
+
const pagePromise = context.waitForEvent('page', { timeout: 5000 })
|
|
79
77
|
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
await serviceWorker.evaluate(async () => {
|
|
79
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
80
|
+
})
|
|
82
81
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
expect(targetPage.url()).toContain(server.baseUrl)
|
|
82
|
+
const targetPage = await pagePromise
|
|
83
|
+
console.log('Page URL when event fired:', targetPage.url())
|
|
86
84
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
expect(targetPage.url()).not.toBe('')
|
|
86
|
+
expect(targetPage.url()).not.toBe(':')
|
|
87
|
+
expect(targetPage.url()).toContain(server.baseUrl)
|
|
88
|
+
|
|
89
|
+
const result = await targetPage.evaluate(() => window.location.href)
|
|
90
|
+
expect(result).toContain(server.baseUrl)
|
|
91
|
+
} finally {
|
|
92
|
+
await browser.close()
|
|
93
|
+
await page.close()
|
|
94
|
+
await server.close()
|
|
95
|
+
}
|
|
96
|
+
}, 15000)
|
|
95
97
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
it('should expose iframe frames when connecting to an existing page over CDP', async () => {
|
|
99
|
+
const browserContext = getBrowserContext()
|
|
100
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
const childServer = await createSimpleServer({
|
|
103
|
+
routes: {
|
|
104
|
+
'/child.html': '<!doctype html><html><body>child</body></html>',
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
const childUrl = `${childServer.baseUrl}/child.html`
|
|
106
108
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
const parentServer = await createSimpleServer({
|
|
110
|
+
routes: {
|
|
111
|
+
'/': `<!doctype html><html><body><iframe src="${childUrl}"></iframe></body></html>`,
|
|
112
|
+
},
|
|
113
|
+
})
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}, 60000)
|
|
115
|
+
const page = await browserContext.newPage()
|
|
116
|
+
try {
|
|
117
|
+
await withTimeout({
|
|
118
|
+
promise: page.goto(parentServer.baseUrl, { waitUntil: 'domcontentloaded', timeout: 5000 }),
|
|
119
|
+
timeoutMs: 6000,
|
|
120
|
+
errorMessage: 'Timed out loading parent page for iframe test',
|
|
121
|
+
})
|
|
122
|
+
await withTimeout({
|
|
123
|
+
promise: page.frameLocator('iframe').locator('body').waitFor({ timeout: 5000 }),
|
|
124
|
+
timeoutMs: 6000,
|
|
125
|
+
errorMessage: 'Timed out waiting for iframe to attach in parent page',
|
|
126
|
+
})
|
|
127
|
+
expect(page.frames().map((frame) => frame.url())).toContain(childUrl)
|
|
128
|
+
await page.bringToFront()
|
|
129
|
+
|
|
130
|
+
await withTimeout({
|
|
131
|
+
promise: serviceWorker.evaluate(async () => {
|
|
132
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
133
|
+
}),
|
|
134
|
+
timeoutMs: 5000,
|
|
135
|
+
errorMessage: 'Timed out toggling extension for iframe test',
|
|
136
|
+
})
|
|
137
|
+
await new Promise((r) => {
|
|
138
|
+
setTimeout(r, 400)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
const browser = await withTimeout({
|
|
142
|
+
promise: chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT })),
|
|
143
|
+
timeoutMs: 5000,
|
|
144
|
+
errorMessage: 'Timed out connecting over CDP for iframe test',
|
|
145
|
+
})
|
|
146
|
+
const context = browser.contexts()[0]
|
|
147
|
+
const cdpPage = context.pages().find((candidate) => {
|
|
148
|
+
return candidate.url().startsWith(parentServer.baseUrl)
|
|
149
|
+
})
|
|
150
|
+
expect(cdpPage).toBeDefined()
|
|
151
|
+
|
|
152
|
+
const frames = cdpPage!.frames()
|
|
153
|
+
const childFrame = frames.find((frame) => {
|
|
154
|
+
return frame.url() === childUrl
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
expect(frames.length).toBe(2)
|
|
158
|
+
expect(childFrame).toBeDefined()
|
|
159
|
+
|
|
160
|
+
await withTimeout({
|
|
161
|
+
promise: browser.close(),
|
|
162
|
+
timeoutMs: 5000,
|
|
163
|
+
errorMessage: 'Timed out closing CDP browser for iframe test',
|
|
164
|
+
})
|
|
165
|
+
} finally {
|
|
166
|
+
await withTimeout({
|
|
167
|
+
promise: page.close(),
|
|
168
|
+
timeoutMs: 5000,
|
|
169
|
+
errorMessage: 'Timed out closing page for iframe test',
|
|
170
|
+
})
|
|
171
|
+
await Promise.all([parentServer.close(), childServer.close()])
|
|
172
|
+
}
|
|
173
|
+
}, 60000)
|
|
173
174
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
175
|
+
it('should resolve locators for cross-origin iframe that starts with empty src', async () => {
|
|
176
|
+
const browserContext = getBrowserContext()
|
|
177
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
177
178
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
179
|
+
const childServer = await createSimpleServer({
|
|
180
|
+
routes: {
|
|
181
|
+
'/login.html': '<!doctype html><html><body><button id="login-btn">Login</button></body></html>',
|
|
182
|
+
'/canvas.html': '<!doctype html><html><body><button id="canvas-btn">Canvas</button></body></html>',
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
const loginUrl = `${childServer.baseUrl}/login.html`
|
|
186
|
+
const canvasUrl = `${childServer.baseUrl}/canvas.html`
|
|
187
|
+
|
|
188
|
+
const parentServer = await createSimpleServer({
|
|
189
|
+
routes: {
|
|
190
|
+
// Reproduces Framer-like plugin iframes: attached with empty src first,
|
|
191
|
+
// then navigated cross-origin after auto-attach is active.
|
|
192
|
+
'/': `<!doctype html>
|
|
192
193
|
<html>
|
|
193
194
|
<body>
|
|
194
195
|
<iframe id="plugin-frame"></iframe>
|
|
@@ -203,194 +204,200 @@ describe('Relay Navigation Tests', () => {
|
|
|
203
204
|
</script>
|
|
204
205
|
</body>
|
|
205
206
|
</html>`,
|
|
206
|
-
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
const page = await browserContext.newPage()
|
|
211
|
+
try {
|
|
212
|
+
await withTimeout({
|
|
213
|
+
promise: page.goto(parentServer.baseUrl, { waitUntil: 'domcontentloaded', timeout: 5000 }),
|
|
214
|
+
timeoutMs: 6000,
|
|
215
|
+
errorMessage: 'Timed out loading parent page for empty-src iframe test',
|
|
216
|
+
})
|
|
217
|
+
await page.bringToFront()
|
|
218
|
+
|
|
219
|
+
await withTimeout({
|
|
220
|
+
promise: serviceWorker.evaluate(async () => {
|
|
221
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
222
|
+
}),
|
|
223
|
+
timeoutMs: 5000,
|
|
224
|
+
errorMessage: 'Timed out toggling extension for empty-src iframe test',
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
const browser = await withTimeout({
|
|
228
|
+
promise: chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT })),
|
|
229
|
+
timeoutMs: 5000,
|
|
230
|
+
errorMessage: 'Timed out connecting over CDP for empty-src iframe test',
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const context = browser.contexts()[0]
|
|
235
|
+
const cdpPage = context.pages().find((candidate) => {
|
|
236
|
+
return candidate.url().startsWith(parentServer.baseUrl)
|
|
207
237
|
})
|
|
238
|
+
expect(cdpPage).toBeDefined()
|
|
208
239
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
await page.bringToFront()
|
|
217
|
-
|
|
218
|
-
await withTimeout({
|
|
219
|
-
promise: serviceWorker.evaluate(async () => {
|
|
220
|
-
await globalThis.toggleExtensionForActiveTab()
|
|
221
|
-
}),
|
|
222
|
-
timeoutMs: 5000,
|
|
223
|
-
errorMessage: 'Timed out toggling extension for empty-src iframe test',
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
const browser = await withTimeout({
|
|
227
|
-
promise: chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT })),
|
|
228
|
-
timeoutMs: 5000,
|
|
229
|
-
errorMessage: 'Timed out connecting over CDP for empty-src iframe test',
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
try {
|
|
233
|
-
const context = browser.contexts()[0]
|
|
234
|
-
const cdpPage = context.pages().find((candidate) => {
|
|
235
|
-
return candidate.url().startsWith(parentServer.baseUrl)
|
|
236
|
-
})
|
|
237
|
-
expect(cdpPage).toBeDefined()
|
|
238
|
-
|
|
239
|
-
await withTimeout({
|
|
240
|
-
promise: page.evaluate(() => {
|
|
241
|
-
;(window as Window & { startPluginFlow?: () => void }).startPluginFlow?.()
|
|
242
|
-
}),
|
|
243
|
-
timeoutMs: 3000,
|
|
244
|
-
errorMessage: 'Timed out starting plugin iframe flow',
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
const pluginFrame = await withTimeout({
|
|
248
|
-
promise: (async () => {
|
|
249
|
-
for (let attempt = 0; attempt < 40; attempt += 1) {
|
|
250
|
-
const frame = cdpPage!.frames().find((candidate) => {
|
|
251
|
-
return candidate.url() === loginUrl || candidate.url() === canvasUrl
|
|
252
|
-
})
|
|
253
|
-
if (frame) {
|
|
254
|
-
return frame
|
|
255
|
-
}
|
|
256
|
-
await cdpPage!.waitForTimeout(100)
|
|
257
|
-
}
|
|
258
|
-
throw new Error('Plugin frame did not appear with expected URL')
|
|
259
|
-
})(),
|
|
260
|
-
timeoutMs: 5000,
|
|
261
|
-
errorMessage: 'Timed out waiting for plugin frame URL in empty-src iframe test',
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
await withTimeout({
|
|
265
|
-
promise: pluginFrame.locator('button').first().waitFor({ state: 'attached' }),
|
|
266
|
-
timeoutMs: 5000,
|
|
267
|
-
errorMessage: 'Timed out waiting for button locator in empty-src iframe test',
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
const buttonCount = await pluginFrame.locator('button').count()
|
|
271
|
-
expect(buttonCount).toBe(1)
|
|
272
|
-
} finally {
|
|
273
|
-
await withTimeout({
|
|
274
|
-
promise: browser.close(),
|
|
275
|
-
timeoutMs: 5000,
|
|
276
|
-
errorMessage: 'Timed out closing CDP browser for empty-src iframe test',
|
|
277
|
-
})
|
|
278
|
-
}
|
|
279
|
-
} finally {
|
|
280
|
-
await withTimeout({
|
|
281
|
-
promise: page.close(),
|
|
282
|
-
timeoutMs: 5000,
|
|
283
|
-
errorMessage: 'Timed out closing page for empty-src iframe test',
|
|
284
|
-
})
|
|
285
|
-
await Promise.all([
|
|
286
|
-
parentServer.close(),
|
|
287
|
-
childServer.close(),
|
|
288
|
-
])
|
|
289
|
-
}
|
|
290
|
-
}, 60000)
|
|
240
|
+
await withTimeout({
|
|
241
|
+
promise: page.evaluate(() => {
|
|
242
|
+
;(window as Window & { startPluginFlow?: () => void }).startPluginFlow?.()
|
|
243
|
+
}),
|
|
244
|
+
timeoutMs: 3000,
|
|
245
|
+
errorMessage: 'Timed out starting plugin iframe flow',
|
|
246
|
+
})
|
|
291
247
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
248
|
+
const pluginFrame = await withTimeout({
|
|
249
|
+
promise: (async () => {
|
|
250
|
+
for (let attempt = 0; attempt < 40; attempt += 1) {
|
|
251
|
+
const frame = cdpPage!.frames().find((candidate) => {
|
|
252
|
+
return candidate.url() === loginUrl || candidate.url() === canvasUrl
|
|
253
|
+
})
|
|
254
|
+
if (frame) {
|
|
255
|
+
return frame
|
|
256
|
+
}
|
|
257
|
+
await cdpPage!.waitForTimeout(100)
|
|
258
|
+
}
|
|
259
|
+
throw new Error('Plugin frame did not appear with expected URL')
|
|
260
|
+
})(),
|
|
261
|
+
timeoutMs: 5000,
|
|
262
|
+
errorMessage: 'Timed out waiting for plugin frame URL in empty-src iframe test',
|
|
263
|
+
})
|
|
295
264
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
265
|
+
await withTimeout({
|
|
266
|
+
promise: pluginFrame.locator('button').first().waitFor({ state: 'attached' }),
|
|
267
|
+
timeoutMs: 5000,
|
|
268
|
+
errorMessage: 'Timed out waiting for button locator in empty-src iframe test',
|
|
269
|
+
})
|
|
299
270
|
|
|
300
|
-
await
|
|
301
|
-
|
|
271
|
+
const buttonCount = await pluginFrame.locator('button').count()
|
|
272
|
+
expect(buttonCount).toBe(1)
|
|
273
|
+
} finally {
|
|
274
|
+
await withTimeout({
|
|
275
|
+
promise: browser.close(),
|
|
276
|
+
timeoutMs: 5000,
|
|
277
|
+
errorMessage: 'Timed out closing CDP browser for empty-src iframe test',
|
|
302
278
|
})
|
|
279
|
+
}
|
|
280
|
+
} finally {
|
|
281
|
+
await withTimeout({
|
|
282
|
+
promise: page.close(),
|
|
283
|
+
timeoutMs: 5000,
|
|
284
|
+
errorMessage: 'Timed out closing page for empty-src iframe test',
|
|
285
|
+
})
|
|
286
|
+
await Promise.all([parentServer.close(), childServer.close()])
|
|
287
|
+
}
|
|
288
|
+
}, 60000)
|
|
303
289
|
|
|
304
|
-
|
|
305
|
-
|
|
290
|
+
it('should have non-empty URLs when connecting to already-loaded pages', async () => {
|
|
291
|
+
const _browserContext = getBrowserContext()
|
|
292
|
+
const serviceWorker = await getExtensionServiceWorker(_browserContext)
|
|
306
293
|
|
|
307
|
-
|
|
308
|
-
|
|
294
|
+
const page = await _browserContext.newPage()
|
|
295
|
+
await page.goto('https://discord.com/login', { waitUntil: 'load' })
|
|
296
|
+
await page.bringToFront()
|
|
309
297
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
expect(p.url()).not.toBe(':')
|
|
314
|
-
expect(p.url()).not.toBeUndefined()
|
|
315
|
-
}
|
|
298
|
+
await serviceWorker.evaluate(async () => {
|
|
299
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
300
|
+
})
|
|
316
301
|
|
|
317
|
-
|
|
318
|
-
|
|
302
|
+
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
303
|
+
const context = browser.contexts()[0]
|
|
319
304
|
|
|
320
|
-
|
|
321
|
-
|
|
305
|
+
const pages = context.pages()
|
|
306
|
+
console.log(
|
|
307
|
+
'All page URLs:',
|
|
308
|
+
pages.map((p) => p.url()),
|
|
309
|
+
)
|
|
322
310
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
311
|
+
expect(pages.length).toBeGreaterThan(0)
|
|
312
|
+
for (const p of pages) {
|
|
313
|
+
expect(p.url()).not.toBe('')
|
|
314
|
+
expect(p.url()).not.toBe(':')
|
|
315
|
+
expect(p.url()).not.toBeUndefined()
|
|
316
|
+
}
|
|
326
317
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
318
|
+
const discordPage = pages.find((p) => p.url().includes('discord.com'))
|
|
319
|
+
expect(discordPage).toBeDefined()
|
|
330
320
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
await page.goto(initialUrl)
|
|
334
|
-
await page.bringToFront()
|
|
321
|
+
const result = await discordPage!.evaluate(() => window.location.href)
|
|
322
|
+
expect(result).toContain('discord.com')
|
|
335
323
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
324
|
+
await browser.close()
|
|
325
|
+
await page.close()
|
|
326
|
+
}, 60000)
|
|
339
327
|
|
|
340
|
-
|
|
328
|
+
it('should navigate to notion without hanging', async () => {
|
|
329
|
+
const browserContext = getBrowserContext()
|
|
330
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
341
331
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
332
|
+
const page = await browserContext.newPage()
|
|
333
|
+
const initialUrl = 'https://example.com/notion-repro'
|
|
334
|
+
await page.goto(initialUrl)
|
|
335
|
+
await page.bringToFront()
|
|
345
336
|
|
|
346
|
-
|
|
337
|
+
await serviceWorker.evaluate(async () => {
|
|
338
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
339
|
+
})
|
|
347
340
|
|
|
348
|
-
|
|
349
|
-
const responseUrl = response?.url() ?? ''
|
|
350
|
-
expect(responseUrl).toMatch(/notion\.(so|com)/)
|
|
351
|
-
expect(currentUrl).toMatch(/notion\.(so|com)/)
|
|
352
|
-
expect(await cdpPage!.evaluate(() => document.readyState)).not.toBe('loading')
|
|
341
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
353
342
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
343
|
+
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
344
|
+
const cdpPage = browser
|
|
345
|
+
.contexts()[0]
|
|
346
|
+
.pages()
|
|
347
|
+
.find((p) => p.url() === initialUrl)
|
|
348
|
+
expect(cdpPage).toBeDefined()
|
|
357
349
|
|
|
358
|
-
|
|
359
|
-
const browserContext = getBrowserContext()
|
|
360
|
-
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
350
|
+
const response = await cdpPage!.goto('https://www.notion.so', { waitUntil: 'domcontentloaded', timeout: 20000 })
|
|
361
351
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
352
|
+
const currentUrl = cdpPage!.url()
|
|
353
|
+
const responseUrl = response?.url() ?? ''
|
|
354
|
+
expect(responseUrl).toMatch(/notion\.(so|com)/)
|
|
355
|
+
expect(currentUrl).toMatch(/notion\.(so|com)/)
|
|
356
|
+
expect(await cdpPage!.evaluate(() => document.readyState)).not.toBe('loading')
|
|
365
357
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
358
|
+
await browser.close()
|
|
359
|
+
await page.close()
|
|
360
|
+
}, 60000)
|
|
369
361
|
|
|
370
|
-
|
|
362
|
+
it('should navigate to youtube without hanging', async () => {
|
|
363
|
+
const browserContext = getBrowserContext()
|
|
364
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
371
365
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
366
|
+
const page = await browserContext.newPage()
|
|
367
|
+
await page.goto('about:blank')
|
|
368
|
+
await page.bringToFront()
|
|
375
369
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
370
|
+
await serviceWorker.evaluate(async () => {
|
|
371
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
375
|
+
|
|
376
|
+
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
377
|
+
const cdpPage = browser
|
|
378
|
+
.contexts()[0]
|
|
379
|
+
.pages()
|
|
380
|
+
.find((p) => p.url().includes('about:'))
|
|
381
|
+
expect(cdpPage).toBeDefined()
|
|
379
382
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
+
const response = await cdpPage!.goto('https://www.youtube.com', { waitUntil: 'domcontentloaded', timeout: 20000 })
|
|
384
|
+
const currentUrl = cdpPage!.url()
|
|
385
|
+
const responseUrl = response?.url() ?? ''
|
|
383
386
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
}
|
|
387
|
+
expect(responseUrl).toContain('youtube')
|
|
388
|
+
expect(currentUrl).toContain('youtube')
|
|
389
|
+
await waitForStableDocumentReadyState({ page: cdpPage!, timeoutMs: 5000 })
|
|
387
390
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
+
await browser.close()
|
|
392
|
+
await page.close()
|
|
393
|
+
}, 60000)
|
|
391
394
|
|
|
392
|
-
|
|
393
|
-
|
|
395
|
+
it('should maintain correct page.url() with iframe-heavy pages', async () => {
|
|
396
|
+
const browserContext = getBrowserContext()
|
|
397
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
398
|
+
|
|
399
|
+
const page = await browserContext.newPage()
|
|
400
|
+
await page.setContent(`
|
|
394
401
|
<html>
|
|
395
402
|
<head><title>Iframe Test Page</title></head>
|
|
396
403
|
<body>
|
|
@@ -401,195 +408,353 @@ describe('Relay Navigation Tests', () => {
|
|
|
401
408
|
</body>
|
|
402
409
|
</html>
|
|
403
410
|
`)
|
|
404
|
-
|
|
411
|
+
await page.bringToFront()
|
|
405
412
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
413
|
+
await serviceWorker.evaluate(async () => {
|
|
414
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
415
|
+
})
|
|
409
416
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
417
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
418
|
+
|
|
419
|
+
for (let i = 0; i < 3; i++) {
|
|
420
|
+
const browser = await chromium.connectOverCDP(getCdpUrl({ port: TEST_PORT }))
|
|
421
|
+
const pages = browser.contexts()[0].pages()
|
|
422
|
+
let iframePage
|
|
423
|
+
for (const p of pages) {
|
|
424
|
+
const html = await p.content()
|
|
425
|
+
if (html.includes('Iframe Heavy Page')) {
|
|
426
|
+
iframePage = p
|
|
427
|
+
break
|
|
428
|
+
}
|
|
429
|
+
}
|
|
423
430
|
|
|
424
|
-
|
|
425
|
-
|
|
431
|
+
expect(iframePage).toBeDefined()
|
|
432
|
+
expect(iframePage?.url()).toContain('about:')
|
|
426
433
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
434
|
+
await browser.close()
|
|
435
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
436
|
+
}
|
|
430
437
|
|
|
431
|
-
|
|
432
|
-
|
|
438
|
+
await page.close()
|
|
439
|
+
}, 30000)
|
|
433
440
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
441
|
+
it('should work with stagehand', async () => {
|
|
442
|
+
const browserContext = getBrowserContext()
|
|
443
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
437
444
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
445
|
+
await serviceWorker.evaluate(async () => {
|
|
446
|
+
await globalThis.disconnectEverything()
|
|
447
|
+
})
|
|
448
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
442
449
|
|
|
443
|
-
|
|
450
|
+
const targetUrl = 'https://example.com/'
|
|
444
451
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
452
|
+
const enableResult = await serviceWorker.evaluate(async (url) => {
|
|
453
|
+
const tab = await chrome.tabs.create({ url, active: true })
|
|
454
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
455
|
+
return await globalThis.toggleExtensionForActiveTab()
|
|
456
|
+
}, targetUrl)
|
|
450
457
|
|
|
451
|
-
|
|
452
|
-
|
|
458
|
+
console.log('Extension enabled:', enableResult)
|
|
459
|
+
expect(enableResult.isConnected).toBe(true)
|
|
453
460
|
|
|
454
|
-
|
|
461
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
455
462
|
|
|
456
|
-
|
|
463
|
+
const { Stagehand } = await import('@browserbasehq/stagehand')
|
|
457
464
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
465
|
+
const stagehand = new Stagehand({
|
|
466
|
+
env: 'LOCAL',
|
|
467
|
+
verbose: 1,
|
|
468
|
+
disablePino: true,
|
|
469
|
+
localBrowserLaunchOptions: {
|
|
470
|
+
cdpUrl: getCdpUrl({ port: TEST_PORT }),
|
|
471
|
+
},
|
|
472
|
+
})
|
|
466
473
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
474
|
+
console.log('Initializing Stagehand...')
|
|
475
|
+
await stagehand.init()
|
|
476
|
+
console.log('Stagehand initialized')
|
|
470
477
|
|
|
471
|
-
|
|
472
|
-
|
|
478
|
+
const context = stagehand.context
|
|
479
|
+
expect(context).toBeDefined()
|
|
473
480
|
|
|
474
|
-
|
|
475
|
-
|
|
481
|
+
const pages = context.pages()
|
|
482
|
+
console.log(
|
|
483
|
+
'Stagehand pages:',
|
|
484
|
+
pages.length,
|
|
485
|
+
pages.map((p) => p.url()),
|
|
486
|
+
)
|
|
476
487
|
|
|
477
|
-
|
|
478
|
-
|
|
488
|
+
const stagehandPage = pages.find((p) => p.url().includes('example.com'))
|
|
489
|
+
expect(stagehandPage).toBeDefined()
|
|
479
490
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
491
|
+
const url = stagehandPage!.url()
|
|
492
|
+
console.log('Stagehand page URL:', url)
|
|
493
|
+
expect(url).toContain('example.com')
|
|
483
494
|
|
|
484
|
-
|
|
485
|
-
|
|
495
|
+
await stagehand.close()
|
|
496
|
+
}, 60000)
|
|
486
497
|
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
498
|
+
it('should expose CDP discovery endpoints /json/version and /json/list', async () => {
|
|
499
|
+
const browserContext = getBrowserContext()
|
|
500
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
490
501
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
502
|
+
const page = await browserContext.newPage()
|
|
503
|
+
await page.goto('https://example.com')
|
|
504
|
+
await page.bringToFront()
|
|
494
505
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
506
|
+
await serviceWorker.evaluate(async () => {
|
|
507
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
508
|
+
})
|
|
509
|
+
await new Promise((r) => setTimeout(r, 200))
|
|
510
|
+
|
|
511
|
+
// Test /json/version
|
|
512
|
+
const versionRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json/version`)
|
|
513
|
+
expect(versionRes.status).toBe(200)
|
|
514
|
+
const versionJson = (await versionRes.json()) as { webSocketDebuggerUrl: string }
|
|
515
|
+
expect(versionJson).toMatchObject({
|
|
516
|
+
Browser: expect.stringContaining('Playwriter/'),
|
|
517
|
+
'Protocol-Version': '1.3',
|
|
518
|
+
webSocketDebuggerUrl: expect.stringContaining('ws://'),
|
|
519
|
+
})
|
|
520
|
+
expect(versionJson.webSocketDebuggerUrl).toContain(`127.0.0.1:${TEST_PORT}/cdp`)
|
|
521
|
+
|
|
522
|
+
// Test /json/version/ (trailing slash)
|
|
523
|
+
const versionSlashRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json/version/`)
|
|
524
|
+
expect(versionSlashRes.status).toBe(200)
|
|
525
|
+
|
|
526
|
+
// Test /json/list
|
|
527
|
+
const listRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json/list`)
|
|
528
|
+
expect(listRes.status).toBe(200)
|
|
529
|
+
const listJson = (await listRes.json()) as Array<{ url?: string }>
|
|
530
|
+
expect(Array.isArray(listJson)).toBe(true)
|
|
531
|
+
expect(listJson.length).toBeGreaterThan(0)
|
|
532
|
+
|
|
533
|
+
const examplePage = listJson.find((t) => t.url?.includes('example.com'))
|
|
534
|
+
expect(examplePage).toBeDefined()
|
|
535
|
+
expect(examplePage).toMatchObject({
|
|
536
|
+
id: expect.any(String),
|
|
537
|
+
type: 'page',
|
|
538
|
+
url: expect.stringContaining('example.com'),
|
|
539
|
+
webSocketDebuggerUrl: expect.stringContaining('ws://'),
|
|
540
|
+
})
|
|
530
541
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
// Test PUT method (Chrome 66+ prefers PUT)
|
|
538
|
-
const putRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json/version`, { method: 'PUT' })
|
|
539
|
-
expect(putRes.status).toBe(200)
|
|
540
|
-
|
|
541
|
-
await page.close()
|
|
542
|
-
}, 60000)
|
|
543
|
-
|
|
544
|
-
// Skip: chrome.tabCapture.getMediaStreamId() requires activeTab permission
|
|
545
|
-
it.skip('should record screen with navigation using chrome.tabCapture', async () => {
|
|
546
|
-
const browserContext = getBrowserContext()
|
|
547
|
-
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
548
|
-
const path = await import('node:path')
|
|
549
|
-
const fs = await import('node:fs')
|
|
550
|
-
|
|
551
|
-
const recordingPage = await browserContext.newPage()
|
|
552
|
-
await recordingPage.goto('https://news.ycombinator.com/', { waitUntil: 'domcontentloaded' })
|
|
553
|
-
await recordingPage.bringToFront()
|
|
554
|
-
|
|
555
|
-
await serviceWorker.evaluate(async () => {
|
|
556
|
-
await globalThis.toggleExtensionForActiveTab()
|
|
557
|
-
})
|
|
558
|
-
await new Promise(r => setTimeout(r, 200))
|
|
542
|
+
// Test /json (alias for /json/list)
|
|
543
|
+
const jsonRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json`)
|
|
544
|
+
expect(jsonRes.status).toBe(200)
|
|
545
|
+
const jsonData = await jsonRes.json()
|
|
546
|
+
expect(Array.isArray(jsonData)).toBe(true)
|
|
559
547
|
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
548
|
+
// Test PUT method (Chrome 66+ prefers PUT)
|
|
549
|
+
const putRes = await fetch(`http://127.0.0.1:${TEST_PORT}/json/version`, { method: 'PUT' })
|
|
550
|
+
expect(putRes.status).toBe(200)
|
|
551
|
+
|
|
552
|
+
await page.close()
|
|
553
|
+
}, 60000)
|
|
554
|
+
|
|
555
|
+
// Skip: chrome.tabCapture.getMediaStreamId() requires activeTab permission
|
|
556
|
+
it.skip('should record screen with navigation using chrome.tabCapture', async () => {
|
|
557
|
+
const browserContext = getBrowserContext()
|
|
558
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
559
|
+
const path = await import('node:path')
|
|
560
|
+
const fs = await import('node:fs')
|
|
561
|
+
|
|
562
|
+
const recordingPage = await browserContext.newPage()
|
|
563
|
+
await recordingPage.goto('https://news.ycombinator.com/', { waitUntil: 'domcontentloaded' })
|
|
564
|
+
await recordingPage.bringToFront()
|
|
565
|
+
|
|
566
|
+
await serviceWorker.evaluate(async () => {
|
|
567
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
568
|
+
})
|
|
569
|
+
await new Promise((r) => setTimeout(r, 200))
|
|
570
|
+
|
|
571
|
+
const outputPath = path.join(process.cwd(), 'tmp', 'test-recording.mp4')
|
|
572
|
+
if (!fs.existsSync(path.dirname(outputPath))) {
|
|
573
|
+
fs.mkdirSync(path.dirname(outputPath), { recursive: true })
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const { startRecording, stopRecording, isRecording } = await import('./screen-recording.js')
|
|
564
577
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
578
|
+
const startResult = await startRecording({
|
|
579
|
+
page: recordingPage,
|
|
580
|
+
outputPath,
|
|
581
|
+
frameRate: 30,
|
|
582
|
+
audio: false,
|
|
583
|
+
videoBitsPerSecond: 1500000,
|
|
584
|
+
relayPort: TEST_PORT,
|
|
585
|
+
})
|
|
586
|
+
expect(startResult.isRecording).toBe(true)
|
|
587
|
+
|
|
588
|
+
await recordingPage.locator('.titleline a').first().click()
|
|
589
|
+
await recordingPage.waitForLoadState('domcontentloaded')
|
|
590
|
+
await new Promise((r) => setTimeout(r, 500))
|
|
591
|
+
|
|
592
|
+
await recordingPage.goBack()
|
|
593
|
+
await recordingPage.waitForLoadState('domcontentloaded')
|
|
594
|
+
|
|
595
|
+
const status = await isRecording({ page: recordingPage, relayPort: TEST_PORT })
|
|
596
|
+
expect(status.isRecording).toBe(true)
|
|
597
|
+
|
|
598
|
+
const stopResult = await stopRecording({ page: recordingPage, relayPort: TEST_PORT })
|
|
599
|
+
expect(stopResult.path).toBe(outputPath)
|
|
600
|
+
expect(stopResult.size).toBeGreaterThan(10000)
|
|
601
|
+
expect(fs.existsSync(outputPath)).toBe(true)
|
|
602
|
+
|
|
603
|
+
// Create a sped-up demo video from the recording.
|
|
604
|
+
// We fake executionTimestamps since this test calls screen-recording
|
|
605
|
+
// directly (not via executor sandbox which tracks them automatically).
|
|
606
|
+
const { createDemoVideo } = await import('./ffmpeg.js')
|
|
607
|
+
const demoPath = await createDemoVideo({
|
|
608
|
+
recordingPath: outputPath,
|
|
609
|
+
durationMs: stopResult.duration,
|
|
610
|
+
executionTimestamps: [
|
|
611
|
+
// Simulate two interactions with an idle gap between them
|
|
612
|
+
{ start: 0.5, end: 1.5 },
|
|
613
|
+
{ start: 3, end: 4 },
|
|
614
|
+
],
|
|
615
|
+
speed: 4,
|
|
616
|
+
})
|
|
617
|
+
expect(fs.existsSync(demoPath)).toBe(true)
|
|
618
|
+
expect(demoPath).toContain('-demo')
|
|
619
|
+
|
|
620
|
+
// Verify the demo video is smaller (idle sections were sped up)
|
|
621
|
+
const demoSize = fs.statSync(demoPath).size
|
|
622
|
+
expect(demoSize).toBeGreaterThan(0)
|
|
623
|
+
console.log(`Recording: ${stopResult.size} bytes, Demo: ${demoSize} bytes`)
|
|
624
|
+
|
|
625
|
+
await recordingPage.close()
|
|
626
|
+
fs.unlinkSync(outputPath)
|
|
627
|
+
fs.unlinkSync(demoPath)
|
|
628
|
+
}, 60000)
|
|
629
|
+
|
|
630
|
+
// Regression test for https://github.com/remorses/playwriter/issues/40
|
|
631
|
+
// When Playwright sends Target.detachFromTarget on the root CDP session (no top-level
|
|
632
|
+
// sessionId), the extension must still route the command by looking at params.sessionId.
|
|
633
|
+
// Previously the extension threw "No tab found for method Target.detachFromTarget"
|
|
634
|
+
// because it only checked the top-level sessionId for routing, which is absent on root
|
|
635
|
+
// session commands. This caused cascading disconnects and instability.
|
|
636
|
+
it('should route Target.detachFromTarget without top-level sessionId (issue #40)', async () => {
|
|
637
|
+
const browserContext = getBrowserContext()
|
|
638
|
+
const serviceWorker = await getExtensionServiceWorker(browserContext)
|
|
639
|
+
|
|
640
|
+
const server = await createSimpleServer({
|
|
641
|
+
routes: { '/': '<!doctype html><html><body>detach test</body></html>' },
|
|
642
|
+
})
|
|
643
|
+
|
|
644
|
+
const page = await browserContext.newPage()
|
|
645
|
+
try {
|
|
646
|
+
await page.goto(server.baseUrl, { waitUntil: 'domcontentloaded' })
|
|
647
|
+
await page.bringToFront()
|
|
648
|
+
|
|
649
|
+
await withTimeout({
|
|
650
|
+
promise: serviceWorker.evaluate(async () => {
|
|
651
|
+
await globalThis.toggleExtensionForActiveTab()
|
|
652
|
+
}),
|
|
653
|
+
timeoutMs: 5000,
|
|
654
|
+
errorMessage: 'Timed out toggling extension for detach test',
|
|
655
|
+
})
|
|
656
|
+
await new Promise((r) => {
|
|
657
|
+
setTimeout(r, 400)
|
|
658
|
+
})
|
|
659
|
+
|
|
660
|
+
// Connect a raw WebSocket to the relay — this lets us send CDP messages
|
|
661
|
+
// exactly as they appear on the wire, without Playwright adding sessionId.
|
|
662
|
+
const ws = new WebSocket(`ws://localhost:${TEST_PORT}/cdp/test-detach-raw`)
|
|
663
|
+
await new Promise<void>((resolve, reject) => {
|
|
664
|
+
ws.on('open', () => {
|
|
665
|
+
resolve()
|
|
574
666
|
})
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
667
|
+
ws.on('error', reject)
|
|
668
|
+
})
|
|
669
|
+
|
|
670
|
+
let nextId = 1
|
|
671
|
+
const sendCdp = <T = unknown>(msg: Record<string, unknown>): Promise<T> => {
|
|
672
|
+
return new Promise((resolve, reject) => {
|
|
673
|
+
const id = nextId++
|
|
674
|
+
const timeout = setTimeout(() => {
|
|
675
|
+
ws.off('message', handler)
|
|
676
|
+
reject(new Error(`CDP response timeout for id ${id}`))
|
|
677
|
+
}, 5000)
|
|
678
|
+
|
|
679
|
+
const handler = (data: WebSocket.RawData) => {
|
|
680
|
+
const parsed = JSON.parse(data.toString())
|
|
681
|
+
if (parsed.id === id) {
|
|
682
|
+
ws.off('message', handler)
|
|
683
|
+
clearTimeout(timeout)
|
|
684
|
+
resolve(parsed as T)
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
ws.on('message', handler)
|
|
688
|
+
ws.send(JSON.stringify({ id, ...msg }))
|
|
689
|
+
})
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// Collect async events from the relay
|
|
693
|
+
const events: Array<{ method: string; params: Record<string, unknown>; sessionId?: string }> = []
|
|
694
|
+
ws.on('message', (data) => {
|
|
695
|
+
const msg = JSON.parse(data.toString())
|
|
696
|
+
if (!msg.id && msg.method) {
|
|
697
|
+
events.push(msg)
|
|
698
|
+
}
|
|
699
|
+
})
|
|
700
|
+
|
|
701
|
+
// Trigger Target.setAutoAttach so the relay sends Target.attachedToTarget for
|
|
702
|
+
// all connected tabs. This gives us the page's pw-tab-* sessionId.
|
|
703
|
+
await sendCdp({
|
|
704
|
+
method: 'Target.setAutoAttach',
|
|
705
|
+
params: { autoAttach: true, waitForDebuggerOnStart: false, flatten: true },
|
|
706
|
+
})
|
|
707
|
+
|
|
708
|
+
// Wait for events to arrive
|
|
709
|
+
await new Promise((r) => {
|
|
710
|
+
setTimeout(r, 500)
|
|
711
|
+
})
|
|
712
|
+
|
|
713
|
+
// Filter for the specific page target by URL to avoid grabbing wrong sessions
|
|
714
|
+
// (welcome tab, extension pages, etc.)
|
|
715
|
+
type AttachParams = { sessionId?: string; targetInfo?: { type?: string; url?: string } }
|
|
716
|
+
const attachEvent = events.find((e) => {
|
|
717
|
+
if (e.method !== 'Target.attachedToTarget') {
|
|
718
|
+
return false
|
|
719
|
+
}
|
|
720
|
+
const p = e.params as AttachParams
|
|
721
|
+
return p.targetInfo?.type === 'page' && p.targetInfo?.url?.startsWith(server.baseUrl)
|
|
722
|
+
})
|
|
723
|
+
expect(attachEvent).toBeDefined()
|
|
724
|
+
const pageSessionId = (attachEvent!.params as AttachParams).sessionId
|
|
725
|
+
expect(pageSessionId).toBeTruthy()
|
|
726
|
+
|
|
727
|
+
// Verify the session is usable before detach — send a command that requires routing.
|
|
728
|
+
const evalBefore = await sendCdp<{ id: number; error?: { message: string }; result?: unknown }>({
|
|
729
|
+
method: 'Runtime.evaluate',
|
|
730
|
+
sessionId: pageSessionId,
|
|
731
|
+
params: { expression: '1 + 1', returnByValue: true },
|
|
732
|
+
})
|
|
733
|
+
expect(evalBefore.error).toBeUndefined()
|
|
734
|
+
expect((evalBefore.result as { result?: { value?: number } })?.result?.value).toBe(2)
|
|
735
|
+
|
|
736
|
+
// NOW: send Target.detachFromTarget WITHOUT a top-level sessionId.
|
|
737
|
+
// This is the exact wire format Playwright uses when sending on the root session
|
|
738
|
+
// (e.g. from CRSession.detach() where _parentSession is the root browser session).
|
|
739
|
+
// The extension must route this by looking at params.sessionId.
|
|
740
|
+
const detachResult = await sendCdp<{ id: number; error?: { message: string }; result?: unknown }>({
|
|
741
|
+
method: 'Target.detachFromTarget',
|
|
742
|
+
// Intentionally NO sessionId field — this is the root session
|
|
743
|
+
params: { sessionId: pageSessionId },
|
|
744
|
+
})
|
|
745
|
+
|
|
746
|
+
// Must not fail with extension routing error — the command must reach Chrome.
|
|
747
|
+
// Chrome returns "No session with given id" because pw-tab-* is a virtual session
|
|
748
|
+
// managed by the relay, not a real Chrome CDP session. This is expected — the key
|
|
749
|
+
// proof is that the extension routed the command to Chrome instead of throwing
|
|
750
|
+
// "No tab found" at the routing layer.
|
|
751
|
+
expect(detachResult.error?.message).not.toContain('No tab found')
|
|
752
|
+
expect(detachResult.error?.message).toContain('No session with given id')
|
|
753
|
+
|
|
754
|
+
ws.close()
|
|
755
|
+
} finally {
|
|
756
|
+
await page.close()
|
|
757
|
+
await server.close()
|
|
758
|
+
}
|
|
759
|
+
}, 30000)
|
|
595
760
|
})
|