31
31
import json
32
32
from .desktop_browser import DesktopBrowser
33
33
34
+ def _get_location_uri (accuracy , lat , lng ) -> str :
35
+ return f'data:application/json, {{ "status":"OK", "accuracy":{ accuracy } , "location":{{ "lat":{ lat } , "lng":{ lng } }} }}'
34
36
35
37
class Firefox (DesktopBrowser ):
36
38
"""Firefox"""
@@ -140,26 +142,47 @@ def start_firefox(self, job, task):
140
142
return
141
143
from selenium import webdriver # pylint: disable=import-error
142
144
143
- capabilities = webdriver .DesiredCapabilities .FIREFOX .copy ()
144
- if 'ignoreSSL' in job and job ['ignoreSSL' ]:
145
- capabilities ['acceptInsecureCerts' ] = True
146
- else :
147
- capabilities ['acceptInsecureCerts' ] = False
145
+ if webdriver .__version__ >= "4.12" :
146
+ service_args = ["--marionette-port" , "2828" ]
147
+ service = webdriver .FirefoxService (service_args = service_args , log_output = os .environ ["MOZ_LOG_FILE" ])
148
148
149
- capabilities ['moz:firefoxOptions' ] = {
150
- 'binary' : self .path ,
151
- 'args' : ['-profile' , task ['profile' ]],
152
- 'prefs' : self .prepare_prefs (),
153
- "log" : {"level" : "error" },
154
- 'env' : {
155
- "MOZ_LOG_FILE" : os .environ ["MOZ_LOG_FILE" ],
156
- "MOZ_LOG" : os .environ ["MOZ_LOG" ]
157
- }
158
- }
159
- service_args = ["--marionette-port" , "2828" ]
149
+ options = webdriver .FirefoxOptions ()
150
+ options .binary_location = self .path
151
+ options .add_argument ('--profile' )
152
+ options .add_argument (f'{ task ["profile" ]} ' )
153
+ options .log .level = 'error'
154
+ options .prefs = self .prepare_prefs ()
160
155
161
- self .driver = webdriver .Firefox (desired_capabilities = capabilities , service_args = service_args )
162
- logging .debug (self .driver .capabilities )
156
+ capabilities = webdriver .DesiredCapabilities .FIREFOX .copy ()
157
+ if 'ignoreSSL' in job and job ['ignoreSSL' ]:
158
+ capabilities ['acceptInsecureCerts' ] = True
159
+ else :
160
+ capabilities ['acceptInsecureCerts' ] = False
161
+
162
+ for key , value in capabilities .items ():
163
+ options .set_capability (key , value )
164
+ self .driver = webdriver .Firefox (options = options , service = service )
165
+ elif webdriver .__version__ <= "4.9" :
166
+ capabilities = webdriver .DesiredCapabilities .FIREFOX .copy ()
167
+ if 'ignoreSSL' in job and job ['ignoreSSL' ]:
168
+ capabilities ['acceptInsecureCerts' ] = True
169
+ else :
170
+ capabilities ['acceptInsecureCerts' ] = False
171
+
172
+ capabilities ['moz:firefoxOptions' ] = {
173
+ 'binary' : self .path ,
174
+ 'args' : ['-profile' , task ['profile' ]],
175
+ 'prefs' : self .prepare_prefs (),
176
+ "log" : {"level" : "error" },
177
+ 'env' : {
178
+ "MOZ_LOG_FILE" : os .environ ["MOZ_LOG_FILE" ],
179
+ "MOZ_LOG" : os .environ ["MOZ_LOG" ]
180
+ }
181
+ }
182
+ service_args = ["--marionette-port" , "2828" ]
183
+ self .driver = webdriver .Firefox (desired_capabilities = capabilities , service_args = service_args )
184
+ else :
185
+ raise Exception ("Unsupported selenium version %s" , webdriver .__version__ )
163
186
164
187
self .driver .set_page_load_timeout (task ['time_limit' ])
165
188
if 'browserVersion' in self .driver .capabilities :
@@ -208,17 +231,13 @@ def launch(self, job, task):
208
231
ua_string += ' ' + task ['AppendUA' ]
209
232
modified = True
210
233
if modified :
211
- logging .debug (ua_string )
212
234
self .driver_set_pref ('general.useragent.override' , ua_string )
213
235
# Location
214
236
if 'lat' in self .job and 'lng' in self .job :
215
237
try :
216
238
lat = float (str (self .job ['lat' ]))
217
239
lng = float (str (self .job ['lng' ]))
218
- location_uri = 'data:application/json,{{' \
219
- '"status":"OK","accuracy":10.0,' \
220
- '"location":{{"lat":{0:f},"lng":{1:f}}}' \
221
- '}}' .format (lat , lng )
240
+ location_uri = _get_location_uri (10 , lat , lng )
222
241
logging .debug ('Setting location: %s' , location_uri )
223
242
self .driver_set_pref ('geo.wifi.uri' , location_uri )
224
243
except Exception :
@@ -261,20 +280,12 @@ def driver_set_pref(self, key, value):
261
280
"""Set a Firefox pref at runtime"""
262
281
if self .driver is not None :
263
282
try :
264
- script = 'const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");'
265
- script += 'Services.prefs.'
266
- if isinstance (value , bool ):
267
- script += 'setBoolPref'
268
- elif isinstance (value , (str , unicode )):
269
- script += 'setStringPref'
270
- else :
271
- script += 'setIntPref'
272
- script += '({0}, {1});' .format (json .dumps (key ), json .dumps (value ))
273
- logging .debug (script )
283
+ script = 'const { Preferences } = ChromeUtils.importESModule("resource://gre/modules/Preferences.sys.mjs");'
284
+ script += f'Preferences.set({ json .dumps (key )} , { json .dumps (value )} );'
274
285
self .driver .set_context (self .driver .CONTEXT_CHROME )
275
286
self .driver .execute_script (script )
276
- except Exception :
277
- logging .exception ("Error setting pref" )
287
+ except Exception as err :
288
+ logging .exception ("Error setting pref %s => %s: %s" , key , value , err )
278
289
finally :
279
290
self .driver .set_context (self .driver .CONTEXT_CONTENT )
280
291
@@ -300,6 +311,8 @@ def close_browser(self, job, task):
300
311
if platform .system () == "Linux" :
301
312
subprocess .call (['killall' , '-9' , 'firefox' ])
302
313
subprocess .call (['killall' , '-9' , 'firefox-trunk' ])
314
+ subprocess .call (['killall' , '-9' , 'firefox-nightly' ])
315
+ subprocess .call (['killall' , '-9' , 'firefox-esr' ])
303
316
os .environ ["MOZ_LOG_FILE" ] = ''
304
317
os .environ ["MOZ_LOG" ] = ''
305
318
@@ -334,7 +347,7 @@ def run_axe(self, task):
334
347
script += "'" + "', '" .join (axe_cats ) + "'"
335
348
script += ']}).then(results=>{return results;});'
336
349
except Exception as err :
337
- logging .exception ("Exception running Axe: %s" , err . __str__ () )
350
+ logging .exception ("Exception running Axe: %s" , err )
338
351
if self .must_exit_now :
339
352
return
340
353
completed = False
@@ -357,7 +370,7 @@ def run_axe(self, task):
357
370
axe_info ['incomplete' ] = axe_results ['incomplete' ]
358
371
task ['page_data' ]['axe' ] = axe_info
359
372
except Exception as err :
360
- logging .exception ("Exception running Axe: %s" , err . __str__ () )
373
+ logging .exception ("Exception running Axe: %s" , err )
361
374
if not completed :
362
375
task ['page_data' ]['axe_failed' ] = 1
363
376
self .axe_time = monotonic () - start
@@ -384,7 +397,7 @@ def run_task(self, task):
384
397
logging .exception ("Exception running task" )
385
398
if command ['record' ]:
386
399
self .wait_for_page_load ()
387
- if not task ['combine_steps' ] or not len ( task ['script' ]) :
400
+ if not task ['combine_steps' ] or not task ['script' ]:
388
401
self .on_stop_capture (task )
389
402
self .on_stop_recording (task )
390
403
recording = False
@@ -405,10 +418,9 @@ def run_task(self, task):
405
418
self .task = None
406
419
407
420
def alert_size (self , _alert_config , _task_dir , _prefix ):
408
- '''Checks the agents file size and alert on certain percentage over avg byte size'''
421
+ '''Checks the agents file size and alert on certain percentage over avg byte size'''
409
422
self .alert_desktop_results (_alert_config , 'Firefox' , _task_dir , _prefix )
410
423
411
-
412
424
def wait_for_extension (self ):
413
425
"""Wait for the extension to send the started message"""
414
426
if self .job ['message_server' ] is not None :
@@ -514,7 +526,7 @@ def run_js_file(self, file_name):
514
526
script = None
515
527
script_file_path = os .path .join (self .script_dir , file_name )
516
528
if os .path .isfile (script_file_path ):
517
- with open (script_file_path , 'r' ) as script_file :
529
+ with open (script_file_path , 'r' , encoding = 'utf-8' ) as script_file :
518
530
script = script_file .read ()
519
531
if self .driver is not None and script is not None :
520
532
try :
@@ -526,7 +538,7 @@ def run_js_file(self, file_name):
526
538
logging .debug (ret )
527
539
return ret
528
540
529
- def get_sorted_requests_json (self , include_bodies ):
541
+ def get_sorted_requests_json (self , _include_bodies ):
530
542
return 'null'
531
543
532
544
def collect_browser_metrics (self , task ):
@@ -970,10 +982,7 @@ def process_command(self, command):
970
982
parts = command ['target' ].split (',' )
971
983
lat = float (parts [0 ])
972
984
lng = float (parts [1 ])
973
- location_uri = 'data:application/json,{{' \
974
- '"status":"OK","accuracy":{2:d},' \
975
- '"location":{{"lat":{0:f},"lng":{1:f}}}' \
976
- '}}' .format (lat , lng , accuracy )
985
+ location_uri = _get_location_uri (accuracy , lat , lng )
977
986
logging .debug ('Setting location: %s' , location_uri )
978
987
self .set_pref ('geo.wifi.uri' , location_uri )
979
988
except Exception :
0 commit comments