Skip to content

Commit 75f49a1

Browse files
committed
Add option to set capabilities with --cap-string=STRING
1 parent ffc44bf commit 75f49a1

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
sb.highlights = None
7373
sb.check_js = False
7474
sb.cap_file = None
75+
sb.cap_string = None
7576

7677
sb.setUp()
7778
try:

seleniumbase/core/browser_launcher.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import os
34
import random
@@ -345,8 +346,9 @@ def validate_proxy_string(proxy_string):
345346

346347
def get_driver(browser_name, headless=False, use_grid=False,
347348
servername='localhost', port=4444, proxy_string=None,
348-
user_agent=None, cap_file=None, disable_csp=None,
349-
enable_sync=None, no_sandbox=None, disable_gpu=None,
349+
user_agent=None, cap_file=None, cap_string=None,
350+
disable_csp=None, enable_sync=None,
351+
no_sandbox=None, disable_gpu=None,
350352
incognito=None, guest_mode=None, devtools=None,
351353
user_data_dir=None, extension_zip=None, extension_dir=None,
352354
mobile_emulator=False, device_width=None,
@@ -384,8 +386,8 @@ def get_driver(browser_name, headless=False, use_grid=False,
384386
return get_remote_driver(
385387
browser_name, headless, servername, port,
386388
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
387-
cap_file, disable_csp, enable_sync, no_sandbox, disable_gpu,
388-
incognito, guest_mode, devtools,
389+
cap_file, cap_string, disable_csp, enable_sync,
390+
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
389391
user_data_dir, extension_zip, extension_dir,
390392
mobile_emulator, device_width, device_height, device_pixel_ratio)
391393
else:
@@ -400,17 +402,30 @@ def get_driver(browser_name, headless=False, use_grid=False,
400402

401403
def get_remote_driver(
402404
browser_name, headless, servername, port, proxy_string, proxy_auth,
403-
proxy_user, proxy_pass, user_agent, cap_file, disable_csp,
404-
enable_sync, no_sandbox, disable_gpu,
405+
proxy_user, proxy_pass, user_agent, cap_file, cap_string,
406+
disable_csp, enable_sync, no_sandbox, disable_gpu,
405407
incognito, guest_mode, devtools,
406408
user_data_dir, extension_zip, extension_dir,
407409
mobile_emulator, device_width, device_height, device_pixel_ratio):
408410
downloads_path = download_helper.get_downloads_folder()
409411
download_helper.reset_downloads_folder()
410412
address = "http://%s:%s/wd/hub" % (servername, port)
411413
desired_caps = {}
414+
extra_caps = {}
412415
if cap_file:
413416
desired_caps = capabilities_parser.get_desired_capabilities(cap_file)
417+
if cap_string:
418+
try:
419+
extra_caps = json.loads(cap_string)
420+
except Exception as e:
421+
p1 = "Invalid input format for --cap-string:\n %s" % e
422+
p2 = "The --cap-string input was: %s" % cap_string
423+
p3 = "Enclose cap-string in single quotes; keys in double quotes."
424+
p4 = ("""Here's an example of correct cap-string usage:\n """
425+
"""--cap-string='{"browserName":"chrome","name":"test1"}'""")
426+
raise Exception("%s\n%s\n%s\n%s" % (p1, p2, p3, p4))
427+
for cap_key in extra_caps.keys():
428+
desired_caps[cap_key] = extra_caps[cap_key]
414429
if browser_name == constants.Browser.GOOGLE_CHROME:
415430
chrome_options = _set_chrome_options(
416431
downloads_path, headless,

seleniumbase/fixtures/base_case.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,9 @@ def switch_to_default_window(self):
15211521

15221522
def get_new_driver(self, browser=None, headless=None,
15231523
servername=None, port=None, proxy=None, agent=None,
1524-
switch_to=True, cap_file=None, disable_csp=None,
1525-
enable_sync=None, no_sandbox=None, disable_gpu=None,
1524+
switch_to=True, cap_file=None, cap_string=None,
1525+
disable_csp=None, enable_sync=None,
1526+
no_sandbox=None, disable_gpu=None,
15261527
incognito=None, guest_mode=None, devtools=None,
15271528
user_data_dir=None, extension_zip=None,
15281529
extension_dir=None, is_mobile=False,
@@ -1539,6 +1540,7 @@ def get_new_driver(self, browser=None, headless=None,
15391540
proxy - if using a proxy server, specify the "host:port" combo here
15401541
switch_to - the option to switch to the new driver (default = True)
15411542
cap_file - the file containing desired capabilities for the browser
1543+
cap_string - the string with desired capabilities for the browser
15421544
disable_csp - an option to disable Chrome's Content Security Policy
15431545
enable_sync - the option to enable the Chrome Sync feature (Chrome)
15441546
no_sandbox - the option to enable the "No-Sandbox" feature (Chrome)
@@ -1566,7 +1568,7 @@ def get_new_driver(self, browser=None, headless=None,
15661568
'https://browserstack.com/automate/capabilities')
15671569
sauce_labs_ref = (
15681570
'https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/')
1569-
if self.browser == "remote" and not self.cap_file:
1571+
if self.browser == "remote" and not (self.cap_file or self.cap_string):
15701572
raise Exception('Need to specify a desired capabilities file when '
15711573
'using "--browser=remote". Add "--cap_file=FILE". '
15721574
'File should be in the Python format used by: '
@@ -1619,6 +1621,8 @@ def get_new_driver(self, browser=None, headless=None,
16191621
# disable_csp = True
16201622
if cap_file is None:
16211623
cap_file = self.cap_file
1624+
if cap_string is None:
1625+
cap_string = self.cap_string
16221626
if is_mobile is None:
16231627
is_mobile = False
16241628
if d_width is None:
@@ -1641,6 +1645,7 @@ def get_new_driver(self, browser=None, headless=None,
16411645
proxy_string=proxy_string,
16421646
user_agent=user_agent,
16431647
cap_file=cap_file,
1648+
cap_string=cap_string,
16441649
disable_csp=disable_csp,
16451650
enable_sync=enable_sync,
16461651
no_sandbox=no_sandbox,
@@ -4530,6 +4535,7 @@ def setUp(self, masterqa_mode=False):
45304535
self.mobile_emulator = sb_config.mobile_emulator
45314536
self.device_metrics = sb_config.device_metrics
45324537
self.cap_file = sb_config.cap_file
4538+
self.cap_string = sb_config.cap_string
45334539
self.settings_file = sb_config.settings_file
45344540
self.database_env = sb_config.database_env
45354541
self.message_duration = sb_config.message_duration
@@ -4693,6 +4699,7 @@ def setUp(self, masterqa_mode=False):
46934699
agent=self.user_agent,
46944700
switch_to=True,
46954701
cap_file=self.cap_file,
4702+
cap_string=self.cap_string,
46964703
disable_csp=self.disable_csp,
46974704
enable_sync=self.enable_sync,
46984705
no_sandbox=self.no_sandbox,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def pytest_addoption(parser):
1414
This parser plugin includes the following command-line options for pytest:
1515
--browser=BROWSER (The web browser to use.)
1616
--cap-file=FILE (The web browser's desired capabilities to use.)
17+
--cap-string=STRING (The web browser's desired capabilities to use.)
1718
--settings-file=FILE (Overrides SeleniumBase settings.py values.)
1819
--env=ENV (Set a test environment. Use "self.env" to use this in tests.)
1920
--data=DATA (Extra data to pass to tests. Use "self.data" in tests.)
@@ -91,13 +92,24 @@ def pytest_addoption(parser):
9192
dest='cap_file',
9293
default=None,
9394
help="""The file that stores browser desired capabilities
94-
for BrowserStack or Sauce Labs web drivers.""")
95+
for BrowserStack, Sauce Labs, and other
96+
remote web drivers to use.""")
97+
parser.addoption('--cap_string', '--cap-string',
98+
dest='cap_string',
99+
default=None,
100+
help="""The string that stores browser desired
101+
capabilities for BrowserStack, Sauce Labs,
102+
and other remote web drivers to use.
103+
Enclose cap-string in single quotes.
104+
Enclose parameter keys in double quotes.
105+
Example: --cap-string='{"name":"test1","v":"42"}'""")
95106
parser.addoption('--settings_file', '--settings-file', '--settings',
96107
action='store',
97108
dest='settings_file',
98109
default=None,
99-
help="""The file that stores key/value pairs for overriding
100-
values in the SeleniumBase settings.py file.""")
110+
help="""The file that stores key/value pairs for
111+
overriding values in the
112+
seleniumbase/config/settings.py file.""")
101113
parser.addoption('--user_data_dir', '--user-data-dir',
102114
dest='user_data_dir',
103115
default=None,
@@ -415,6 +427,7 @@ def pytest_configure(config):
415427
sb_config.port = config.getoption('port')
416428
sb_config.proxy_string = config.getoption('proxy_string')
417429
sb_config.cap_file = config.getoption('cap_file')
430+
sb_config.cap_string = config.getoption('cap_string')
418431
sb_config.settings_file = config.getoption('settings_file')
419432
sb_config.user_data_dir = config.getoption('user_data_dir')
420433
sb_config.database_env = config.getoption('database_env')

seleniumbase/plugins/selenium_plugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SeleniumBrowser(Plugin):
1212
This parser plugin includes the following command-line options for Nose:
1313
--browser=BROWSER (The web browser to use.)
1414
--cap-file=FILE (The web browser's desired capabilities to use.)
15+
--cap-string=STRING (The web browser's desired capabilities to use.)
1516
--user-data-dir=DIR (Set the Chrome user data directory to use.)
1617
--server=SERVER (The server / IP address used by the tests.)
1718
--port=PORT (The port that's used by the test server.)
@@ -73,6 +74,16 @@ def options(self, parser, env):
7374
default=None,
7475
help="""The file that stores browser desired capabilities
7576
for BrowserStack or Sauce Labs web drivers.""")
77+
parser.add_option(
78+
'--cap_string', '--cap-string',
79+
dest='cap_string',
80+
default=None,
81+
help="""The string that stores browser desired
82+
capabilities for BrowserStack, Sauce Labs,
83+
and other remote web drivers to use.
84+
Enclose cap-string in single quotes.
85+
Enclose parameter keys in double quotes.
86+
Example: --cap-string='{"name":"test1","v":"42"}'""")
7687
parser.add_option(
7788
'--user_data_dir', '--user-data-dir',
7889
action='store',
@@ -326,6 +337,7 @@ def configure(self, options, conf):
326337
def beforeTest(self, test):
327338
test.test.browser = self.options.browser
328339
test.test.cap_file = self.options.cap_file
340+
test.test.cap_string = self.options.cap_string
329341
test.test.headless = self.options.headless
330342
test.test.headed = self.options.headed
331343
test.test.start_page = self.options.start_page

0 commit comments

Comments
 (0)