20
20
)
21
21
from .playwright import Playwright
22
22
from .utils import logger
23
+ from .utils .data_types import AutoClosingLevel
23
24
from .version import VERSION
24
25
25
26
@@ -141,15 +142,30 @@ class Browser(DynamicCore):
141
142
Builtin Evaluating expressions]
142
143
for more info on the syntax.
143
144
145
+ = Automatic page and context closing =
146
+
147
+ Library will close pages and contexts that are created during test execution.
148
+ Pages and contexts created before test in Suite Setup or Suite Teardown will be closed after that suite.
149
+ This will remove the burden of closing these resources in teardowns.
150
+
151
+ Browsers will not be automatically closed. A browser is expensive to create and should be reused.
152
+
153
+ Automatic closing can be configured or switched off with the ``auto_closing_level`` library parameter.
144
154
"""
145
155
146
156
ROBOT_LIBRARY_VERSION = VERSION
147
157
ROBOT_LISTENER_API_VERSION = 2
148
158
ROBOT_LIBRARY_LISTENER : "Browser"
149
159
ROBOT_LIBRARY_SCOPE = "GLOBAL"
150
160
SUPPORTED_BROWSERS = ["chromium" , "firefox" , "webkit" ]
151
-
152
- def __init__ (self , timeout = "10s" , enable_playwright_debug : bool = False ):
161
+ _auto_closing_level : AutoClosingLevel
162
+
163
+ def __init__ (
164
+ self ,
165
+ timeout = "10s" ,
166
+ enable_playwright_debug : bool = False ,
167
+ auto_closing_level : AutoClosingLevel = AutoClosingLevel .TEST ,
168
+ ):
153
169
"""Browser library can be taken into use with optional arguments:
154
170
155
171
- ``timeout``:
@@ -158,6 +174,10 @@ def __init__(self, timeout="10s", enable_playwright_debug: bool = False):
158
174
- ``enable_playwright_debug``:
159
175
Enable low level debug information from the playwright tool. Mainly
160
176
Useful for the library developers and for debugging purposes.
177
+ - ``auto_closing_level``:
178
+ Configure context and page automatic closing. Default is after each test.
179
+ Other options are SUITE for closing after each suite and MANUAL
180
+ for no automatic closing.
161
181
"""
162
182
self .ROBOT_LIBRARY_LISTENER = self
163
183
self ._execution_stack : List [object ] = []
@@ -180,6 +200,7 @@ def __init__(self, timeout="10s", enable_playwright_debug: bool = False):
180
200
WebAppState (self ),
181
201
]
182
202
self .playwright = Playwright (timeout , enable_playwright_debug )
203
+ self ._auto_closing_level = auto_closing_level
183
204
DynamicCore .__init__ (self , libraries )
184
205
185
206
@property
@@ -190,21 +211,25 @@ def _close(self):
190
211
self .playwright .close ()
191
212
192
213
def _start_suite (self , name , attrs ):
193
- self ._execution_stack .append (self .getters .get_browser_catalog ())
214
+ if self ._auto_closing_level != AutoClosingLevel .MANUAL :
215
+ self ._execution_stack .append (self .getters .get_browser_catalog ())
194
216
195
217
def _start_test (self , name , attrs ):
196
- self ._execution_stack .append (self .getters .get_browser_catalog ())
218
+ if self ._auto_closing_level == AutoClosingLevel .TEST :
219
+ self ._execution_stack .append (self .getters .get_browser_catalog ())
197
220
198
221
def _end_test (self , name , attrs ):
199
222
if len (self .promises ._unresolved_promises ) > 0 :
200
223
logger .warn (f"Waiting unresolved promises at the end of test '{ name } '" )
201
224
self .wait_for_all_promises ()
202
- catalog_before_test = self ._execution_stack .pop ()
203
- self ._prune_execution_stack (catalog_before_test )
225
+ if self ._auto_closing_level == AutoClosingLevel .TEST :
226
+ catalog_before_test = self ._execution_stack .pop ()
227
+ self ._prune_execution_stack (catalog_before_test )
204
228
205
229
def _end_suite (self , name , attrs ):
206
- catalog_before_suite = self ._execution_stack .pop ()
207
- self ._prune_execution_stack (catalog_before_suite )
230
+ if self ._auto_closing_level != AutoClosingLevel .MANUAL :
231
+ catalog_before_suite = self ._execution_stack .pop ()
232
+ self ._prune_execution_stack (catalog_before_suite )
208
233
209
234
def _prune_execution_stack (self , catalog_before ):
210
235
# WIP CODE BEGINS
@@ -233,6 +258,7 @@ def _prune_execution_stack(self, catalog_before):
233
258
self .playwright_state .switch_context (ctx_id )
234
259
self .playwright_state .switch_page (page_id )
235
260
self .playwright_state .close_page ()
261
+ # try to set active page and context back to right place.
236
262
for browser in catalog_after :
237
263
if browser ["activeBrowser" ]:
238
264
activeContext = browser .get ("activeContext" , None )
0 commit comments