@@ -31,7 +31,7 @@ import type { ClientInstrumentationListener } from '../../playwright-core/src/cl
3131import type { Playwright as PlaywrightImpl } from '../../playwright-core/src/client/playwright' ;
3232import type { Browser as BrowserImpl } from '../../playwright-core/src/client/browser' ;
3333import type { BrowserContext as BrowserContextImpl } from '../../playwright-core/src/client/browserContext' ;
34- import type { APIRequestContext as APIRequestContextImpl } from '../../playwright-core/src/client/fetch' ;
34+ import type { APIRequestContext as APIRequestContextImpl , NewContextOptions as APIRequestContextOptions } from '../../playwright-core/src/client/fetch' ;
3535import type { ChannelOwner } from '../../playwright-core/src/client/channelOwner' ;
3636import type { Page as PageImpl } from '../../playwright-core/src/client/page' ;
3737import type { BrowserContext , BrowserContextOptions , LaunchOptions , Page , Tracing } from 'playwright-core' ;
@@ -228,42 +228,33 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
228228 if ( serviceWorkers !== undefined )
229229 options . serviceWorkers = serviceWorkers ;
230230
231- const workerFile = await agentCacheWorkerFile ( agent , testInfo as TestInfoImpl ) ;
232- if ( agent && workerFile )
233- options . agent = { ...agent , cacheFile : workerFile } ;
234-
235231 await use ( {
236232 ...contextOptions ,
237233 ...options ,
238234 } ) ;
239-
240- if ( testInfo . status === 'passed' && workerFile )
241- await ( testInfo as TestInfoImpl ) . _upstreamStorage ( workerFile ) ;
242235 } , { box : true } ] ,
243236
244- _setupContextOptions : [ async ( { playwright, _combinedContextOptions , actionTimeout, navigationTimeout, testIdAttribute } , use , testInfo ) => {
237+ _setupContextOptions : [ async ( { playwright, actionTimeout, navigationTimeout, testIdAttribute } , use , testInfo ) => {
245238 if ( testIdAttribute )
246239 playwrightLibrary . selectors . setTestIdAttribute ( testIdAttribute ) ;
247240 testInfo . snapshotSuffix = process . platform ;
248241 if ( debugMode ( ) === 'inspector' )
249242 ( testInfo as TestInfoImpl ) . _setDebugMode ( ) ;
250243
251- playwright . _defaultContextOptions = _combinedContextOptions ;
252244 playwright . _defaultContextTimeout = actionTimeout || 0 ;
253245 playwright . _defaultContextNavigationTimeout = navigationTimeout || 0 ;
254246 await use ( ) ;
255- playwright . _defaultContextOptions = undefined ;
256247 playwright . _defaultContextTimeout = undefined ;
257248 playwright . _defaultContextNavigationTimeout = undefined ;
258249 } , { auto : 'all-hooks-included' , title : 'context configuration' , box : true } as any ] ,
259250
260- _setupArtifacts : [ async ( { playwright, screenshot } , use , testInfo ) => {
251+ _setupArtifacts : [ async ( { playwright, screenshot, _combinedContextOptions , agent } , use , testInfo ) => {
261252 // This fixture has a separate zero-timeout slot to ensure that artifact collection
262253 // happens even after some fixtures or hooks time out.
263254 // Now that default test timeout is known, we can replace zero with an actual value.
264255 testInfo . setTimeout ( testInfo . project . timeout ) ;
265256
266- const artifactsRecorder = new ArtifactsRecorder ( playwright , tracing ( ) . artifactsDir ( ) , screenshot ) ;
257+ const artifactsRecorder = new ArtifactsRecorder ( playwright , tracing ( ) . artifactsDir ( ) , screenshot , agent ) ;
267258 await artifactsRecorder . willStartTest ( testInfo as TestInfoImpl ) ;
268259
269260 const tracingGroupSteps : TestStepInternal [ ] = [ ] ;
@@ -317,20 +308,33 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
317308 if ( ! keepTestTimeout )
318309 currentTestInfo ( ) ?. _setDebugMode ( ) ;
319310 } ,
311+ runBeforeCreateBrowserContext : async ( options : BrowserContextOptions ) => {
312+ for ( const [ key , value ] of Object . entries ( _combinedContextOptions ) ) {
313+ if ( ! ( key in options ) )
314+ options [ key as keyof BrowserContextOptions ] = value ;
315+ }
316+ await artifactsRecorder . willCreateBrowserContext ( options ) ;
317+ } ,
318+ runBeforeCreateRequestContext : async ( options : APIRequestContextOptions ) => {
319+ for ( const [ key , value ] of Object . entries ( _combinedContextOptions ) ) {
320+ if ( ! ( key in options ) )
321+ options [ key as keyof APIRequestContextOptions ] = value ;
322+ }
323+ } ,
320324 runAfterCreateBrowserContext : async ( context : BrowserContextImpl ) => {
321- await artifactsRecorder ? .didCreateBrowserContext ( context ) ;
325+ await artifactsRecorder . didCreateBrowserContext ( context ) ;
322326 const testInfo = currentTestInfo ( ) ;
323327 if ( testInfo )
324328 attachConnectedHeaderIfNeeded ( testInfo , context . browser ( ) ) ;
325329 } ,
326330 runAfterCreateRequestContext : async ( context : APIRequestContextImpl ) => {
327- await artifactsRecorder ? .didCreateRequestContext ( context ) ;
331+ await artifactsRecorder . didCreateRequestContext ( context ) ;
328332 } ,
329333 runBeforeCloseBrowserContext : async ( context : BrowserContextImpl ) => {
330- await artifactsRecorder ? .willCloseBrowserContext ( context ) ;
334+ await artifactsRecorder . willCloseBrowserContext ( context ) ;
331335 } ,
332336 runBeforeCloseRequestContext : async ( context : APIRequestContextImpl ) => {
333- await artifactsRecorder ? .willCloseRequestContext ( context ) ;
337+ await artifactsRecorder . willCloseRequestContext ( context ) ;
334338 } ,
335339 } ;
336340
@@ -643,10 +647,12 @@ class ArtifactsRecorder {
643647
644648 private _screenshotRecorder : SnapshotRecorder ;
645649 private _pageSnapshot : string | undefined ;
650+ private _agent : PlaywrightTestOptions [ 'agent' ] ;
646651
647- constructor ( playwright : PlaywrightImpl , artifactsDir : string , screenshot : ScreenshotOption ) {
652+ constructor ( playwright : PlaywrightImpl , artifactsDir : string , screenshot : ScreenshotOption , agent : PlaywrightTestOptions [ 'agent' ] ) {
648653 this . _playwright = playwright ;
649654 this . _artifactsDir = artifactsDir ;
655+ this . _agent = agent ;
650656 const screenshotOptions = typeof screenshot === 'string' ? undefined : screenshot ;
651657 this . _startedCollectingArtifacts = Symbol ( 'startedCollectingArtifacts' ) ;
652658
@@ -673,10 +679,15 @@ class ArtifactsRecorder {
673679 await this . _startTraceChunkOnContextCreation ( context , context . tracing ) ;
674680 }
675681
682+ async willCreateBrowserContext ( options : BrowserContextOptions ) {
683+ await this . _cloneAgentCache ( options ) ;
684+ }
685+
676686 async willCloseBrowserContext ( context : BrowserContextImpl ) {
677687 await this . _stopTracing ( context , context . tracing ) ;
678688 await this . _screenshotRecorder . captureTemporary ( context ) ;
679689 await this . _takePageSnapshot ( context ) ;
690+ await this . _upstreamAgentCache ( context ) ;
680691 }
681692
682693 private async _takePageSnapshot ( context : BrowserContextImpl ) {
@@ -698,6 +709,24 @@ class ArtifactsRecorder {
698709 } catch { }
699710 }
700711
712+ private async _cloneAgentCache ( options : BrowserContextOptions ) {
713+ if ( ! this . _agent || this . _agent . cacheMode === 'ignore' )
714+ return ;
715+ if ( ! this . _agent . cacheFile && ! this . _agent . cachePathTemplate )
716+ return ;
717+
718+ const cacheFile = this . _agent . cacheFile ?? this . _testInfo . _applyPathTemplate ( this . _agent . cachePathTemplate ! , 'cache' , '.json' ) ;
719+ const workerFile = await this . _testInfo . _cloneStorage ( cacheFile ) ;
720+ if ( this . _agent && workerFile )
721+ options . agent = { ...this . _agent , cacheFile : workerFile } ;
722+ }
723+
724+ private async _upstreamAgentCache ( context : BrowserContextImpl ) {
725+ const agent = context . _options . agent ;
726+ if ( this . _testInfo . status === 'passed' && agent ?. cacheFile )
727+ await this . _testInfo . _upstreamStorage ( agent . cacheFile ) ;
728+ }
729+
701730 async didCreateRequestContext ( context : APIRequestContextImpl ) {
702731 await this . _startTraceChunkOnContextCreation ( context , context . _tracing ) ;
703732 }
@@ -792,16 +821,6 @@ function tracing() {
792821 return ( test . info ( ) as TestInfoImpl ) . _tracing ;
793822}
794823
795- async function agentCacheWorkerFile ( agent : PlaywrightTestOptions [ 'agent' ] , testInfo : TestInfoImpl ) : Promise < string | undefined > {
796- if ( ! agent || agent . cacheMode === 'ignore' )
797- return undefined ;
798- if ( ! agent . cacheFile && ! agent . cachePathTemplate )
799- return undefined ;
800-
801- const cacheFile = agent . cacheFile ?? testInfo . _applyPathTemplate ( agent . cachePathTemplate ! , 'cache' , '.json' ) ;
802- return await testInfo . _cloneStorage ( cacheFile ) ;
803- }
804-
805824export const test = _baseTest . extend < TestFixtures , WorkerFixtures > ( playwrightFixtures ) ;
806825
807826export { defineConfig } from './common/configLoader' ;
0 commit comments