Skip to content

Commit 7fb461d

Browse files
committed
Selenium test fixture to open a notebook with prefilled content
This makes several tests shorter and (hopefully) more efficient
1 parent 9640e1f commit 7fb461d

16 files changed

+157
-192
lines changed

notebook/tests/selenium/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import json
2+
import nbformat
3+
from nbformat.v4 import new_notebook, new_code_cell
24
import os
35
import pytest
46
import requests
57
from subprocess import Popen
68
import sys
9+
from tempfile import mkstemp
710
from testpath.tempdir import TemporaryDirectory
811
import time
912
from urllib.parse import urljoin
@@ -121,3 +124,20 @@ def notebook(authenticated_browser):
121124
tree_wh = authenticated_browser.current_window_handle
122125
yield Notebook.new_notebook(authenticated_browser)
123126
authenticated_browser.switch_to.window(tree_wh)
127+
128+
@pytest.fixture
129+
def prefill_notebook(selenium_driver, notebook_server):
130+
def inner(cells):
131+
cells = [new_code_cell(c) if isinstance(c, str) else c
132+
for c in cells]
133+
nb = new_notebook(cells=cells)
134+
fd, path = mkstemp(dir=notebook_server['nbdir'], suffix='.ipynb')
135+
with open(fd, 'w', encoding='utf-8') as f:
136+
nbformat.write(nb, f)
137+
fname = os.path.basename(path)
138+
selenium_driver.get(
139+
"{url}notebooks/{}?token={token}".format(fname, **notebook_server)
140+
)
141+
return Notebook(selenium_driver)
142+
143+
return inner

notebook/tests/selenium/test_buffering.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@ def wait_for_kernel_ready(notebook):
1313
wait_for_selector(notebook.browser, ".kernel_idle_icon")
1414

1515

16-
def kernels_buffer_without_conn(notebook):
16+
def test_kernels_buffer_without_conn(prefill_notebook):
1717
"""Test that execution request made while disconnected is buffered."""
18-
# Assert that cell executed while kernel is disconnected still
19-
# executes when reconnected
18+
notebook = prefill_notebook(["print(1 + 2)"])
19+
2020
wait_for_kernel_ready(notebook)
2121
notebook.browser.execute_script("IPython.notebook.kernel.stop_channels();")
22-
notebook.edit_cell(index=0, content="print(1 + 2)")
2322
notebook.execute_cell(0)
2423
notebook.browser.execute_script("IPython.notebook.kernel.reconnect();")
2524
wait_for_kernel_ready(notebook)
25+
2626
assert wait_for_cell_text_output(notebook, 0) == "3"
27-
notebook.delete_cell(0)
2827

2928

30-
def buffered_cells_execute_in_order(notebook):
29+
def test_buffered_cells_execute_in_order(prefill_notebook):
3130
"""Test that buffered requests execute in order."""
32-
notebook.append('k=1', 'k+=1', 'k*=3', 'print(k)')
31+
notebook = prefill_notebook(['', 'k=1', 'k+=1', 'k*=3', 'print(k)'])
3332

3433
# Repeated execution of cell queued up in the kernel executes
3534
# each execution request in order.
@@ -49,8 +48,3 @@ def buffered_cells_execute_in_order(notebook):
4948

5049
# Check that current value of k is 7
5150
assert wait_for_cell_text_output(notebook, 4) == "7"
52-
53-
54-
def test_buffering(notebook):
55-
kernels_buffer_without_conn(notebook)
56-
buffered_cells_execute_in_order(notebook)

notebook/tests/selenium/test_clipboard_multiselect.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
from selenium.webdriver.common.keys import Keys
33
from .utils import wait_for_selector, wait_for_xpath
44

5-
def test_clipboard_multiselect(notebook):
6-
7-
# Cell contents setup
8-
values = ['1', '2', '3', '4', '5a', '6b', '7c', '8d']
9-
notebook.extend(values)
5+
def test_clipboard_multiselect(prefill_notebook):
6+
notebook = prefill_notebook(['', '1', '2', '3', '4', '5a', '6b', '7c', '8d'])
107

118
assert notebook.get_cells_contents() == ['', '1', '2', '3', '4', '5a', '6b', '7c', '8d']
129

