1
+ import os
2
+ import shutil
1
3
from PuppeteerLibrary .base .librarycomponent import LibraryComponent
2
4
from PuppeteerLibrary .base .robotlibcore import keyword
3
5
from PuppeteerLibrary .ikeywords .ibrowsermanagement_async import iBrowserManagementAsync
@@ -8,6 +10,7 @@ class BrowserManagementKeywords(LibraryComponent):
8
10
9
11
def __init__ (self , ctx ):
10
12
super ().__init__ (ctx )
13
+ self .STATES_FOLDER = './states'
11
14
12
15
def get_async_keyword_group (self ) -> iBrowserManagementAsync :
13
16
return self .ctx .get_current_library_context ().get_async_keyword_group (type (self ).__name__ )
@@ -32,6 +35,7 @@ def open_browser(self, url, browser="chrome", alias=None, options={}):
32
35
| width | default 1366 |
33
36
| height | default 768 |
34
37
| emulate | iPhone 11 |
38
+ | state_ref | State Reference |
35
39
36
40
**Other options**
37
41
pwchrome, webkit and firefox please visit: https://playwright.dev/python/docs/api/class-browser?_highlight=new_page#browsernew_pagekwargs
@@ -234,3 +238,47 @@ def delete_all_cookies(self):
234
238
""" Deletes all cookies.
235
239
"""
236
240
return self .loop .run_until_complete (self .get_async_keyword_group ().delete_all_cookies ())
241
+
242
+ ##############################
243
+ # State
244
+ ##############################
245
+ @keyword
246
+ def save_browser_storage_state (self , ref = 'user' ):
247
+ """ Save browser storage state that can resue Authentication state
248
+
249
+ *ref* : reference state name
250
+
251
+ *Limitation* only support Playwright browser
252
+ """
253
+ self .info ('Save storate state for ' + ref )
254
+ try :
255
+ os .mkdir (self .STATES_FOLDER )
256
+ except :
257
+ self .info ('states folder already exists.' )
258
+ return self .loop .run_until_complete (self .get_async_keyword_group ().save_browser_storage_state (self .STATES_FOLDER , ref ))
259
+
260
+ @keyword
261
+ def delete_browser_storage_state (self , ref ):
262
+ """ Delete browser storage state
263
+
264
+ *ref* : reference state name
265
+
266
+ *Limitation* only support Playwright browser
267
+ """
268
+ file_path = self .STATES_FOLDER + '/state-' + ref + '.json'
269
+ if os .path .exists (file_path ):
270
+ os .remove (file_path )
271
+ else :
272
+ self .warn ('Can not delete the storate ' + ref + ' as it doesn\' t exists' )
273
+
274
+ @keyword
275
+ def delete_all_browser_storage_states (self ):
276
+ """ Delete all browser storage state
277
+
278
+ *Limitation* only support Playwright browser
279
+ """
280
+ try :
281
+ shutil .rmtree (self .STATES_FOLDER )
282
+ except OSError as e :
283
+ self .warn ("Error: %s - %s." % (e .filename , e .strerror ))
284
+
0 commit comments