Skip to content

Commit a3dde7e

Browse files
authored
Merge pull request #277 from CenterForOpenScience/feature/user-agent
[ENG-7727] Feature/user agent
2 parents 0fdd942 + d5f3f2c commit a3dde7e

File tree

2 files changed

+91
-32
lines changed

2 files changed

+91
-32
lines changed

.github/workflows/nightly_core_functionality_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
fail-fast: false
8181
max-parallel: 1 # run in series
8282
matrix:
83-
browser: [chrome, firefox, edge]
83+
browser: [firefox, edge, chrome]
8484
steps:
8585
- uses: actions/checkout@v4
8686
- name: Set up Python 3.9

utils.py

Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from selenium import webdriver
55

66
import settings
7+
from settings import DESIRED_CAP
78

89

910
def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
@@ -12,51 +13,104 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
1213
driver_name : Name of WebDriver to use
1314
desired_capabilities : Desired browser specs
1415
"""
16+
driver = None
1517

1618
try:
1719
driver_cls = getattr(webdriver, driver_name)
1820
except AttributeError:
1921
driver_cls = getattr(webdriver, settings.DRIVER)
2022

2123
if driver_name == 'Remote':
24+
2225
if desired_capabilities is None:
2326
desired_capabilities = settings.DESIRED_CAP
2427
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub'.format(
2528
settings.BSTACK_USER, settings.BSTACK_KEY
2629
)
2730

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()
38113

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-
)
60114
elif driver_name == 'Chrome' and settings.HEADLESS:
61115
from selenium.webdriver.chrome.options import Options
62116

@@ -99,6 +153,11 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
99153
else:
100154
driver = driver_cls()
101155

156+
if driver is None:
157+
raise RuntimeError(
158+
'WebDriver could not be instantiated based on provided configuration.'
159+
)
160+
102161
driver.maximize_window()
103162
return driver
104163

0 commit comments

Comments
 (0)