@@ -7,6 +7,7 @@ import { ExistingBrowser, getActivePuppeteerPage } from "../../existing-browser"
77import * as logger from "../../../utils/logger" ;
88import { Cookie } from "../../../types" ;
99import type { Browser } from "../../types" ;
10+ import { MasterEvents } from "../../../events" ;
1011
1112export type SaveStateOptions = {
1213 path ?: string ;
@@ -16,6 +17,7 @@ export type SaveStateOptions = {
1617 sessionStorage ?: boolean ;
1718
1819 cookieFilter ?: ( cookie : Cookie ) => boolean ;
20+ keepFile ?: boolean ;
1921} ;
2022
2123export type FrameData = StorageData ;
@@ -29,6 +31,7 @@ export const defaultOptions = {
2931 cookies : true ,
3032 localStorage : true ,
3133 sessionStorage : true ,
34+ keepFile : false ,
3235} ;
3336
3437// in case when we use webdriver protocol, bidi and isolation
@@ -178,8 +181,21 @@ export default (browser: ExistingBrowser): void => {
178181 data . cookies = data . cookies . filter ( options . cookieFilter ) ;
179182 }
180183
181- if ( options && options . path ) {
182- await fs . writeJson ( options . path , data , { spaces : 2 } ) ;
184+ const dataIsEmpty = data . cookies ?. length === 0 && _ . isEmpty ( data . framesData ) ;
185+
186+ if ( options && options . path && ! dataIsEmpty ) {
187+ await fs . outputJson ( options . path , data , { spaces : 2 } ) ;
188+
189+ if ( options . keepFile ) {
190+ logger . warn (
191+ "\x1b[31mPlease be aware that the file containing authorization data will not be automatically deleted after the tests are completed!!!\x1b[0m" ,
192+ ) ;
193+ } else if ( process . send ) {
194+ process . send ( {
195+ event : MasterEvents . ADD_FILE_TO_REMOVE ,
196+ data : options . path ,
197+ } ) ;
198+ }
183199 }
184200
185201 return data ;
0 commit comments