@@ -30,9 +30,15 @@ type BrowserWithExtension = {
30
30
launch : ( mode ?: 'disable-extension' ) => Promise < BrowserContext > ;
31
31
} ;
32
32
33
- const test = base . extend < { browserWithExtension : BrowserWithExtension , pathToExtension : string } > ( {
34
- pathToExtension : async ( ) => {
35
- return fileURLToPath ( new URL ( '../dist' , import . meta. url ) ) ;
33
+ type TestFixtures = {
34
+ browserWithExtension : BrowserWithExtension ,
35
+ pathToExtension : string ,
36
+ useShortConnectionTimeout : ( timeoutMs : number ) => void
37
+ } ;
38
+
39
+ const test = base . extend < TestFixtures > ( {
40
+ pathToExtension : async ( { } , use ) => {
41
+ await use ( fileURLToPath ( new URL ( '../dist' , import . meta. url ) ) ) ;
36
42
} ,
37
43
38
44
browserWithExtension : async ( { mcpBrowser, pathToExtension } , use , testInfo ) => {
@@ -65,9 +71,16 @@ const test = base.extend<{ browserWithExtension: BrowserWithExtension, pathToExt
65
71
return browserContext ;
66
72
}
67
73
} ) ;
68
-
69
74
await browserContext ?. close ( ) ;
70
75
} ,
76
+
77
+ useShortConnectionTimeout : async ( { } , use ) => {
78
+ await use ( ( timeoutMs : number ) => {
79
+ process . env . PWMCP_TEST_CONNECTION_TIMEOUT = timeoutMs . toString ( ) ;
80
+ } ) ;
81
+ process . env . PWMCP_TEST_CONNECTION_TIMEOUT = undefined ;
82
+ } ,
83
+
71
84
} ) ;
72
85
73
86
async function startAndCallConnectTool ( browserWithExtension : BrowserWithExtension , startClient : StartClient ) : Promise < Client > {
@@ -104,6 +117,21 @@ async function startWithExtensionFlag(browserWithExtension: BrowserWithExtension
104
117
return client ;
105
118
}
106
119
120
+ const testWithOldVersion = test . extend ( {
121
+ pathToExtension : async ( { } , use , testInfo ) => {
122
+ const extensionDir = testInfo . outputPath ( 'extension' ) ;
123
+ const oldPath = fileURLToPath ( new URL ( '../dist' , import . meta. url ) ) ;
124
+
125
+ await fs . promises . cp ( oldPath , extensionDir , { recursive : true } ) ;
126
+ const manifestPath = path . join ( extensionDir , 'manifest.json' ) ;
127
+ const manifest = JSON . parse ( await fs . promises . readFile ( manifestPath , 'utf8' ) ) ;
128
+ manifest . version = '0.0.1' ;
129
+ await fs . promises . writeFile ( manifestPath , JSON . stringify ( manifest , null , 2 ) + '\n' ) ;
130
+
131
+ await use ( extensionDir ) ;
132
+ } ,
133
+ } ) ;
134
+
107
135
for ( const [ mode , startClientMethod ] of [
108
136
[ 'connect-tool' , startAndCallConnectTool ] ,
109
137
[ 'extension-flag' , startWithExtensionFlag ] ,
@@ -165,8 +193,8 @@ for (const [mode, startClientMethod] of [
165
193
expect ( browserContext . pages ( ) ) . toHaveLength ( 4 ) ;
166
194
} ) ;
167
195
168
- test ( `extension not installed timeout (${ mode } )` , async ( { browserWithExtension, startClient, server } ) => {
169
- process . env . PWMCP_TEST_CONNECTION_TIMEOUT = ' 100' ;
196
+ test ( `extension not installed timeout (${ mode } )` , async ( { browserWithExtension, startClient, server, useShortConnectionTimeout } ) => {
197
+ useShortConnectionTimeout ( 100 ) ;
170
198
171
199
const browserContext = await browserWithExtension . launch ( ) ;
172
200
@@ -185,40 +213,32 @@ for (const [mode, startClientMethod] of [
185
213
} ) ;
186
214
187
215
await confirmationPagePromise ;
188
-
189
- process . env . PWMCP_TEST_CONNECTION_TIMEOUT = undefined ;
190
216
} ) ;
191
217
192
- }
218
+ testWithOldVersion ( `extension version mismatch (${ mode } )` , async ( { browserWithExtension, startClient, server, useShortConnectionTimeout } ) => {
219
+ useShortConnectionTimeout ( 500 ) ;
193
220
194
- const testWithOldVersion = test . extend ( {
195
- pathToExtension : async ( { } , use , testInfo ) => {
196
- const extensionDir = testInfo . outputPath ( 'extension' ) ;
197
- const oldPath = fileURLToPath ( new URL ( '../dist' , import . meta. url ) ) ;
221
+ // Prelaunch the browser, so that it is properly closed after the test.
222
+ const browserContext = await browserWithExtension . launch ( ) ;
198
223
199
- await fs . promises . cp ( oldPath , extensionDir , { recursive : true } ) ;
200
- const manifestPath = path . join ( extensionDir , 'manifest.json' ) ;
201
- const manifest = JSON . parse ( await fs . promises . readFile ( manifestPath , 'utf8' ) ) ;
202
- manifest . version = '0.0.1' ;
203
- await fs . promises . writeFile ( manifestPath , JSON . stringify ( manifest , null , 2 ) + '\n' ) ;
224
+ const client = await startClientMethod ( browserWithExtension , startClient ) ;
204
225
205
- await use ( extensionDir ) ;
206
- } ,
207
- } ) ;
226
+ const confirmationPagePromise = browserContext . waitForEvent ( 'page' , page => {
227
+ return page . url ( ) . startsWith ( 'chrome-extension://jakfalbnbhgkpmoaakfflhflbfpkailf/connect.html' ) ;
228
+ } ) ;
208
229
209
- testWithOldVersion ( `extension version mismatch` , async ( { browserWithExtension, startClient, server } ) => {
210
- // Prelaunch the browser, so that it is properly closed after the test.
211
- await browserWithExtension . launch ( ) ;
230
+ const navigateResponse = client . callTool ( {
231
+ name : 'browser_navigate' ,
232
+ arguments : { url : server . HELLO_WORLD } ,
233
+ } ) ;
212
234
213
- const client = await startWithExtensionFlag ( browserWithExtension , startClient ) ;
235
+ const confirmationPage = await confirmationPagePromise ;
236
+ await expect ( confirmationPage . locator ( '.status-banner' ) ) . toHaveText ( `Incompatible Playwright MCP version: ${ packageJSON . version } (extension version: 0.0.1). Please install the latest version of the extension.` ) ;
214
237
215
- const navigateResponse = client . callTool ( {
216
- name : 'browser_navigate' ,
217
- arguments : { url : server . HELLO_WORLD } ,
238
+ expect ( await navigateResponse ) . toHaveResponse ( {
239
+ result : expect . stringContaining ( 'Extension connection timeout.' ) ,
240
+ isError : true ,
241
+ } ) ;
218
242
} ) ;
219
243
220
- expect ( await navigateResponse ) . toHaveResponse ( {
221
- result : expect . stringContaining ( 'Extension version mismatch: expected ' + packageJSON . version + ', got 0.0.1. Make sure the extension is up to date.' ) ,
222
- isError : true ,
223
- } ) ;
224
- } ) ;
244
+ }
0 commit comments