Skip to content

[ENG-7727] Feature/user agent #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nightly_core_functionality_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
fail-fast: false
max-parallel: 1 # run in series
matrix:
browser: [chrome, firefox, edge]
browser: [firefox, edge, chrome]
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
Expand Down
121 changes: 90 additions & 31 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from selenium import webdriver

import settings
from settings import DESIRED_CAP


def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
Expand All @@ -12,51 +13,104 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
driver_name : Name of WebDriver to use
desired_capabilities : Desired browser specs
"""
driver = None

try:
driver_cls = getattr(webdriver, driver_name)
except AttributeError:
driver_cls = getattr(webdriver, settings.DRIVER)

if driver_name == 'Remote':

if desired_capabilities is None:
desired_capabilities = settings.DESIRED_CAP
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub'.format(
settings.BSTACK_USER, settings.BSTACK_KEY
)

# NOTE: BrowserStack does support the use of Chrome Options, but we are not
# currently using any of them. Below are several steps to setup preferences
# that are specific to Firefox. Currently when running Chrome or Edge in
# BrowserStack we are running with the default base install options.

from selenium.webdriver.firefox.options import Options

ffo = Options()
# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
ffo.set_preference('browser.download.folderList', 1)
if settings.BUILD == 'firefox':
# Create a temporary Firefox WebDriver to fetch the current user agent
temp_options = webdriver.FirefoxOptions()
temp_driver = webdriver.Firefox(options=temp_options)
default_user_agent = temp_driver.execute_script(
'return navigator.userAgent;'
)
temp_driver.quit()

# Append "Selenium Bot" to the existing user agent
custom_user_agent = f'{default_user_agent} OSF Selenium Bot'

from selenium.webdriver.firefox.options import Options

ffo = Options()

# Set custom user agent
ffo.set_preference('general.useragent.override', custom_user_agent)

# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
ffo.set_preference('browser.download.folderList', 1)

# Disable the OS-level pop-up modal
ffo.set_preference('browser.download.manager.showWhenStarting', False)
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
ffo.set_preference('browser.download.manager.alertOnEXEOpen', False)
ffo.set_preference('browser.download.manager.closeWhenDone', True)
ffo.set_preference('browser.download.manager.showAlertOnComplete', False)
ffo.set_preference('browser.download.manager.useWindow', False)
# Specify the file types supported by the download
ffo.set_preference(
'browser.helperApps.neverAsk.saveToDisk',
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
)
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
# all Cross-site cookies)
ffo.set_preference('network.cookie.cookieBehavior', 4)
driver = driver_cls(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
options=ffo,
)
elif settings.BUILD == 'chrome':
from selenium.webdriver.chrome.options import Options

chrome_options: Options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('window-size=1200x600')

# Fetch default user agent
temp_driver = driver_cls(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
options=chrome_options,
)
default_user_agent = temp_driver.execute_script(
'return navigator.userAgent;'
)
temp_driver.quit()

# Append "OSF Selenium Bot" to the existing user agent
custom_user_agent = f'{default_user_agent} OSF Selenium Bot'
chrome_options.add_argument(f'user-agent={custom_user_agent}')

# Make a copy of desired capabilities for Chrome
desired_capabilities = DESIRED_CAP.copy()

# Attach Chrome options
desired_capabilities['goog:chromeOptions'] = {
'args': chrome_options.arguments
}

driver = driver_cls(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
options=chrome_options,
)
elif settings.BUILD == 'edge':
# Use default settings for edge driver
# We can update this once we upgrade to selenium v4
driver = webdriver.Edge()

# Disable the OS-level pop-up modal
ffo.set_preference('browser.download.manager.showWhenStarting', False)
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
ffo.set_preference('browser.download.manager.alertOnEXEOpen', False)
ffo.set_preference('browser.download.manager.closeWhenDone', True)
ffo.set_preference('browser.download.manager.showAlertOnComplete', False)
ffo.set_preference('browser.download.manager.useWindow', False)
# Specify the file types supported by the download
ffo.set_preference(
'browser.helperApps.neverAsk.saveToDisk',
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
)
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
# all Cross-site cookies)
ffo.set_preference('network.cookie.cookieBehavior', 4)
driver = driver_cls(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
options=ffo,
)
elif driver_name == 'Chrome' and settings.HEADLESS:
from selenium.webdriver.chrome.options import Options

Expand Down Expand Up @@ -99,6 +153,11 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
else:
driver = driver_cls()

if driver is None:
raise RuntimeError(
'WebDriver could not be instantiated based on provided configuration.'
)

driver.maximize_window()
return driver

Expand Down