How to use add_js_link method in SeleniumBase

Best Python code snippet using SeleniumBase

js_utils.py

Source:js_utils.py Github

copy

Full Screen

...284 }285 injectCSS("%s");""")286 css_link = escape_quotes_if_needed(css_link)287 driver.execute_script(script_to_add_css % css_link)288def add_js_link(driver, js_link):289 script_to_add_js = (290 """function injectJS(link) {291 var body = document.getElementsByTagName("body")[0];292 var script = document.createElement("script");293 script.src = link;294 script.defer;295 script.type="text/javascript";296 script.crossorigin = "anonymous";297 script.onload = function() { null };298 body.appendChild(script);299 }300 injectJS("%s");""")301 js_link = escape_quotes_if_needed(js_link)302 driver.execute_script(script_to_add_js % js_link)303def add_css_style(driver, css_style):304 add_css_style_script = (305 """function injectStyle(css) {306 var head = document.getElementsByTagName("head")[0];307 var style = document.createElement("style");308 style.type = "text/css";309 style.appendChild(document.createTextNode(css));310 head.appendChild(style);311 }312 injectStyle("%s");""")313 css_style = css_style.replace('\n', '')314 css_style = escape_quotes_if_needed(css_style)315 driver.execute_script(add_css_style_script % css_style)316def add_js_code_from_link(driver, js_link):317 if js_link.startswith("//"):318 js_link = "http:" + js_link319 js_code = requests.get(js_link).text320 add_js_code_script = (321 '''var body = document.getElementsByTagName('body').item(0);'''322 '''var script = document.createElement("script");'''323 '''script.type = "text/javascript";'''324 '''script.onload = function() { null };'''325 '''script.appendChild(document.createTextNode("%s"));'''326 '''body.appendChild(script);''')327 js_code = js_code.replace('\n', ' ')328 js_code = escape_quotes_if_needed(js_code)329 driver.execute_script(add_js_code_script % js_code)330def add_js_code(driver, js_code):331 add_js_code_script = (332 '''var body = document.getElementsByTagName('body').item(0);'''333 '''var script = document.createElement("script");'''334 '''script.type = "text/javascript";'''335 '''script.onload = function() { null };'''336 '''script.appendChild(document.createTextNode("%s"));'''337 '''body.appendChild(script);''')338 js_code = js_code.replace('\n', ' ')339 js_code = escape_quotes_if_needed(js_code)340 driver.execute_script(add_js_code_script % js_code)341def add_meta_tag(driver, http_equiv=None, content=None):342 if http_equiv is None:343 http_equiv = "Content-Security-Policy"344 if content is None:345 content = ("default-src *; style-src 'self' 'unsafe-inline'; "346 "script-src: 'self' 'unsafe-inline' 'unsafe-eval'")347 script_to_add_meta = (348 """function injectMeta() {349 var meta = document.createElement('meta');350 meta.httpEquiv = "%s";351 meta.content = "%s";352 document.getElementsByTagName('head')[0].appendChild(meta);353 }354 injectMeta();""" % (http_equiv, content))355 driver.execute_script(script_to_add_meta)356def is_jquery_confirm_activated(driver):357 try:358 driver.execute_script("jconfirm") # Fails if jq_confirm is not defined359 return True360 except Exception:361 return False362def activate_jquery_confirm(driver):363 jquery_js = constants.JQuery.MIN_JS364 jq_confirm_css = constants.JqueryConfirm.MIN_CSS365 jq_confirm_js = constants.JqueryConfirm.MIN_JS366 if not is_jquery_activated(driver):367 add_js_link(driver, jquery_js)368 wait_for_jquery_active(driver, timeout=0.9)369 add_css_link(driver, jq_confirm_css)370 add_js_link(driver, jq_confirm_js)371 for x in range(15):372 # jQuery-Confirm needs a small amount of time to load & activate.373 try:374 driver.execute_script("jconfirm")375 wait_for_ready_state_complete(driver)376 wait_for_angularjs(driver)377 return378 except Exception:379 time.sleep(0.1)380def activate_html_inspector(driver):381 jquery_js = constants.JQuery.MIN_JS382 html_inspector_js = constants.HtmlInspector.MIN_JS383 if is_html_inspector_activated(driver):384 return385 if not is_jquery_activated(driver):386 add_js_link(driver, jquery_js)387 wait_for_ready_state_complete(driver)388 wait_for_angularjs(driver)389 wait_for_jquery_active(driver, timeout=1.5)390 add_js_link(driver, html_inspector_js)391 wait_for_ready_state_complete(driver)392 wait_for_angularjs(driver)393 for x in range(15):394 # HTML-Inspector needs a small amount of time to load & activate.395 try:396 driver.execute_script("HTMLInspector")397 wait_for_ready_state_complete(driver)398 wait_for_angularjs(driver)399 return400 except Exception:401 time.sleep(0.1)402 wait_for_ready_state_complete(driver)403 wait_for_angularjs(driver)404def activate_messenger(driver):405 jquery_js = constants.JQuery.MIN_JS406 messenger_css = constants.Messenger.MIN_CSS407 messenger_js = constants.Messenger.MIN_JS408 msgr_theme_flat_js = constants.Messenger.THEME_FLAT_JS409 msgr_theme_future_js = constants.Messenger.THEME_FUTURE_JS410 msgr_theme_flat_css = constants.Messenger.THEME_FLAT_CSS411 msgr_theme_future_css = constants.Messenger.THEME_FUTURE_CSS412 msgr_theme_block_css = constants.Messenger.THEME_BLOCK_CSS413 msgr_theme_air_css = constants.Messenger.THEME_AIR_CSS414 msgr_theme_ice_css = constants.Messenger.THEME_ICE_CSS415 spinner_css = constants.Messenger.SPINNER_CSS416 underscore_js = constants.Underscore.MIN_JS417 backbone_js = constants.Backbone.MIN_JS418 msg_style = ("Messenger.options = {'maxMessages': 8, "419 "extraClasses: 'messenger-fixed "420 "messenger-on-bottom messenger-on-right', "421 "theme: 'future'}")422 add_js_link(driver, jquery_js)423 wait_for_jquery_active(driver, timeout=0.2)424 add_css_link(driver, messenger_css)425 add_css_link(driver, msgr_theme_flat_css)426 add_css_link(driver, msgr_theme_future_css)427 add_css_link(driver, msgr_theme_block_css)428 add_css_link(driver, msgr_theme_air_css)429 add_css_link(driver, msgr_theme_ice_css)430 add_js_link(driver, underscore_js)431 add_js_link(driver, backbone_js)432 add_css_link(driver, spinner_css)433 add_js_link(driver, messenger_js)434 add_js_link(driver, msgr_theme_flat_js)435 add_js_link(driver, msgr_theme_future_js)436 from seleniumbase.core import style_sheet437 add_css_style(driver, style_sheet.messenger_style)438 for x in range(int(settings.MINI_TIMEOUT * 10.0)):439 # Messenger needs a small amount of time to load & activate.440 try:441 driver.execute_script(msg_style)442 wait_for_ready_state_complete(driver)443 wait_for_angularjs(driver)444 return445 except Exception:446 time.sleep(0.1)447def set_messenger_theme(driver, theme="default", location="default",448 max_messages="default"):449 if theme == "default":...

Full Screen

Full Screen

helpers.py

Source:helpers.py Github

copy

Full Screen

1from haplugin.formskit.helpers import FormWidget as BaseFormWidget2class FormWidget(BaseFormWidget):3 konwentor_prefix = 'konwentor.forms:templates'4 def combobox(self, name, disabled=False, autofocus=False):5 self.request.add_js_link('/js/combobox.js')6 self.request.add_js(7 '''$(document).ready(function() {8 $("#%s").combobox();9 });''' % (self.get_id(name)))10 return self._input('combobox', name, disabled, autofocus)11 def text_with_add(12 self,13 name,14 disabled=False,15 autofocus=False,16 button_label='Add',17 ):18 self.request.add_js_link('/js/add_button.js')19 self.request.add_js('''20 $(document).ready(function() {21 $(".addroom").addroom();22 });23 ''')24 return self._input(25 'text_with_add',26 name,27 disabled,28 autofocus,29 prefix=self.konwentor_prefix,30 button_label=button_label,31 )32 def text_with_add_from_list(33 self,34 name,35 disabled=False,36 autofocus=False,37 button_label='Add',38 elements=[],39 ):40 self.request.add_js_link('/js/add_button.js')41 self.request.add_js('''42 $(document).ready(function() {43 $(".add_from_list").add_from_list();44 $(".remove_button").remove_button();45 });46 ''')47 return self._input(48 'text_with_add_from_list',49 name,50 disabled,51 autofocus,52 prefix=self.konwentor_prefix,53 button_label=button_label,54 elements=elements,...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful