1+ import { join } from 'node:path' ;
2+ import { writeFile } from 'node:fs/promises' ;
3+ import { chromium , FullConfig } from '@playwright/test' ;
4+ import { isProduction } from './utils' ;
5+
6+ export default async function globalSetup ( config : FullConfig ) {
7+ const storageStatePath = join ( __dirname , 'storage-state.json' ) ;
8+ await writeFile ( storageStatePath , '{}' , 'utf-8' ) ;
9+
10+ const project = config . projects [ 0 ] ;
11+ console . log ( `[Global Setup] Processing project ${ project . name } ` ) ;
12+ const baseURL = project . use ?. baseURL ;
13+
14+ if ( isProduction ( baseURL ) ) {
15+ await closeProductionElements ( baseURL , storageStatePath ) ;
16+ }
17+ }
18+
19+ async function closeProductionElements ( baseURL : string , storageStatePath : string ) {
20+ console . log ( `[Global Setup] Starting cookie banner setup for ${ baseURL } ` ) ;
21+
22+ const browser = await chromium . launch ( ) ;
23+ const context = await browser . newContext ( ) ;
24+ const page = await context . newPage ( ) ;
25+
26+ try {
27+ await page . goto ( baseURL , { waitUntil : 'domcontentloaded' } ) ;
28+
29+ try {
30+ const acceptButton = page . getByRole ( 'button' , { name : 'Accept All' } ) ;
31+ await acceptButton . waitFor ( { state : 'visible' , timeout : 5000 } ) ;
32+ await acceptButton . click ( ) ;
33+
34+ await page . waitForTimeout ( 1000 ) ;
35+ } catch ( error ) {
36+ console . log ( '[Global Setup] Cookie banner not found - continuing' ) ;
37+ }
38+
39+ const closeBanner = page . locator ( '#optly-banner_close' ) ;
40+
41+ if ( await closeBanner . count ( ) > 0 ) {
42+ console . log ( '[Global Setup] Closing "purple" banner' ) ;
43+ await closeBanner . click ( ) ;
44+ await page . waitForSelector ( '#optly-banner_close' , { state : 'hidden' } ) ;
45+ }
46+
47+ await context . storageState ( { path : storageStatePath } ) ;
48+ console . log ( `[Global Setup] Storage state saved to ${ storageStatePath } ` ) ;
49+ } catch ( error ) {
50+ console . error ( '[Global Setup] Error during setup:' , error ) ;
51+ throw error ;
52+ } finally {
53+ await context . close ( ) ;
54+ await browser . close ( ) ;
55+ }
56+ }
0 commit comments