notebook/tests/selenium/test_deletecell.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ def remove_all_cells(notebook):
77
for i in range(len(notebook.cells)):
88
notebook.delete_cell(0)
99

10-
def test_delete_cells(notebook):
11-
a = 'print("a")'
12-
b = 'print("b")'
13-
c = 'print("c")'
10+
INITIAL_CELLS = ['print("a")', 'print("b")', 'print("c")']
11+
12+
def test_delete_cells(prefill_notebook):
13+
a, b, c = INITIAL_CELLS
14+
notebook = prefill_notebook(INITIAL_CELLS)
1415

15-
notebook.edit_cell(index=0, content=a)
16-
notebook.append(b, c)
17-
notebook.to_command_mode()
18-
1916
# Validate initial state
2017
assert notebook.get_cells_contents() == [a, b, c]
2118
for cell in range(0, 3):

notebook/tests/selenium/test_dualmode_clipboard.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
"""Test"""
22
from .utils import shift, validate_dualmode_state
33

4-
def test_dualmode_clipboard(notebook):
5-
a = 'print("a")'
6-
notebook.append(a)
7-
notebook.execute_cell(1)
4+
INITIAL_CELLS = ['', 'print("a")', 'print("b")', 'print("c")']
85

9-
b = 'print("b")'
10-
notebook.append(b)
11-
notebook.execute_cell(2)
12-
13-
c = 'print("c")'
14-
notebook.append(c)
15-
notebook.execute_cell(3)
6+
def test_dualmode_clipboard(prefill_notebook):
7+
notebook = prefill_notebook(INITIAL_CELLS)
8+
_, a, b, c = INITIAL_CELLS
9+
for i in range(1, 4):
10+
notebook.execute_cell(i)
1611

1712
#Copy/past/cut
1813
num_cells = len(notebook.cells)
@@ -56,4 +51,4 @@ def test_dualmode_clipboard(notebook):
5651
shift(notebook.browser, 'v') #Paste
5752
validate_dualmode_state(notebook, 'command', 0)
5853
assert notebook.get_cell_contents(0) == c #Cell 0 has the copied contents
59-
assert len(notebook.cells) == num_cells+3 #A cell was added
54+
assert len(notebook.cells) == num_cells+3 #A cell was added

notebook/tests/selenium/test_dualmode_execute.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44

55
from .utils import shift, cmdtrl, alt, validate_dualmode_state
66

7+
INITIAL_CELLS = ['', 'print("a")', 'print("b")', 'print("c")']
78

8-
def test_dualmode_execute(notebook):
9-
a = 'print("a")'
10-
notebook.append(a)
11-
notebook.execute_cell(1)
12-
13-
b = 'print("b")'
14-
notebook.append(b)
15-
notebook.execute_cell(2)
16-
17-
c = 'print("c")'
18-
notebook.append(c)
19-
notebook.execute_cell(3)
9+
def test_dualmode_execute(prefill_notebook):
10+
notebook = prefill_notebook(INITIAL_CELLS)
11+
for i in range(1, 4):
12+
notebook.execute_cell(i)
2013

2114
#shift-enter
2215
#last cell in notebook

notebook/tests/selenium/test_dualmode_insertcell.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
from selenium.webdriver.common.keys import Keys
22
from .utils import shift
33

4-
def test_insert_cell(notebook):
5-
a = "print('a')"
6-
b = "print('b')"
7-
c = "print('c')"
4+
INITIAL_CELLS = ['print("a")', 'print("b")', 'print("c")']
85

9-
notebook.edit_cell(index=0, content=a)
10-
notebook.append(b, c)
11-
notebook.to_command_mode()
12-
13-
assert notebook.get_cells_contents() == [a, b, c]
6+
def test_insert_cell(prefill_notebook):
7+
notebook = prefill_notebook(INITIAL_CELLS)
148

159
notebook.to_command_mode()
1610
notebook.focus_cell(2)
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
import pytest
1+
INITIAL_CELLS = ["hello", "hellohello", "abc", "ello"]
22

