1616
1717import path from 'path' ;
1818
19+ import { disposeAll } from '../../client/disposable' ;
1920import { eventsHelper } from '../../client/eventEmitter' ;
2021import { debug } from '../../utilsBundle' ;
2122import { escapeWithQuotes } from '../../utils/isomorphic/stringUtils' ;
@@ -29,7 +30,7 @@ import type * as playwright from '../../..';
2930import type { FullConfig } from './config' ;
3031import type { SessionLog } from './sessionLog' ;
3132import type { Tracing } from '../../client/tracing' ;
32- import type { Disposable } from '../../client/eventEmitter ' ;
33+ import type { Disposable } from '../../client/disposable ' ;
3334import type { BrowserContext } from '../../client/browserContext' ;
3435import type { ClientInfo } from '../sdk/server' ;
3536
@@ -88,14 +89,12 @@ export class Context {
8889 }
8990
9091 async dispose ( ) {
91- for ( const disposable of this . _disposables )
92- disposable . dispose ( ) ;
93- this . _disposables = [ ] ;
92+ disposeAll ( this . _disposables ) ;
9493 for ( const tab of this . _tabs )
95- tab . dispose ( ) ;
94+ await tab . dispose ( ) ;
9695 this . _tabs . length = 0 ;
9796 this . _currentTab = undefined ;
98- this . stopVideoRecording ( ) ;
97+ await this . stopVideoRecording ( ) ;
9998 }
10099
101100 tabs ( ) : Tab [ ] {
@@ -235,15 +234,17 @@ export class Context {
235234
236235 private async _setupRequestInterception ( context : playwright . BrowserContext ) {
237236 if ( this . config . network ?. allowedOrigins ?. length ) {
238- await context . route ( '**' , route => route . abort ( 'blockedbyclient' ) ) ;
237+ this . _disposables . push ( await context . route ( '**' , route => route . abort ( 'blockedbyclient' ) ) ) ;
239238
240- for ( const origin of this . config . network . allowedOrigins )
241- await context . route ( originOrHostGlob ( origin ) , route => route . continue ( ) ) ;
239+ for ( const origin of this . config . network . allowedOrigins ) {
240+ const glob = originOrHostGlob ( origin ) ;
241+ this . _disposables . push ( await context . route ( glob , route => route . continue ( ) ) ) ;
242+ }
242243 }
243244
244245 if ( this . config . network ?. blockedOrigins ?. length ) {
245246 for ( const origin of this . config . network . blockedOrigins )
246- await context . route ( originOrHostGlob ( origin ) , route => route . abort ( 'blockedbyclient' ) ) ;
247+ this . _disposables . push ( await context . route ( originOrHostGlob ( origin ) , route => route . abort ( 'blockedbyclient' ) ) ) ;
247248 }
248249 }
249250
@@ -263,9 +264,7 @@ export class Context {
263264 ( browserContext as any ) . _setAllowedDirectories ( allRootPaths ( this . _clientInfo ) ) ;
264265 }
265266 await this . _setupRequestInterception ( browserContext ) ;
266- for ( const page of browserContext . pages ( ) )
267- this . _onPageCreated ( page ) ;
268- this . _disposables . push ( eventsHelper . addEventListener ( browserContext as BrowserContext , 'page' , page => this . _onPageCreated ( page ) ) ) ;
267+
269268 if ( this . config . saveTrace ) {
270269 await ( browserContext . tracing as Tracing ) . start ( {
271270 name : 'trace-' + Date . now ( ) ,
@@ -281,7 +280,12 @@ export class Context {
281280 }
282281 const rootPath = firstRootPath ( this . _clientInfo ) ;
283282 for ( const initScript of this . config . browser . initScript || [ ] )
284- await browserContext . addInitScript ( { path : path . resolve ( rootPath , initScript ) } ) ;
283+ this . _disposables . push ( await browserContext . addInitScript ( { path : path . resolve ( rootPath , initScript ) } ) ) ;
284+
285+ for ( const page of browserContext . pages ( ) )
286+ this . _onPageCreated ( page ) ;
287+ this . _disposables . push ( eventsHelper . addEventListener ( browserContext as BrowserContext , 'page' , page => this . _onPageCreated ( page ) ) ) ;
288+
285289 return browserContext ;
286290 }
287291
0 commit comments