Skip to content

Commit 132f273

Browse files
authored
Merge pull request jupyter#4377 from takluyver/selenium-test-save
Convert test of saving with complex name to Selenium
2 parents 8a4cbd0 + bc3a8cb commit 132f273

File tree

4 files changed

+74
-116
lines changed

4 files changed

+74
-116
lines changed

notebook/static/notebook/js/notebook.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,8 @@ define([
27422742
$.proxy(that.save_notebook_success, that, start),
27432743
function (error) {
27442744
that.events.trigger('notebook_save_failed.Notebook', error);
2745+
// This hasn't handled the error, so propagate it up
2746+
return Promise.reject(error);
27452747
}
27462748
);
27472749
};
@@ -2845,6 +2847,7 @@ define([
28452847
this.create_checkpoint();
28462848
this._checkpoint_after_save = false;
28472849
}
2850+
return data;
28482851
};
28492852

28502853
Notebook.prototype.save_notebook_as = function() {
@@ -3309,7 +3312,7 @@ define([
33093312
*/
33103313
Notebook.prototype.save_checkpoint = function () {
33113314
this._checkpoint_after_save = true;
3312-
this.save_notebook(true);
3315+
return this.save_notebook(true);
33133316
};
33143317

33153318
/**

notebook/tests/notebook/save.js

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Test saving a notebook with escaped characters
2+
"""
3+
4+
from urllib.parse import quote
5+
from .utils import wait_for_selector, new_window
6+
7+
promise_js = """
8+
var done = arguments[arguments.length - 1];
9+
%s.then(
10+
data => { done(["success", data]); },
11+
error => { done(["error", error]); }
12+
);
13+
"""
14+
15+
def execute_promise(js, browser):
16+
state, data = browser.execute_async_script(promise_js % js)
17+
if state == 'success':
18+
return data
19+
raise Exception(data)
20+
21+
22+
def test_save(notebook):
23+
# don't use unicode with ambiguous composed/decomposed normalization
24+
# because the filesystem may use a different normalization than literals.
25+
# This causes no actual problems, but will break string comparison.
26+
nbname = "has#hash and space and unicø∂e.ipynb"
27+
escaped_name = quote(nbname)
28+
29+
notebook.edit_cell(index=0, content="s = '??'")
30+
31+
notebook.browser.execute_script("Jupyter.notebook.set_notebook_name(arguments[0])", nbname)
32+
33+
model = execute_promise("Jupyter.notebook.save_notebook()", notebook.browser)
34+
assert model['name'] == nbname
35+
36+
current_name = notebook.browser.execute_script("return Jupyter.notebook.notebook_name")
37+
assert current_name == nbname
38+
39+
current_path = notebook.browser.execute_script("return Jupyter.notebook.notebook_path")
40+
assert current_path == nbname
41+
42+
displayed_name = notebook.browser.find_element_by_id('notebook_name').text
43+
assert displayed_name + '.ipynb' == nbname
44+
45+
execute_promise("Jupyter.notebook.save_checkpoint()", notebook.browser)
46+
47+
checkpoints = notebook.browser.execute_script("return Jupyter.notebook.checkpoints")
48+
assert len(checkpoints) == 1
49+
50+
notebook.browser.find_element_by_css_selector('#ipython_notebook a').click()
51+
hrefs_nonmatch = []
52+
for link in wait_for_selector(notebook.browser, 'a.item_link'):
53+
href = link.get_attribute('href')
54+
if escaped_name in href:
55+
print("Opening", href)
56+
notebook.browser.get(href)
57+
wait_for_selector(notebook.browser, '.cell')
58+
break
59+
hrefs_nonmatch.append(href)
60+
else:
61+
raise AssertionError("{!r} not found in {!r}"
62+
.format(escaped_name, hrefs_nonmatch))
63+
64+
current_name = notebook.browser.execute_script("return Jupyter.notebook.notebook_name")
65+
assert current_name == nbname

notebook/tests/selenium/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,11 @@ def new_window(browser, selector=None):
324324
"""
325325
initial_window_handles = browser.window_handles
326326
yield
327-
new_window_handle = next(window for window in browser.window_handles
328-
if window not in initial_window_handles)
329-
browser.switch_to.window(new_window_handle)
327+
new_window_handles = [window for window in browser.window_handles
328+
if window not in initial_window_handles]
329+
if not new_window_handles:
330+
raise Exception("No new windows opened during context")
331+
browser.switch_to.window(new_window_handles[0])
330332
if selector is not None:
331333
wait_for_selector(browser, selector)
332334

0 commit comments

Comments
 (0)