@@ -41,7 +41,7 @@ import { urlMatchesEqual } from '../utils/isomorphic/urlMatch';
4141import { isRegExp , isString } from '../utils/isomorphic/rtti' ;
4242import { rewriteErrorMessage } from '../utils/isomorphic/stackTrace' ;
4343
44- import type { BrowserContextOptions , Headers , StorageState , WaitForEventOptions } from './types' ;
44+ import type { BrowserContextOptions , Headers , SetStorageState , StorageState , WaitForEventOptions } from './types' ;
4545import type * as structs from '../../types/structs' ;
4646import type * as api from '../../types/types' ;
4747import type { URLMatch } from '../utils/isomorphic/urlMatch' ;
@@ -455,6 +455,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
455455 return state ;
456456 }
457457
458+ async setStorageState ( storageState : string | SetStorageState ) {
459+ await this . _channel . setStorageState ( { storageState : await prepareStorageState ( this . _platform , storageState ) } ) ;
460+ }
461+
458462 backgroundPages ( ) : Page [ ] {
459463 return [ ...this . _backgroundPages ] ;
460464 }
@@ -527,13 +531,13 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
527531 }
528532}
529533
530- async function prepareStorageState ( platform : Platform , options : BrowserContextOptions ) : Promise < channels . BrowserNewContextParams [ 'storageState' ] > {
531- if ( typeof options . storageState !== 'string' )
532- return options . storageState as any ;
534+ async function prepareStorageState ( platform : Platform , storageState : string | SetStorageState ) : Promise < NonNullable < channels . BrowserNewContextParams [ 'storageState' ] > > {
535+ if ( typeof storageState !== 'string' )
536+ return storageState as any ;
533537 try {
534- return JSON . parse ( await platform . fs ( ) . promises . readFile ( options . storageState , 'utf8' ) ) ;
538+ return JSON . parse ( await platform . fs ( ) . promises . readFile ( storageState , 'utf8' ) ) ;
535539 } catch ( e ) {
536- rewriteErrorMessage ( e , `Error reading storage state from ${ options . storageState } :\n` + e . message ) ;
540+ rewriteErrorMessage ( e , `Error reading storage state from ${ storageState } :\n` + e . message ) ;
537541 throw e ;
538542 }
539543}
@@ -548,7 +552,7 @@ export async function prepareBrowserContextParams(platform: Platform, options: B
548552 viewport : options . viewport === null ? undefined : options . viewport ,
549553 noDefaultViewport : options . viewport === null ,
550554 extraHTTPHeaders : options . extraHTTPHeaders ? headersObjectToArray ( options . extraHTTPHeaders ) : undefined ,
551- storageState : await prepareStorageState ( platform , options ) ,
555+ storageState : options . storageState ? await prepareStorageState ( platform , options . storageState ) : undefined ,
552556 serviceWorkers : options . serviceWorkers ,
553557 colorScheme : options . colorScheme === null ? 'no-override' : options . colorScheme ,
554558 reducedMotion : options . reducedMotion === null ? 'no-override' : options . reducedMotion ,
0 commit comments