11import { test , expect , type Page } from "@playwright/test" ;
22
3+ // Type declarations for the test API exposed by main.ts
4+ declare global {
5+ interface Window {
6+ thumbdriveTest : {
7+ start : ( ) => Promise < void > ;
8+ shutdown : ( ) => Promise < void > ;
9+ isLeader : ( ) => boolean ;
10+ writeFile : ( p : string , s : string ) => Promise < void > ;
11+ readFile : ( p : string ) => Promise < string > ;
12+ exists : ( p : string ) => Promise < boolean > ;
13+ } ;
14+ }
15+ }
16+
317function url ( ns : string , arena : string ) {
418 return `/testapp/index.html?ns=${ encodeURIComponent ( ns ) } &arena=${ encodeURIComponent ( arena ) } ` ;
519}
@@ -17,14 +31,34 @@ async function setupPage(page: Page) {
1731}
1832
1933async function waitForReady ( page : Page ) {
34+ // Wait for the page to be fully loaded and stable
35+ await page . waitForLoadState ( 'domcontentloaded' ) ;
36+
2037 // Wait for the page to load and thumbdriveTest to be available
2138 await page . waitForFunction ( ( ) => window . thumbdriveTest !== undefined , { timeout : 10000 } ) ;
2239}
2340
2441async function startCandidate ( page : Page ) {
25- await page . evaluate ( ( ) => window . thumbdriveTest . start ( ) ) ;
26- // Give the worker time to initialize
27- await page . waitForTimeout ( 100 ) ;
42+ // Retry logic to handle transient execution context issues
43+ let lastError : Error | undefined ;
44+ for ( let attempt = 0 ; attempt < 3 ; attempt ++ ) {
45+ try {
46+ await page . evaluate ( ( ) => window . thumbdriveTest . start ( ) ) ;
47+ // Give the worker time to initialize
48+ await page . waitForTimeout ( 100 ) ;
49+ return ; // Success
50+ } catch ( error ) {
51+ lastError = error as Error ;
52+ if ( error instanceof Error && error . message . includes ( 'Execution context was destroyed' ) ) {
53+ // Wait and retry
54+ await page . waitForTimeout ( 100 ) ;
55+ continue ;
56+ }
57+ // Re-throw if it's not an execution context error
58+ throw error ;
59+ }
60+ }
61+ throw lastError ;
2862}
2963
3064async function shutdownLeader ( page : Page ) {
0 commit comments