Skip to content

Commit 3806c77

Browse files
committed
Add methods for injecting html into a web page
1 parent 998655d commit 3806c77

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

help_docs/method_summary.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ self.select_option_by_value(dropdown_selector, option,
126126
dropdown_by=By.CSS_SELECTOR,
127127
timeout=None)
128128

129+
self.load_html_string(html_string, new_page=True)
130+
131+
self.load_html_file(html_file, new_page=True)
132+
133+
self.open_html_file(html_file)
134+
129135
self.execute_script(script)
130136

131137
self.execute_async_script(script, timeout=None)
@@ -294,6 +300,8 @@ self.add_css_style(css_style)
294300

295301
self.add_js_code_from_link(js_link)
296302

303+
self.add_js_code(js_code)
304+
297305
self.add_meta_tag(http_equiv=None, content=None)
298306

299307
########

seleniumbase/fixtures/base_case.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,88 @@ def select_option_by_value(self, dropdown_selector, option,
13721372
dropdown_by=dropdown_by, option_by="value",
13731373
timeout=timeout)
13741374

1375+
def load_html_string(self, html_string, new_page=True):
1376+
""" Loads an HTML string into the web browser.
1377+
If new_page==True, the page will switch to: "data:text/html,"
1378+
If new_page==False, will load HTML into the current page. """
1379+
1380+
soup = self.get_beautiful_soup(html_string)
1381+
scripts = soup.findAll("script")
1382+
for script in scripts:
1383+
html_string = html_string.replace(str(script), "")
1384+
soup = self.get_beautiful_soup(html_string)
1385+
1386+
found_head = False
1387+
found_body = False
1388+
html_head = None
1389+
html_body = None
1390+
if soup.head and len(str(soup.head)) > 12:
1391+
found_head = True
1392+
html_head = str(soup.head)
1393+
html_head = re.escape(html_head)
1394+
html_head = self.__escape_quotes_if_needed(html_head)
1395+
html_head = html_head.replace('\\ ', ' ')
1396+
if soup.body and len(str(soup.body)) > 12:
1397+
found_body = True
1398+
html_body = str(soup.body)
1399+
html_body = re.escape(html_body)
1400+
html_body = self.__escape_quotes_if_needed(html_body)
1401+
html_body = html_body.replace('\\ ', ' ')
1402+
html_string = re.escape(html_string)
1403+
html_string = self.__escape_quotes_if_needed(html_string)
1404+
html_string = html_string.replace('\\ ', ' ')
1405+
1406+
if new_page:
1407+
self.open("data:text/html,")
1408+
inner_head = '''document.getElementsByTagName("head")[0].innerHTML'''
1409+
inner_body = '''document.getElementsByTagName("body")[0].innerHTML'''
1410+
if not found_body:
1411+
self.execute_script(
1412+
'''%s = \"%s\"''' % (inner_body, html_string))
1413+
elif found_body and not found_head:
1414+
self.execute_script(
1415+
'''%s = \"%s\"''' % (inner_body, html_body))
1416+
elif found_body and found_head:
1417+
self.execute_script(
1418+
'''%s = \"%s\"''' % (inner_head, html_head))
1419+
self.execute_script(
1420+
'''%s = \"%s\"''' % (inner_body, html_body))
1421+
else:
1422+
raise Exception("Logic Error!")
1423+
1424+
for script in scripts:
1425+
js_code = script.string
1426+
js_code_lines = js_code.split('\n')
1427+
new_lines = []
1428+
for line in js_code_lines:
1429+
line = line.strip()
1430+
new_lines.append(line)
1431+
js_code = '\n'.join(new_lines)
1432+
js_utils.add_js_code(self.driver, js_code)
1433+
1434+
def load_html_file(self, html_file, new_page=True):
1435+
""" Loads a local html file into the browser from a relative file path.
1436+
If new_page==True, the page will switch to: "data:text/html,"
1437+
If new_page==False, will load HTML into the current page.
1438+
Local images and other local src content WILL BE IGNORED. """
1439+
if len(html_file) < 6 or not html_file.endswith(".html"):
1440+
raise Exception('Expecting a ".html" file!')
1441+
abs_path = os.path.abspath('.')
1442+
file_path = abs_path + "/%s" % html_file
1443+
f = open(file_path, 'r')
1444+
html_string = f.read().strip()
1445+
f.close()
1446+
self.load_html_string(html_string, new_page)
1447+
1448+
def open_html_file(self, html_file):
1449+
""" Opens a local html file into the browser from a relative file path.
1450+
The URL displayed in the web browser will start with "file://". """
1451+
if len(html_file) < 6 or not html_file.endswith(".html"):
1452+
raise Exception('Expecting a ".html" file!')
1453+
abs_path = os.path.abspath('.')
1454+
file_path = abs_path + "/%s" % html_file
1455+
self.open("file://" + file_path)
1456+
13751457
def execute_script(self, script):
13761458
return self.driver.execute_script(script)
13771459

@@ -2610,6 +2692,9 @@ def add_css_style(self, css_style):
26102692
def add_js_code_from_link(self, js_link):
26112693
js_utils.add_js_code_from_link(self.driver, js_link)
26122694

2695+
def add_js_code(self, js_code):
2696+
js_utils.add_js_code(self.driver, js_code)
2697+
26132698
def add_meta_tag(self, http_equiv=None, content=None):
26142699
js_utils.add_meta_tag(
26152700
self.driver, http_equiv=http_equiv, content=content)

seleniumbase/fixtures/js_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,20 @@ def add_js_code_from_link(driver, js_link):
349349
'''script.onload = function() { null };'''
350350
'''script.appendChild(document.createTextNode("%s"));'''
351351
'''body.appendChild(script);''')
352-
js_code = js_code.replace('\n', '')
352+
js_code = js_code.replace('\n', ' ')
353+
js_code = escape_quotes_if_needed(js_code)
354+
driver.execute_script(add_js_code_script % js_code)
355+
356+
357+
def add_js_code(driver, js_code):
358+
add_js_code_script = (
359+
'''var body = document.getElementsByTagName('body').item(0);'''
360+
'''var script = document.createElement("script");'''
361+
'''script.type = "text/javascript";'''
362+
'''script.onload = function() { null };'''
363+
'''script.appendChild(document.createTextNode("%s"));'''
364+
'''body.appendChild(script);''')
365+
js_code = js_code.replace('\n', ' ')
353366
js_code = escape_quotes_if_needed(js_code)
354367
driver.execute_script(add_js_code_script % js_code)
355368

0 commit comments

Comments
 (0)