Skip to content

Commit c01642e

Browse files
authored
Merge pull request #546 from seleniumbase/fix-chrome-proxy-with-auth
Fix Chrome proxy with auth; and a few other updates
2 parents 4fe0dc7 + b8d4f4d commit c01642e

File tree

12 files changed

+81
-32
lines changed

12 files changed

+81
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ SeleniumBase provides additional Pytest command-line options for tests:
242242
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
243243
--disable-csp # (This disables the Content Security Policy of websites.)
244244
--enable-sync # (The option to enable "Chrome Sync".)
245+
--use-auto-ext # (The option to use Chrome's automation extension.)
245246
--no-sandbox # (The option to enable Chrome's "No-Sandbox" feature.)
246247
--disable-gpu # (The option to enable Chrome's "Disable GPU" feature.)
247248
--incognito # (The option to enable Chrome's Incognito mode.)

examples/capabilities/ReadMe.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ A regex parser was built into SeleniumBase to capture all lines from the specifi
5050
``caps['KEY'] = False``
5151
(Each pair must be on a separate line. You can interchange single and double quotes.)
5252

53-
You can also swap ``--browser=remote`` with an actual browser, eg ``--browser=chrome``, which will combine the default SeleniumBase desired capabilities with those that were specified in the capabilities file when using ``--cap_file=FILE.py``. Capabilities will override other parameters, so if you set the browser to one thing and the capabilities browser to another, SeleniumBase will use the capabilities browser as the browser. You'll need default SeleniumBase desired capabilities when using a proxy server (not the same as a Selenium Grid server), when downloading files to a desired folder, for disabling some warnings on Chrome, for overriding a website's Content Security Policy on Firefox, and for other reasons.
53+
You can also swap ``--browser=remote`` with an actual browser, eg ``--browser=chrome``, which will combine the default SeleniumBase desired capabilities with those that were specified in the capabilities file when using ``--cap_file=FILE.py``. Capabilities will override other parameters, so if you set the browser to one thing and the capabilities browser to another, SeleniumBase will use the capabilities browser as the browser.
54+
55+
You'll need default SeleniumBase capabilities for:
56+
* Using a proxy server (not the same as a Selenium Grid server)
57+
* Downloading files to a desired folder
58+
* Disabling some warnings on Chrome
59+
* Overriding a website's Content Security Policy on Chrome
60+
* Other possible reasons
5461

