Skip to content

Commit c9378c7

Browse files
authored
Merge pull request #4146 from askerry/selenium_display_isolation
Migrate display isolation test to selenium
2 parents 099383e + 3ccc4ed commit c9378c7

File tree

3 files changed

+93
-91
lines changed

3 files changed

+93
-91
lines changed

notebook/tests/notebook/isolated_svg.js

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Test display isolation.
2+
3+
An object whose metadata contains an "isolated" tag must be isolated
4+
from the rest of the document.
5+
"""
6+
7+
8+
def test_display_isolation(notebook):
9+
import_ln = "from IPython.core.display import HTML, SVG, display, display_svg"
10+
notebook.edit_cell(index=0, content=import_ln)
11+
notebook.execute_cell(notebook.current_cell)
12+
try:
13+
isolated_html(notebook)
14+
isolated_svg(notebook)
15+
finally:
16+
# Ensure we switch from iframe back to default content even if test fails
17+
notebook.browser.switch_to.default_content()
18+
19+
20+
def isolated_html(notebook):
21+
"""Test HTML display isolation.
22+
23+
HTML styling rendered without isolation will affect the whole
24+
document, whereas styling applied with isolation will affect only
25+
the local display object.
26+
"""
27+
red = 'rgb(255, 0, 0)'
28+
blue = 'rgb(0, 0, 255)'
29+
test_str = "<div id='test'>Should be red from non-isolation</div>"
30+
notebook.add_and_execute_cell(content="display(HTML(%r))" % test_str)
31+
non_isolated = (
32+
"<style>div{color:%s;}</style>" % red +
33+
"<div id='non-isolated'>Should be red</div>")
34+
display_ni = "display(HTML(%r), metadata={'isolated':False})" % (
35+
non_isolated)
36+
notebook.add_and_execute_cell(content=display_ni)
37+
isolated = (
38+
"<style>div{color:%s;}</style>" % blue +
39+
"<div id='isolated'>Should be blue</div>")
40+
display_i = "display(HTML(%r), metadata={'isolated':True})" % (
41+
isolated)
42+
notebook.add_and_execute_cell(content=display_i)
43+
44+
# The non-isolated div will be in the body
45+
non_isolated_div = notebook.body.find_element_by_id("non-isolated")
46+
assert non_isolated_div.value_of_css_property("color") == red
47+
48+
# The non-isolated styling will have affected the output of other cells
49+
test_div = notebook.body.find_element_by_id("test")
50+
assert test_div.value_of_css_property("color") == red
51+
52+
# The isolated div will be in an iframe, only that element will be blue
53+
iframe = notebook.body.find_element_by_tag_name("iframe")
54+
notebook.browser.switch_to.frame(iframe)
55+
isolated_div = notebook.browser.find_element_by_id("isolated")
56+
assert isolated_div.value_of_css_property("color") == blue
57+
notebook.browser.switch_to.default_content()
58+
59+
60+
def isolated_svg(notebook):
61+
"""Test that multiple isolated SVGs have different scopes.
62+
63+
Asserts that there no CSS leaks between two isolated SVGs.
64+
"""
65+
yellow = "rgb(255, 255, 0)"
66+
black = "rgb(0, 0, 0)"
67+
svg_1_str = """s1 = '''<svg width="1cm" height="1cm" viewBox="0 0 1000 500"><defs><style>rect {fill:%s;}; </style></defs><rect id="r1" x="200" y="100" width="600" height="300" /></svg>'''""" % yellow
68+
svg_2_str = """s2 = '''<svg width="1cm" height="1cm" viewBox="0 0 1000 500"><rect id="r2" x="200" y="100" width="600" height="300" /></svg>'''"""
69+
70+
notebook.add_and_execute_cell(content=svg_1_str)
71+
notebook.add_and_execute_cell(content=svg_2_str)
72+
notebook.add_and_execute_cell(
73+
content="display_svg(SVG(s1), metadata=dict(isolated=True))")
74+
notebook.add_and_execute_cell(
75+
content="display_svg(SVG(s2), metadata=dict(isolated=True))")
76+
iframes = notebook.body.find_elements_by_tag_name("iframe")
77+
78+
# The first rectangle will be red
79+
notebook.browser.switch_to.frame(iframes[1])
80+
isolated_svg_1 = notebook.browser.find_element_by_id('r1')
81+
assert isolated_svg_1.value_of_css_property("fill") == yellow
82+
notebook.browser.switch_to.default_content()
83+
84+
# The second rectangle will be black
85+
notebook.browser.switch_to.frame(iframes[2])
86+
isolated_svg_2 = notebook.browser.find_element_by_id('r2')
87+
assert isolated_svg_2.value_of_css_property("fill") == black
88+

notebook/tests/selenium/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ def add_cell(self, index=-1, cell_type="code", content=""):
200200
if cell_type != 'code':
201201
self.convert_cell_type(index=new_index, cell_type=cell_type)
202202

203+
def add_and_execute_cell(self, index=-1, cell_type="code", content=""):
204+
self.add_cell(index=index, cell_type=cell_type, content=content)
205+
self.execute_cell(index)
206+
203207
def delete_cell(self, index):
204208
self.focus_cell(index)
205209
self.to_command_mode()
@@ -270,7 +274,7 @@ def new_window(browser, selector=None):
270274
yield
271275
new_window_handle = next(window for window in browser.window_handles
272276
if window not in initial_window_handles)
273-
browser.switch_to_window(new_window_handle)
277+
browser.switch_to.window(new_window_handle)
274278
if selector is not None:
275279
wait_for_selector(browser, selector)
276280

0 commit comments

Comments
 (0)