@@ -7,7 +7,16 @@ import colors from 'css-color-names';
7
7
import { ElementHandle } from 'playwright-core' ;
8
8
import fetch from 'node-fetch' ;
9
9
10
- import { isBuild , isWin , isCI , page , testDir , viteTestUrl , browserLogs } from './vitestSetup' ;
10
+ import {
11
+ isBuild ,
12
+ isWin ,
13
+ isCI ,
14
+ page ,
15
+ testDir ,
16
+ browserLogs ,
17
+ e2eServer ,
18
+ waitForViteConnect
19
+ } from './vitestSetup' ;
11
20
12
21
export * from './vitestSetup' ;
13
22
@@ -42,20 +51,23 @@ const timeout = (n: number) => new Promise((r) => setTimeout(r, n));
42
51
43
52
async function toEl ( el : string | ElementHandle ) : Promise < ElementHandle > {
44
53
if ( typeof el === 'string' ) {
45
- return await page . $ ( el ) ;
54
+ return await page . $ ( el , { strict : true } ) ;
46
55
}
47
56
return el ;
48
57
}
49
58
50
59
export async function getColor ( el : string | ElementHandle ) {
51
60
el = await toEl ( el ) ;
61
+ if ( el == null ) {
62
+ return null ;
63
+ }
52
64
const rgb = await el . evaluate ( ( el ) => getComputedStyle ( el as Element ) . color ) ;
53
65
return hexToNameMap [ rgbToHex ( rgb ) ] || rgb ;
54
66
}
55
67
56
68
export async function getBg ( el : string | ElementHandle ) {
57
69
el = await toEl ( el ) ;
58
- return el . evaluate ( ( el ) => getComputedStyle ( el as Element ) . backgroundImage ) ;
70
+ return el == null ? null : el . evaluate ( ( el ) => getComputedStyle ( el as Element ) . backgroundImage ) ;
59
71
}
60
72
61
73
export function readFileContent ( filename : string ) {
@@ -124,7 +136,7 @@ export async function getEl(selector: string) {
124
136
125
137
export async function getText ( el : string | ElementHandle ) {
126
138
el = await toEl ( el ) ;
127
- return el ? await el . evaluate ( ( el ) => el . textContent ) : null ;
139
+ return el ? el . textContent ( ) : null ;
128
140
}
129
141
130
142
export async function hmrUpdateComplete ( file , timeout ) {
@@ -204,9 +216,33 @@ export async function saveScreenshot(name: string) {
204
216
205
217
export async function editViteConfig ( replacer : ( str : string ) => string ) {
206
218
editFile ( 'vite.config.js' , replacer ) ;
207
- await sleep ( isWin ? 1000 : 500 ) ; // editing vite config restarts server, give it some time
208
- await page . goto ( viteTestUrl , { waitUntil : 'networkidle' } ) ;
209
- await sleep ( 50 ) ;
219
+ if ( ! isBuild ) {
220
+ await waitForServerRestartAndReloadPage ( ) ;
221
+ }
222
+ }
223
+
224
+ export async function waitForServerRestartAndReloadPage ( timeout = 10000 ) {
225
+ const logs = e2eServer . logs . server . out ;
226
+ const startIdx = logs . length ;
227
+ let timeleft = timeout ;
228
+ const pollInterval = 50 ;
229
+ let restarted = false ;
230
+ while ( timeleft > 0 ) {
231
+ await sleep ( pollInterval ) ;
232
+ if ( logs . some ( ( text , i ) => i > startIdx && text . endsWith ( 'server restarted.' ) ) ) {
233
+ restarted = true ;
234
+ break ;
235
+ }
236
+ timeleft -= pollInterval ;
237
+ }
238
+ if ( ! restarted ) {
239
+ throw new Error ( `server did not restart after ${ timeout } ms` ) ;
240
+ }
241
+ await reloadPage ( ) ;
242
+ }
243
+
244
+ export async function reloadPage ( ) {
245
+ await Promise . all ( [ page . reload ( ) , waitForViteConnect ( page ) ] ) ;
210
246
}
211
247
212
248
export async function waitForNavigation ( opts : Parameters < typeof page . waitForNavigation > [ 0 ] ) {
0 commit comments