Skip to content

Commit 6c59e68

Browse files
authored
Merge pull request #454 from seleniumbase/js-updates-and-refactoring
JS Optimization and Refactoring
2 parents 43ae2b8 + ef566ad commit 6c59e68

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

seleniumbase/config/proxy_list.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
PROXY_LIST = {
2222
"example1": "104.236.248.219:3128", # (Example) - set your own proxy here
23-
"example2": "81.4.255.25:3128", # (Example) - set your own proxy here
24-
"example3": "52.187.121.7:3128", # (Example) - set your own proxy here
23+
"example2": "52.187.121.7:3128", # (Example) - set your own proxy here
2524
"proxy1": None,
2625
"proxy2": None,
2726
"proxy3": None,

seleniumbase/core/browser_launcher.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import random
34
import re
@@ -519,8 +520,8 @@ def get_local_driver(
519520
try:
520521
make_driver_executable_if_not(LOCAL_GECKODRIVER)
521522
except Exception as e:
522-
print("\nWarning: Could not make geckodriver"
523-
" executable: %s" % e)
523+
logging.debug("\nWarning: Could not make geckodriver"
524+
" executable: %s" % e)
524525
elif not is_geckodriver_on_path():
525526
if not "".join(sys.argv) == "-c": # Skip if multithreaded
526527
from seleniumbase.console_scripts import sb_install
@@ -563,11 +564,19 @@ def get_local_driver(
563564
ie_options.persistent_hover = True
564565
ie_capabilities = ie_options.to_capabilities()
565566
if LOCAL_IEDRIVER and os.path.exists(LOCAL_IEDRIVER):
566-
make_driver_executable_if_not(LOCAL_IEDRIVER)
567+
try:
568+
make_driver_executable_if_not(LOCAL_IEDRIVER)
569+
except Exception as e:
570+
logging.debug("\nWarning: Could not make iedriver"
571+
" executable: %s" % e)
567572
return webdriver.Ie(capabilities=ie_capabilities)
568573
elif browser_name == constants.Browser.EDGE:
569574
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
570-
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
575+
try:
576+
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
577+
except Exception as e:
578+
logging.debug("\nWarning: Could not make edgedriver"
579+
" executable: %s" % e)
571580
# The new Microsoft Edge browser is based on Chromium
572581
chrome_options = _set_chrome_options(
573582
downloads_path, headless,
@@ -585,7 +594,11 @@ def get_local_driver(
585594
return webdriver.Safari()
586595
elif browser_name == constants.Browser.OPERA:
587596
if LOCAL_OPERADRIVER and os.path.exists(LOCAL_OPERADRIVER):
588-
make_driver_executable_if_not(LOCAL_OPERADRIVER)
597+
try:
598+
make_driver_executable_if_not(LOCAL_OPERADRIVER)
599+
except Exception as e:
600+
logging.debug("\nWarning: Could not make operadriver"
601+
" executable: %s" % e)
589602
return webdriver.Opera()
590603
elif browser_name == constants.Browser.PHANTOM_JS:
591604
with warnings.catch_warnings():
@@ -601,7 +614,11 @@ def get_local_driver(
601614
extension_zip, extension_dir, servername, mobile_emulator,
602615
device_width, device_height, device_pixel_ratio)
603616
if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER):
604-
make_driver_executable_if_not(LOCAL_CHROMEDRIVER)
617+
try:
618+
make_driver_executable_if_not(LOCAL_CHROMEDRIVER)
619+
except Exception as e:
620+
logging.debug("\nWarning: Could not make chromedriver"
621+
" executable: %s" % e)
605622
elif not is_chromedriver_on_path():
606623
if not "".join(sys.argv) == "-c": # Skip if multithreaded
607624
from seleniumbase.console_scripts import sb_install
@@ -614,7 +631,11 @@ def get_local_driver(
614631
if headless:
615632
raise Exception(e)
616633
if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER):
617-
make_driver_executable_if_not(LOCAL_CHROMEDRIVER)
634+
try:
635+
make_driver_executable_if_not(LOCAL_CHROMEDRIVER)
636+
except Exception as e:
637+
logging.debug("\nWarning: Could not make chromedriver"
638+
" executable: %s" % e)
618639
return webdriver.Chrome()
619640
else:
620641
raise Exception(

seleniumbase/core/tour_helper.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
EXPORTED_TOURS_FOLDER = "tours_exported"
1616

1717

18-
def raise_unable_to_load_jquery_exception(driver):
19-
""" The most-likely reason for jQuery not loading on web pages. """
20-
raise Exception(
21-
'''Unable to load jQuery on "%s" due to a possible violation '''
22-
'''of the website's Content Security Policy '''
23-
'''directive. ''' % driver.current_url)
24-
25-
2618
def activate_bootstrap(driver):
2719
""" Allows you to use Bootstrap Tours with SeleniumBase
2820
http://bootstraptour.com/
@@ -51,7 +43,7 @@ def activate_bootstrap(driver):
5143
return
5244
except Exception:
5345
time.sleep(0.15)
54-
raise_unable_to_load_jquery_exception(driver)
46+
js_utils.raise_unable_to_load_jquery_exception(driver)
5547

5648

5749
def is_bootstrap_activated(driver):
@@ -96,7 +88,7 @@ def activate_hopscotch(driver):
9688
return
9789
except Exception:
9890
time.sleep(0.15)
99-
raise_unable_to_load_jquery_exception(driver)
91+
js_utils.raise_unable_to_load_jquery_exception(driver)
10092

10193

10294
def is_hopscotch_activated(driver):
@@ -139,7 +131,7 @@ def activate_introjs(driver):
139131
return
140132
except Exception:
141133
time.sleep(0.15)
142-
raise_unable_to_load_jquery_exception(driver)
134+
js_utils.raise_unable_to_load_jquery_exception(driver)
143135

144136

145137
def is_introjs_activated(driver):
@@ -199,7 +191,7 @@ def activate_shepherd(driver):
199191
return
200192
except Exception:
201193
time.sleep(0.15)
202-
raise_unable_to_load_jquery_exception(driver)
194+
js_utils.raise_unable_to_load_jquery_exception(driver)
203195

204196

205197
def is_shepherd_activated(driver):

seleniumbase/fixtures/js_utils.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ def wait_for_jquery_active(driver, timeout=None):
107107
time.sleep(0.1)
108108

109109

110+
def raise_unable_to_load_jquery_exception(driver):
111+
""" The most-likely reason for jQuery not loading on web pages. """
112+
raise Exception(
113+
'''Unable to load jQuery on "%s" due to a possible violation '''
114+
'''of the website's Content Security Policy directive. '''
115+
'''To override this policy, add "--disable-csp" on the '''
116+
'''command-line when running your tests.''' % driver.current_url)
117+
118+
110119
def activate_jquery(driver):
111120
""" If "jQuery is not defined", use this method to activate it for use.
112121
This happens because jQuery is not always defined on web sites. """
@@ -132,11 +141,7 @@ def activate_jquery(driver):
132141
except Exception:
133142
time.sleep(0.1)
134143
# Since jQuery still isn't activating, give up and raise an exception
135-
raise Exception(
136-
'''Unable to load jQuery on "%s" due to a possible violation '''
137-
'''of the website's Content Security Policy directive. '''
138-
'''To override this policy, add "--disable_csp" on the '''
139-
'''command-line when running your tests.''' % driver.current_url)
144+
raise_unable_to_load_jquery_exception(driver)
140145

141146

142147
def are_quotes_escaped(string):
@@ -302,14 +307,14 @@ def add_css_link(driver, css_link):
302307
def add_js_link(driver, js_link):
303308
script_to_add_js = (
304309
"""function injectJS(link) {
305-
var head = document.getElementsByTagName("head")[0];
310+
var body = document.getElementsByTagName("body")[0];
306311
var script = document.createElement("script");
307312
script.src = link;
308313
script.defer;
309314
script.type="text/javascript";
310315
script.crossorigin = "anonymous";
311316
script.onload = function() { null };
312-
head.appendChild(script);
317+
body.appendChild(script);
313318
}
314319
injectJS("%s");""")
315320
js_link = escape_quotes_if_needed(js_link)
@@ -336,12 +341,12 @@ def add_js_code_from_link(driver, js_link):
336341
js_link = "http:" + js_link
337342
js_code = requests.get(js_link).text
338343
add_js_code_script = (
339-
'''var h = document.getElementsByTagName('head').item(0);'''
340-
'''var s = document.createElement("script");'''
341-
'''s.type = "text/javascript";'''
342-
'''s.onload = function() { null };'''
343-
'''s.appendChild(document.createTextNode("%s"));'''
344-
'''h.appendChild(s);''')
344+
'''var body = document.getElementsByTagName('body').item(0);'''
345+
'''var script = document.createElement("script");'''
346+
'''script.type = "text/javascript";'''
347+
'''script.onload = function() { null };'''
348+
'''script.appendChild(document.createTextNode("%s"));'''
349+
'''body.appendChild(script);''')
345350
js_code = js_code.replace('\n', '')
346351
js_code = escape_quotes_if_needed(js_code)
347352
driver.execute_script(add_js_code_script % js_code)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
setup(
4747
name='seleniumbase',
48-
version='1.34.7',
48+
version='1.34.8',
4949
description='Fast, Easy, and Reliable Browser Automation & Testing.',
5050
long_description=long_description,
5151
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)