@@ -32,12 +32,13 @@ class PlaywrightContext(iLibraryContext):
32
32
current_page : any = None
33
33
current_iframe = None
34
34
35
- page_support_options = ['accept_downloads' , 'bypass_csp' , 'color_scheme' , 'device_scale_factor' , 'extra_http_headers' , 'geolocation' , 'has_touch' , 'http_credentials' , 'ignore_https_errors' , 'is_mobile' , 'java_script_enabled' , 'locale' , 'no_viewport' , 'offline' , 'permissions' , 'proxy' , 'record_har_omit_content' , 'record_har_path' , 'record_video_dir' , 'record_video_size' , 'timezone_id' , 'user_agent' , 'viewport' ]
36
-
35
+ page_support_options = ['accept_downloads' , 'bypass_csp' , 'color_scheme' , 'device_scale_factor' , 'extra_http_headers' , 'geolocation' , 'has_touch' , 'http_credentials' , 'ignore_https_errors' , 'is_mobile' ,
36
+ 'java_script_enabled' , 'locale' , 'no_viewport' , 'offline' , 'permissions' , 'proxy' , 'record_har_omit_content' , 'record_har_path' , 'record_video_dir' , 'record_video_size' , 'timezone_id' , 'user_agent' , 'viewport' ]
37
+
37
38
def __init__ (self , browser_type : str ):
38
39
super ().__init__ (browser_type )
39
40
40
- async def start_server (self , options : dict = {}):
41
+ async def start_server (self , options : dict = {}):
41
42
default_options = {
42
43
'slowMo' : 0 ,
43
44
'headless' : True ,
@@ -54,9 +55,11 @@ async def start_server(self, options: dict={}):
54
55
if key in ['headless' , 'devtools' , 'accept_downloads' , 'is_mobile' ]:
55
56
merged_options [key ] = str2bool (merged_options [key ])
56
57
elif key == 'width' :
57
- merged_options ['viewport' ]['width' ] = str2int (merged_options [key ])
58
+ merged_options ['viewport' ]['width' ] = str2int (
59
+ merged_options [key ])
58
60
elif key == 'height' :
59
- merged_options ['viewport' ]['height' ] = str2int (merged_options [key ])
61
+ merged_options ['viewport' ]['height' ] = str2int (
62
+ merged_options [key ])
60
63
elif key in ['slowMo' ]:
61
64
merged_options [key ] = str2int (merged_options [key ])
62
65
@@ -65,8 +68,14 @@ async def start_server(self, options: dict={}):
65
68
self .playwright = await async_playwright ().start ()
66
69
67
70
if self .browser_type == "chrome" or self .browser_type == "pwchrome" :
71
+ proxy = None
72
+ if 'proxy' in merged_options :
73
+ proxy = merged_options ['proxy' ]
74
+ # Chromium can use proxy only via global launch
75
+ del merged_options ['proxy' ]
68
76
self .browser = await self .playwright .chromium .launch (
69
- headless = merged_options ['headless' ])
77
+ headless = merged_options ['headless' ], proxy = proxy )
78
+ merged_options
70
79
elif self .browser_type == "webkit" :
71
80
self .browser = await self .playwright .webkit .launch (
72
81
headless = merged_options ['headless' ])
@@ -78,7 +87,7 @@ async def start_server(self, options: dict={}):
78
87
async def stop_server (self ):
79
88
await self .playwright .stop ()
80
89
self ._reset_server_context ()
81
-
90
+
82
91
def is_server_started (self ) -> bool :
83
92
if self .browser is not None :
84
93
return True
@@ -88,7 +97,7 @@ def set_default_timeout(self, timeout):
88
97
self .timeout = timeout
89
98
self .get_current_page ().get_page ().set_default_timeout (timeout * 1000 )
90
99
91
- async def create_new_page (self , options : dict = {}) -> BasePage :
100
+ async def create_new_page (self , options : dict = {}) -> BasePage :
92
101
device_options = {
93
102
'accept_downloads' : True ,
94
103
'viewport' : {
@@ -101,25 +110,29 @@ async def create_new_page(self, options: dict={}) -> BasePage:
101
110
if support_key in options :
102
111
device_options [support_key ] = options [support_key ]
103
112
if support_key in ['accept_downloads' , 'ignore_https_errors' ]:
104
- device_options [support_key ] = str2bool (device_options [support_key ])
113
+ device_options [support_key ] = str2bool (
114
+ device_options [support_key ])
105
115
106
116
# Force support viewport
107
117
if support_key == 'viewport' :
108
118
if 'width' in options .keys ():
109
- device_options ['viewport' ]['width' ] = str2int (options ['width' ])
119
+ device_options ['viewport' ]['width' ] = str2int (
120
+ options ['width' ])
110
121
if 'height' in device_options .keys ():
111
- device_options ['viewport' ]['height' ] = str2int (options ['height' ])
122
+ device_options ['viewport' ]['height' ] = str2int (
123
+ options ['height' ])
112
124
113
125
if 'emulate' in options :
114
126
device_options = self .playwright .devices [options ['emulate' ]]
115
127
116
128
if 'state_ref' in options :
117
- device_options ['storage_state' ] = './states/state-' + options ['state_ref' ] + '.json'
129
+ device_options ['storage_state' ] = './states/state-' + \
130
+ options ['state_ref' ] + '.json'
118
131
119
132
new_page = await self .browser .new_page (** device_options )
120
133
self .current_page = PlaywrightPage (new_page )
121
134
return self .current_page
122
-
135
+
123
136
def get_current_page (self ) -> BasePage :
124
137
return self .current_page
125
138
@@ -170,14 +183,13 @@ async def start_tracing(self):
170
183
async def stop_tracing (self , path ):
171
184
if len (self .browser .contexts ) > 0 :
172
185
await self .browser .contexts [0 ].tracing .stop (path = path )
173
-
186
+
174
187
def _reset_context (self ):
175
188
self .browser = None
176
189
self .current_context = None
177
190
self .current_page = None
178
191
self .current_iframe = None
179
-
192
+
180
193
def _reset_server_context (self ):
181
194
self ._reset_context ()
182
195
self .playwright = None
183
-
0 commit comments