5562
You can also set browser desired capabilities from a command line string:
5663
Example:
@@ -59,8 +66,17 @@ pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"test1"}'
5966
```
6067
(Enclose cap-string in single quotes. Enclose parameter keys in double quotes.)
6168

62-
If using a local Selenium Grid with SeleniumBase, make sure to start up the Grid Hub and nodes first:
69+
If you pass ``"*"`` into the ``"name"`` field of ``--cap-string``, the name will become the test identifier. Example:
70+
```bash
71+
pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
72+
```
73+
Example name: ``"my_first_test.MyTestClass.test_basic"``
74+
75+
### Using a local Selenium Grid
76+
77+
If using a local Selenium Grid with SeleniumBase, start up the Grid Hub and nodes first:
6378
```bash
6479
seleniumbase grid-hub start
6580
seleniumbase grid-node start
6681
```
82+
(The Selenium Server JAR file will be automatically downloaded for first-time Grid users. You'll also need Java installed to start up the Grid.)

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
sb.archive_logs = False
4848
sb.disable_csp = False
4949
sb.enable_sync = False
50+
sb.use_auto_ext = False
5051
sb.no_sandbox = False
5152
sb.disable_gpu = False
5253
sb._reuse_session = False

help_docs/customizing_test_runs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ SeleniumBase provides additional Pytest command-line options for tests:
115115
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
116116
--disable-csp # (This disables the Content Security Policy of websites.)
117117
--enable-sync # (The option to enable "Chrome Sync".)
118+
--use-auto-ext # (The option to use Chrome's automation extension.)
118119
--no-sandbox # (The option to enable Chrome's "No-Sandbox" feature.)
119120
--disable-gpu # (The option to enable Chrome's "Disable GPU" feature.)
120121
--incognito # (The option to enable Chrome's Incognito mode.)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ soupsieve==1.9.5;python_version<"3.5"
2929
soupsieve==2.0;python_version>="3.5"
3030
beautifulsoup4==4.9.0
3131
atomicwrites==1.3.0
32-
portalocker==1.7.0
3332
cryptography==2.9
3433
pyopenssl==19.1.0
3534
pygments==2.5.2;python_version<"3.5"

seleniumbase/config/proxy_list.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"example1": "142.93.130.169:8118", # (Example) - set your own proxy here
2424
"example2": "51.91.212.159:3128", # (Example) - set your own proxy here
2525
"example3": "109.74.142.138:53281", # (Example) - set your own proxy here
26-
"example4": "91.207.60.241:1282", # (Example) - set your own proxy here
2726
"proxy1": None,
2827
"proxy2": None,
2928
"proxy3": None,

seleniumbase/console_scripts/run.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def show_basic_usage():
5353
print(" inject-objects [SELENIUMBASE_PYTHON_FILE] [OPTIONS]")
5454
print(" objectify [SELENIUMBASE_PYTHON_FILE] [OPTIONS]")
5555
print(" revert-objects [SELENIUMBASE_PYTHON_FILE]")
56-
print(" encrypt OR obfuscate")
57-
print(" decrypt OR unobfuscate")
58-
print(" download server")
56+
print(" encrypt (OR: obfuscate)")
57+
print(" decrypt (OR: unobfuscate)")
58+
print(" download server (The Selenium Server JAR file)")
5959
print(" grid-hub [start|stop|restart] [OPTIONS]")
6060
print(" grid-node [start|stop|restart] --hub=[HUB_IP] [OPTIONS]")
6161
print(' * (EXAMPLE: "seleniumbase install chromedriver") *')
@@ -259,7 +259,12 @@ def show_grid_node_usage():
259259

260260
def get_version():
261261
import pkg_resources
262-
return pkg_resources.require("seleniumbase")[0:1]
262+
version_info = None
263+
try:
264+
version_info = pkg_resources.require("seleniumbase")[0:1]
265+
except Exception:
266+
version_info = ["ERROR: Cannot detect version! Please reinstall!"]
267+
return version_info
263268

264269

265270
def show_version_info():

seleniumbase/core/browser_launcher.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def _add_chrome_disable_csp_extension(chrome_options):
132132
def _set_chrome_options(
133133
downloads_path, headless,
134134
proxy_string, proxy_auth, proxy_user, proxy_pass,
135-
user_agent, disable_csp, enable_sync, no_sandbox, disable_gpu,
136-
incognito, guest_mode, devtools,
135+
user_agent, disable_csp, enable_sync, use_auto_ext,
136+
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
137137
user_data_dir, extension_zip, extension_dir, servername,
138138
mobile_emulator, device_width, device_height, device_pixel_ratio):
139139
chrome_options = webdriver.ChromeOptions()
@@ -211,15 +211,12 @@ def _set_chrome_options(
211211
chrome_options.add_argument("--disable-web-security")
212212
chrome_options.add_argument("--homepage=about:blank")
213213
chrome_options.add_argument("--dom-automation")
214-
if servername == "localhost" or servername == "127.0.0.1":
214+
if not use_auto_ext: # (It's ON by default. Disable it when not wanted.)
215215
chrome_options.add_experimental_option("useAutomationExtension", False)
216216
if (settings.DISABLE_CSP_ON_CHROME or disable_csp) and not headless:
217217
# Headless Chrome doesn't support extensions, which are required
218218
# for disabling the Content Security Policy on Chrome
219219
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
220-
elif not extension_zip and not extension_dir:
221-
if servername == "localhost" or servername == "127.0.0.1":
222-
chrome_options.add_argument("--disable-extensions")
223220
if proxy_string:
224221
if proxy_auth:
225222
chrome_options = _add_chrome_proxy_extension(
@@ -351,11 +348,11 @@ def validate_proxy_string(proxy_string):
351348
def get_driver(browser_name, headless=False, use_grid=False,
352349
servername='localhost', port=4444, proxy_string=None,
353350
user_agent=None, cap_file=None, cap_string=None,
354-
disable_csp=None, enable_sync=None,
351+
disable_csp=None, enable_sync=None, use_auto_ext=None,
355352
no_sandbox=None, disable_gpu=None,
356353
incognito=None, guest_mode=None, devtools=None,
357354
user_data_dir=None, extension_zip=None, extension_dir=None,
358-
mobile_emulator=False, device_width=None,
355+
test_id=None, mobile_emulator=False, device_width=None,
359356
device_height=None, device_pixel_ratio=None):
360357
proxy_auth = False
361358
proxy_user = None
@@ -390,15 +387,15 @@ def get_driver(browser_name, headless=False, use_grid=False,
390387
return get_remote_driver(
391388
browser_name, headless, servername, port,
392389
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
393-
cap_file, cap_string, disable_csp, enable_sync,
390+
cap_file, cap_string, disable_csp, enable_sync, use_auto_ext,
394391
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
395-
user_data_dir, extension_zip, extension_dir,
392+
user_data_dir, extension_zip, extension_dir, test_id,
396393
mobile_emulator, device_width, device_height, device_pixel_ratio)
397394
else:
398395
return get_local_driver(
399396
browser_name, headless, servername,
400397
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
401-
disable_csp, enable_sync, no_sandbox, disable_gpu,
398+
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
402399
incognito, guest_mode, devtools,
403400
user_data_dir, extension_zip, extension_dir,
404401
mobile_emulator, device_width, device_height, device_pixel_ratio)
@@ -407,9 +404,9 @@ def get_driver(browser_name, headless=False, use_grid=False,
407404
def get_remote_driver(
408405
browser_name, headless, servername, port, proxy_string, proxy_auth,
409406
proxy_user, proxy_pass, user_agent, cap_file, cap_string,
410-
disable_csp, enable_sync, no_sandbox, disable_gpu,
407+
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
411408
incognito, guest_mode, devtools,
412-
user_data_dir, extension_zip, extension_dir,
409+
user_data_dir, extension_zip, extension_dir, test_id,
413410
mobile_emulator, device_width, device_height, device_pixel_ratio):
414411
downloads_path = download_helper.get_downloads_folder()
415412
download_helper.reset_downloads_folder()
@@ -424,17 +421,21 @@ def get_remote_driver(
424421
except Exception as e:
425422
p1 = "Invalid input format for --cap-string:\n %s" % e
426423
p2 = "The --cap-string input was: %s" % cap_string
427-
p3 = "Enclose cap-string in single quotes; keys in double quotes."
424+
p3 = "Enclose cap-string in SINGLE quotes; keys in DOUBLE quotes."
428425
p4 = ("""Here's an example of correct cap-string usage:\n """
429426
"""--cap-string='{"browserName":"chrome","name":"test1"}'""")
430427
raise Exception("%s\n%s\n%s\n%s" % (p1, p2, p3, p4))
431428
for cap_key in extra_caps.keys():
432429
desired_caps[cap_key] = extra_caps[cap_key]
430+
if cap_file or cap_string:
431+
if "name" in desired_caps.keys():
432+
if desired_caps["name"] == "*":
433+
desired_caps["name"] = test_id
433434
if browser_name == constants.Browser.GOOGLE_CHROME:
434435
chrome_options = _set_chrome_options(
435436
downloads_path, headless,
436437
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
437-
disable_csp, enable_sync, no_sandbox, disable_gpu,
438+
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
438439
incognito, guest_mode, devtools,
439440
user_data_dir, extension_zip, extension_dir, servername,
440441
mobile_emulator, device_width, device_height, device_pixel_ratio)
@@ -546,7 +547,7 @@ def get_remote_driver(
546547
def get_local_driver(
547548
browser_name, headless, servername,
548549
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
549-
disable_csp, enable_sync, no_sandbox, disable_gpu,
550+
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
550551
incognito, guest_mode, devtools,
551552
user_data_dir, extension_zip, extension_dir,
552553
mobile_emulator, device_width, device_height, device_pixel_ratio):
@@ -636,8 +637,8 @@ def get_local_driver(
636637
chrome_options = _set_chrome_options(
637638
downloads_path, headless,
638639
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
639-
disable_csp, enable_sync, no_sandbox, disable_gpu,
640-
incognito, guest_mode, devtools,
640+
disable_csp, enable_sync, use_auto_ext,
641+
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
641642
user_data_dir, extension_zip, extension_dir, servername,
642643
mobile_emulator, device_width, device_height,
643644
device_pixel_ratio)
@@ -692,8 +693,8 @@ def get_local_driver(
692693
chrome_options = _set_chrome_options(
693694
downloads_path, headless,
694695
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
695-
disable_csp, enable_sync, no_sandbox, disable_gpu,
696-
incognito, guest_mode, devtools,
696+
disable_csp, enable_sync, use_auto_ext,
697+
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
697698
user_data_dir, extension_zip, extension_dir, servername,
698699
mobile_emulator, device_width, device_height,
699700
device_pixel_ratio)

seleniumbase/fixtures/base_case.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ def switch_to_default_window(self):
15221522
def get_new_driver(self, browser=None, headless=None,
15231523
servername=None, port=None, proxy=None, agent=None,
15241524
switch_to=True, cap_file=None, cap_string=None,
1525-
disable_csp=None, enable_sync=None,
1525+
disable_csp=None, enable_sync=None, use_auto_ext=None,
15261526
no_sandbox=None, disable_gpu=None,
15271527
incognito=None, guest_mode=None, devtools=None,
15281528
user_data_dir=None, extension_zip=None,
@@ -1543,6 +1543,7 @@ def get_new_driver(self, browser=None, headless=None,
15431543
cap_string - the string with desired capabilities for the browser
15441544
disable_csp - an option to disable Chrome's Content Security Policy
15451545
enable_sync - the option to enable the Chrome Sync feature (Chrome)
1546+
use_auto_ext - the option to enable Chrome's Automation Extension
15461547
no_sandbox - the option to enable the "No-Sandbox" feature (Chrome)
15471548
disable_gpu - the option to enable Chrome's "Disable GPU" feature
15481549
incognito - the option to enable Chrome's Incognito mode (Chrome)
@@ -1600,6 +1601,8 @@ def get_new_driver(self, browser=None, headless=None,
16001601
disable_csp = self.disable_csp
16011602
if enable_sync is None:
16021603
enable_sync = self.enable_sync
1604+
if use_auto_ext is None:
1605+
use_auto_ext = self.use_auto_ext
16031606
if no_sandbox is None:
16041607
no_sandbox = self.no_sandbox
16051608
if disable_gpu is None:
@@ -1619,6 +1622,7 @@ def get_new_driver(self, browser=None, headless=None,
16191622
# Due to https://stackoverflow.com/questions/23055651/ , skip extension
16201623
# if self.demo_mode or self.masterqa_mode:
16211624
# disable_csp = True
1625+
test_id = self.__get_test_id()
16221626
if cap_file is None:
16231627
cap_file = self.cap_file
16241628
if cap_string is None:
@@ -1648,6 +1652,7 @@ def get_new_driver(self, browser=None, headless=None,
16481652
cap_string=cap_string,
16491653
disable_csp=disable_csp,
16501654
enable_sync=enable_sync,
1655+
use_auto_ext=use_auto_ext,
16511656
no_sandbox=no_sandbox,
16521657
disable_gpu=disable_gpu,
16531658
incognito=incognito,
@@ -1656,6 +1661,7 @@ def get_new_driver(self, browser=None, headless=None,
16561661
user_data_dir=user_data_dir,
16571662
extension_zip=extension_zip,
16581663
extension_dir=extension_dir,
1664+
test_id=test_id,
16591665
mobile_emulator=is_mobile,
16601666
device_width=d_width,
16611667
device_height=d_height,
@@ -4547,6 +4553,7 @@ def setUp(self, masterqa_mode=False):
45474553
self.verify_delay = sb_config.verify_delay
45484554
self.disable_csp = sb_config.disable_csp
45494555
self.enable_sync = sb_config.enable_sync
4556+
self.use_auto_ext = sb_config.use_auto_ext
45504557
self.no_sandbox = sb_config.no_sandbox
45514558
self.disable_gpu = sb_config.disable_gpu
45524559
self.incognito = sb_config.incognito
@@ -4705,6 +4712,7 @@ def setUp(self, masterqa_mode=False):
47054712
cap_string=self.cap_string,
47064713
disable_csp=self.disable_csp,
47074714
enable_sync=self.enable_sync,
4715+
use_auto_ext=self.use_auto_ext,
47084716
no_sandbox=self.no_sandbox,
47094717
disable_gpu=self.disable_gpu,
47104718
incognito=self.incognito,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def pytest_addoption(parser):
4545
--verify-delay=SECONDS (The delay before MasterQA verification checks.)
4646
--disable-csp (This disables the Content Security Policy of websites.)
4747
--enable-sync (The option to enable "Chrome Sync".)
48+
--use-auto-ext (The option to use Chrome's automation extension.)
4849
--no-sandbox (The option to enable Chrome's "No-Sandbox" feature.)
4950
--disable-gpu (The option to enable Chrome's "Disable GPU" feature.)
5051
--incognito (The option to enable Chrome's Incognito mode.)
@@ -350,6 +351,13 @@ def pytest_addoption(parser):
350351
dest='enable_sync',
351352
default=False,
352353
help="""Using this enables the "Chrome Sync" feature.""")
354+
parser.addoption('--use_auto_ext', '--use-auto-ext', '--auto-ext',
355+
action="store_true",
356+
dest='use_auto_ext',
357+
default=False,
358+
help="""Using this enables Chrome's Automation Extension.
359+
It's not required, but some commands & advanced
360+
features may need it.""")
353361
parser.addoption('--no_sandbox', '--no-sandbox',
354362
action="store_true",
355363
dest='no_sandbox',
@@ -462,6 +470,7 @@ def pytest_configure(config):
462470
sb_config.verify_delay = config.getoption('verify_delay')
463471
sb_config.disable_csp = config.getoption('disable_csp')
464472
sb_config.enable_sync = config.getoption('enable_sync')
473+
sb_config.use_auto_ext = config.getoption('use_auto_ext')
465474
sb_config.no_sandbox = config.getoption('no_sandbox')
466475
sb_config.disable_gpu = config.getoption('disable_gpu')
467476
sb_config.incognito = config.getoption('incognito')

0 commit comments

Comments
 (0)