4
4
from selenium import webdriver
5
5
6
6
import settings
7
+ from settings import DESIRED_CAP
7
8
8
9
9
10
def launch_driver (driver_name = settings .DRIVER , desired_capabilities = None ):
@@ -12,51 +13,104 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
12
13
driver_name : Name of WebDriver to use
13
14
desired_capabilities : Desired browser specs
14
15
"""
16
+ driver = None
15
17
16
18
try :
17
19
driver_cls = getattr (webdriver , driver_name )
18
20
except AttributeError :
19
21
driver_cls = getattr (webdriver , settings .DRIVER )
20
22
21
23
if driver_name == 'Remote' :
24
+
22
25
if desired_capabilities is None :
23
26
desired_capabilities = settings .DESIRED_CAP
24
27
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub' .format (
25
28
settings .BSTACK_USER , settings .BSTACK_KEY
26
29
)
27
30
28
- # NOTE: BrowserStack does support the use of Chrome Options, but we are not
29
- # currently using any of them. Below are several steps to setup preferences
30
- # that are specific to Firefox. Currently when running Chrome or Edge in
31
- # BrowserStack we are running with the default base install options.
32
-
33
- from selenium .webdriver .firefox .options import Options
34
-
35
- ffo = Options ()
36
- # Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
37
- ffo .set_preference ('browser.download.folderList' , 1 )
31
+ if settings .BUILD == 'firefox' :
32
+ # Create a temporary Firefox WebDriver to fetch the current user agent
33
+ temp_options = webdriver .FirefoxOptions ()
34
+ temp_driver = webdriver .Firefox (options = temp_options )
35
+ default_user_agent = temp_driver .execute_script (
36
+ 'return navigator.userAgent;'
37
+ )
38
+ temp_driver .quit ()
39
+
40
+ # Append "Selenium Bot" to the existing user agent
41
+ custom_user_agent = f'{ default_user_agent } OSF Selenium Bot'
42
+
43
+ from selenium .webdriver .firefox .options import Options
44
+
45
+ ffo = Options ()
46
+
47
+ # Set custom user agent
48
+ ffo .set_preference ('general.useragent.override' , custom_user_agent )
49
+
50
+ # Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
51
+ ffo .set_preference ('browser.download.folderList' , 1 )
52
+
53
+ # Disable the OS-level pop-up modal
54
+ ffo .set_preference ('browser.download.manager.showWhenStarting' , False )
55
+ ffo .set_preference ('browser.helperApps.alwaysAsk.force' , False )
56
+ ffo .set_preference ('browser.download.manager.alertOnEXEOpen' , False )
57
+ ffo .set_preference ('browser.download.manager.closeWhenDone' , True )
58
+ ffo .set_preference ('browser.download.manager.showAlertOnComplete' , False )
59
+ ffo .set_preference ('browser.download.manager.useWindow' , False )
60
+ # Specify the file types supported by the download
61
+ ffo .set_preference (
62
+ 'browser.helperApps.neverAsk.saveToDisk' ,
63
+ 'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
64
+ 'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream' ,
65
+ )
66
+ # Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
67
+ # all Cross-site cookies)
68
+ ffo .set_preference ('network.cookie.cookieBehavior' , 4 )
69
+ driver = driver_cls (
70
+ command_executor = command_executor ,
71
+ desired_capabilities = desired_capabilities ,
72
+ options = ffo ,
73
+ )
74
+ elif settings .BUILD == 'chrome' :
75
+ from selenium .webdriver .chrome .options import Options
76
+
77
+ chrome_options : Options = Options ()
78
+ chrome_options .add_argument ('--disable-gpu' )
79
+ chrome_options .add_argument ('window-size=1200x600' )
80
+
81
+ # Fetch default user agent
82
+ temp_driver = driver_cls (
83
+ command_executor = command_executor ,
84
+ desired_capabilities = desired_capabilities ,
85
+ options = chrome_options ,
86
+ )
87
+ default_user_agent = temp_driver .execute_script (
88
+ 'return navigator.userAgent;'
89
+ )
90
+ temp_driver .quit ()
91
+
92
+ # Append "OSF Selenium Bot" to the existing user agent
93
+ custom_user_agent = f'{ default_user_agent } OSF Selenium Bot'
94
+ chrome_options .add_argument (f'user-agent={ custom_user_agent } ' )
95
+
96
+ # Make a copy of desired capabilities for Chrome
97
+ desired_capabilities = DESIRED_CAP .copy ()
98
+
99
+ # Attach Chrome options
100
+ desired_capabilities ['goog:chromeOptions' ] = {
101
+ 'args' : chrome_options .arguments
102
+ }
103
+
104
+ driver = driver_cls (
105
+ command_executor = command_executor ,
106
+ desired_capabilities = desired_capabilities ,
107
+ options = chrome_options ,
108
+ )
109
+ elif settings .BUILD == 'edge' :
110
+ # Use default settings for edge driver
111
+ # We can update this once we upgrade to selenium v4
112
+ driver = webdriver .Edge ()
38
113
39
- # Disable the OS-level pop-up modal
40
- ffo .set_preference ('browser.download.manager.showWhenStarting' , False )
41
- ffo .set_preference ('browser.helperApps.alwaysAsk.force' , False )
42
- ffo .set_preference ('browser.download.manager.alertOnEXEOpen' , False )
43
- ffo .set_preference ('browser.download.manager.closeWhenDone' , True )
44
- ffo .set_preference ('browser.download.manager.showAlertOnComplete' , False )
45
- ffo .set_preference ('browser.download.manager.useWindow' , False )
46
- # Specify the file types supported by the download
47
- ffo .set_preference (
48
- 'browser.helperApps.neverAsk.saveToDisk' ,
49
- 'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
50
- 'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream' ,
51
- )
52
- # Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
53
- # all Cross-site cookies)
54
- ffo .set_preference ('network.cookie.cookieBehavior' , 4 )
55
- driver = driver_cls (
56
- command_executor = command_executor ,
57
- desired_capabilities = desired_capabilities ,
58
- options = ffo ,
59
- )
60
114
elif driver_name == 'Chrome' and settings .HEADLESS :
61
115
from selenium .webdriver .chrome .options import Options
62
116
@@ -99,6 +153,11 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
99
153
else :
100
154
driver = driver_cls ()
101
155
156
+ if driver is None :
157
+ raise RuntimeError (
158
+ 'WebDriver could not be instantiated based on provided configuration.'
159
+ )
160
+
102
161
driver .maximize_window ()
103
162
return driver
104
163
0 commit comments