3-
4-
def test_find_and_replace(notebook):
3+
def test_find_and_replace(prefill_notebook):
54
""" test find and replace on all the cells """
6-
cell_0, cell_1, cell_2, cell_3 = "hello", "hellohello", "abc", "ello"
7-
8-
find_str = "ello" # string to replace
9-
replace_str = "foo" # string to replace to
5+
notebook = prefill_notebook(INITIAL_CELLS)
106

11-
# set the contents of the cells
12-
notebook.add_cell(index=0, content=cell_0);
13-
notebook.add_cell(index=1, content=cell_1);
14-
notebook.add_cell(index=2, content=cell_2);
15-
notebook.add_cell(index=3, content=cell_3);
7+
find_str = "ello"
8+
replace_str = "foo"
169

1710
# replace the strings
1811
notebook.find_and_replace(index=0, find_txt=find_str, replace_txt=replace_str)
1912

2013
# check content of the cells
21-
assert notebook.get_cell_contents(0) == cell_0.replace(find_str, replace_str)
22-
assert notebook.get_cell_contents(1) == cell_1.replace(find_str, replace_str)
23-
assert notebook.get_cell_contents(2) == cell_2.replace(find_str, replace_str)
24-
assert notebook.get_cell_contents(3) == cell_3.replace(find_str, replace_str)
14+
assert notebook.get_cells_contents() == [
15+
s.replace(find_str, replace_str) for s in INITIAL_CELLS
16+
]
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from nbformat.v4 import new_markdown_cell
12

23
def get_rendered_contents(nb):
34
cl = ["text_cell", "render"]
@@ -9,21 +10,15 @@ def get_rendered_contents(nb):
910
if x is not None]
1011

1112

12-
def test_markdown_cell(notebook):
13-
nb = notebook
14-
cell_text = ["# Foo",
15-
'**Bar**',
16-
'*Baz*',
17-
'```\nx = 1\n```',
18-
'```aaaa\nx = 1\n```',
19-
]
20-
expected_contents = ['<h1 id="Foo">Foo<a class="anchor-link" href="#Foo">¶</a></h1>',
21-
'<p><strong>Bar</strong></p>',
22-
'<p><em>Baz</em></p>',
23-
'<pre><code>x = 1</code></pre>',
24-
'<pre><code class="cm-s-ipython language-aaaa">x = 1</code></pre>'
25-
]
26-
nb.append(*cell_text, cell_type="markdown")
27-
nb.run_all()
28-
rendered_contents = get_rendered_contents(nb)
29-
assert rendered_contents == expected_contents
13+
def test_markdown_cell(prefill_notebook):
14+
nb = prefill_notebook([new_markdown_cell(md) for md in [
15+
'# Foo', '**Bar**', '*Baz*', '```\nx = 1\n```', '```aaaa\nx = 1\n```',
16+
]])
17+
18+
assert get_rendered_contents(nb) == [
19+
'<h1 id="Foo">Foo<a class="anchor-link" href="#Foo">¶</a></h1>',
20+
'<p><strong>Bar</strong></p>',
21+
'<p><em>Baz</em></p>',
22+
'<pre><code>x = 1</code></pre>',
23+
'<pre><code class="cm-s-ipython language-aaaa">x = 1</code></pre>',
24+
]

notebook/tests/selenium/test_merge_cells.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""Tests the merge cell api."""
22

3-
def test_merge_cells(notebook):
4-
# Add cells to notebook
5-
a = "foo = 5"
6-
b = "bar = 10"
7-
c = "baz = 15"
8-
d = "print(foo)"
9-
e = "print(bar)"
10-
f = "print(baz)"
11-
notebook.edit_cell(index=0, content=a)
12-
notebook.append(b, c, d, e, f)
3+
INITIAL_CELLS = [
4+
"foo = 5",
5+
"bar = 10",
6+
"baz = 15",
7+
"print(foo)",
8+
"print(bar)",
9+
"print(baz)",
10+
]
11+
12+
def test_merge_cells(prefill_notebook):
13+
notebook = prefill_notebook(INITIAL_CELLS)
14+
a, b, c, d, e, f = INITIAL_CELLS
1315

1416
# Before merging, there are 6 separate cells
1517
assert notebook.get_cells_contents() == [a, b, c, d, e, f]

0 commit comments

Comments
 (0)