Skip to content

Commit 14ae658

Browse files
committed
Add custom user agent field to remote chrome test run
1 parent ce3597c commit 14ae658

File tree

2 files changed

+89
-46
lines changed

2 files changed

+89
-46
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: 88 additions & 45 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):
@@ -19,57 +20,99 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
1920
driver_cls = getattr(webdriver, settings.DRIVER)
2021

2122
if driver_name == 'Remote':
23+
2224
if desired_capabilities is None:
2325
desired_capabilities = settings.DESIRED_CAP
2426
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub'.format(
2527
settings.BSTACK_USER, settings.BSTACK_KEY
2628
)
2729

28-
# Create a temporary Firefox WebDriver to fetch the current user agent
29-
temp_options = webdriver.FirefoxOptions()
30-
temp_driver = webdriver.Firefox(options=temp_options)
31-
default_user_agent = temp_driver.execute_script('return navigator.userAgent;')
32-
temp_driver.quit()
33-
34-
# Append "Selenium Bot" to the existing user agent
35-
custom_user_agent = f'{default_user_agent} Selenium Bot'
36-
37-
# NOTE: BrowserStack does support the use of Chrome Options, but we are not
38-
# currently using any of them. Below are several steps to setup preferences
39-
# that are specific to Firefox. Currently when running Chrome or Edge in
40-
# BrowserStack we are running with the default base install options.
41-
42-
from selenium.webdriver.firefox.options import Options
43-
44-
ffo = Options()
45-
46-
# Set custom user agent
47-
ffo.set_preference('general.useragent.override', custom_user_agent)
48-
49-
# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
50-
ffo.set_preference('browser.download.folderList', 1)
51-
52-
# Disable the OS-level pop-up modal
53-
ffo.set_preference('browser.download.manager.showWhenStarting', False)
54-
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
55-
ffo.set_preference('browser.download.manager.alertOnEXEOpen', False)
56-
ffo.set_preference('browser.download.manager.closeWhenDone', True)
57-
ffo.set_preference('browser.download.manager.showAlertOnComplete', False)
58-
ffo.set_preference('browser.download.manager.useWindow', False)
59-
# Specify the file types supported by the download
60-
ffo.set_preference(
61-
'browser.helperApps.neverAsk.saveToDisk',
62-
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
63-
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
64-
)
65-
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
66-
# all Cross-site cookies)
67-
ffo.set_preference('network.cookie.cookieBehavior', 4)
68-
driver = driver_cls(
69-
command_executor=command_executor,
70-
desired_capabilities=desired_capabilities,
71-
options=ffo,
72-
)
30+
if settings.BUILD == 'firefox':
31+
# Create a temporary Firefox WebDriver to fetch the current user agent
32+
temp_options = webdriver.FirefoxOptions()
33+
temp_driver = webdriver.Firefox(options=temp_options)
34+
default_user_agent = temp_driver.execute_script(
35+
'return navigator.userAgent;'
36+
)
37+
temp_driver.quit()
38+
39+
# Append "Selenium Bot" to the existing user agent
40+
custom_user_agent = f'{default_user_agent} OSF Selenium Bot'
41+
42+
# NOTE: BrowserStack does support the use of Chrome Options, but we are not
43+
# currently using any of them. Below are several steps to setup preferences
44+
# that are specific to Firefox. Currently when running Chrome or Edge in
45+
# BrowserStack we are running with the default base install options.
46+
47+
from selenium.webdriver.firefox.options import Options
48+
49+
ffo = Options()
50+
51+
# Set custom user agent
52+
ffo.set_preference('general.useragent.override', custom_user_agent)
53+
54+
# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
55+
ffo.set_preference('browser.download.folderList', 1)
56+
57+
# Disable the OS-level pop-up modal
58+
ffo.set_preference('browser.download.manager.showWhenStarting', False)
59+
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
60+
ffo.set_preference('browser.download.manager.alertOnEXEOpen', False)
61+
ffo.set_preference('browser.download.manager.closeWhenDone', True)
62+
ffo.set_preference('browser.download.manager.showAlertOnComplete', False)
63+
ffo.set_preference('browser.download.manager.useWindow', False)
64+
# Specify the file types supported by the download
65+
ffo.set_preference(
66+
'browser.helperApps.neverAsk.saveToDisk',
67+
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
68+
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
69+
)
70+
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
71+
# all Cross-site cookies)
72+
ffo.set_preference('network.cookie.cookieBehavior', 4)
73+
driver = driver_cls(
74+
command_executor=command_executor,
75+
desired_capabilities=desired_capabilities,
76+
options=ffo,
77+
)
78+
elif settings.BUILD == 'chrome':
79+
from selenium.webdriver.chrome.options import Options
80+
81+
chrome_options: Options = Options()
82+
chrome_options.add_argument('--disable-gpu')
83+
chrome_options.add_argument('window-size=1200x600')
84+
85+
# Fetch default user agent
86+
temp_driver = driver_cls(
87+
command_executor=command_executor,
88+
desired_capabilities=desired_capabilities,
89+
options=chrome_options,
90+
)
91+
default_user_agent = temp_driver.execute_script(
92+
'return navigator.userAgent;'
93+
)
94+
temp_driver.quit()
95+
96+
# Append "OSF Selenium Bot" to the existing user agent
97+
custom_user_agent = f'{default_user_agent} OSF Selenium Bot'
98+
chrome_options.add_argument(f'user-agent={custom_user_agent}')
99+
100+
# Make a copy of desired capabilities for Chrome
101+
desired_capabilities = DESIRED_CAP.copy()
102+
103+
# Attach Chrome options
104+
desired_capabilities['goog:chromeOptions'] = {
105+
'args': chrome_options.arguments
106+
}
107+
108+
driver = driver_cls(
109+
command_executor=command_executor,
110+
desired_capabilities=desired_capabilities,
111+
options=chrome_options,
112+
)
113+
114+
print(driver.execute_script(DESIRED_CAP))
115+
print(driver.execute_script('return navigator.userAgent;'))
73116

74117
elif driver_name == 'Chrome' and settings.HEADLESS:
75118
from selenium.webdriver.chrome.options import Options

0 commit comments

Comments
 (0)