How to use add_js_code method in SeleniumBase

Best Python code snippet using SeleniumBase

vlam_interpreter.py

Source:vlam_interpreter.py Github

copy

Full Screen

...147 # first we need to make sure that the required javacript code is in the page:148 if interp_kind == "borg" or interp_kind == "interpreter":149 if not page.includes("BorgInterpreter_included"):150 page.add_include("BorgInterpreter_included")151 page.add_js_code(BorgInterpreter_js)152 page.add_js_code('init_BorgInterpreter("%s");' % uid)153 elif interp_kind == "isolated" or interp_kind == "Human":154 if not page.includes("SingleInterpreter_included"):155 page.add_include("SingleInterpreter_included")156 page.add_js_code(SingleInterpreter_js)157 page.add_js_code('init_SingleInterpreter("%s");' % uid)158 elif interp_kind == "parrot":159 if not page.includes("parrot_included"):160 page.add_include("parrot_included")161 page.add_js_code(parrot_js)162 page.add_js_code('init_parrotInterpreter("%s");' % uid)163 elif interp_kind == "Parrots":164 if not page.includes("Parrots_included"):165 page.add_include("Parrots_included")166 page.add_js_code(Parrots_js)167 page.add_js_code('init_ParrotsInterpreter("%s");' % uid)168 elif interp_kind == "TypeInfoConsole":169 if not page.includes("TypeInfoConsole_included"):170 page.add_include("TypeInfoConsole_included")171 page.add_js_code(TypeInfoConsole_js)172 page.add_js_code('init_TypeInfoConsole("%s");' % uid)173# Unfortunately, IPython interferes with Crunchy; I'm commenting it out, keeping it in as a reference.174## else:175## if not page.includes("IPythonInterpreter_included"):176## page.add_include("IPythonInterpreter_included")177## page.add_js_code(IPythonInterpreter_js)178## page.add_js_code('init_IPythonInterpreter("%s");' % uid)179def borg_javascript(page):180 '''create string needed to initialize a Borg interpreter using javascript'''181 return r"""182 function init_BorgInterpreter(uid){183 code = "import src.interpreter\nborg=src.interpreter.BorgConsole(group='%s',username='%s')";184 code += "\nborg.interact()\n";185 var j = new XMLHttpRequest();186 j.open("POST", "/exec%s?uid="+uid, false);187 j.send(code);188 };189 """ % (page.pageid, page.username, plugin['session_random_id'])190def single_javascript(page):191 '''create string needed to initialize an Isolated (single) interpreter192 using javascript'''...

Full Screen

Full Screen

add_js_tools.py

Source:add_js_tools.py Github

copy

Full Screen

1# -*- coding: utf8 -*-2import os3base_dir = os.path.dirname(os.path.abspath(__file__))4#输入需要修改的文件夹名。5doc_name = 'django-docs'6doc_path = os.path.join(base_dir, doc_name)7#输入需要插入的js代码。8js_doc_name = 'append_js.html'9js_doc_path = os.path.join(base_dir, js_doc_name)10walk_path_info = os.walk(doc_path)11html_file_num = 012html_file_list = []13add_js_code = ''14for file_info in walk_path_info:15 if len(file_info[2]) > 0:16 for file_name in file_info[2]:17 file_name_structure = file_name.split('.')18 if 'html' in file_name_structure:19 html_file_num += 120 html_file = []21 html_file.append(file_info[0])22 html_file.append(file_name)23 html_file_list.append(html_file)24 else:25 continue26 else:27 continue28print "html_file_num:", html_file_num29print "html_file_list_len:", len(html_file_list)30file_js_obj = open(js_doc_path, 'r')31add_js_code = file_js_obj.read()32file_js_obj.close()33#print 'add_js_code:', add_js_code34html_file_has_head_num = 035for html_file in html_file_list:36 html_file_path = os.path.join(html_file[0], html_file[1])37 file_obj = open(html_file_path, 'r')38 file_content = file_obj.read()39 #print file_content40 has_head = False41 '''42 文件对象提供了三个“读”方法: .read()、.readline() 和 .readlines()。43 .readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象44 .read() 一样。.readlines() 自动将文件内容分析成一个行的列表,该列45 表可以由 Python 的 for ... in ... 结构进行处理。另一方面,.readline()46 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以47 一次读取整个文件时,才应该使用 .readline()。48 因为file_obj.read(),已把file_obj转化成字符串,所以下面的循环失效。49 下面的方法是从py2.5之后才引进的,效率远高于readline和readlines。50 for i in file_obj:51 if '</head>' in i:52 has_head = True53 break54 else:55 continue56 '''57 if '</head>' in file_content:58 has_head = True59 if has_head:60 html_file_has_head_num += 161 position = file_content.find('</head>')62 file_new_content = file_content[:position] + '\n' + add_js_code + '\n' +file_content[position:]63 add_file_obj = open(html_file_path, 'w')64 add_file_obj.write(file_new_content)65 add_file_obj.close()66 else:67 continue68 file_obj.close()69print 'html_file_has_head_num:', html_file_has_head_num70had_add_js_file_num = 071for html_file in html_file_list:72 html_file_path = os.path.join(html_file[0], html_file[1])73 file_obj = open(html_file_path, 'r')74 file_content = file_obj.read()75 if add_js_code in file_content:76 had_add_js_file_num += 177 file_obj.close()...

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