Skip to content

Commit

Permalink
Merge pull request #4377 from takluyver/selenium-test-save
Browse files Browse the repository at this point in the history
Convert test of saving with complex name to Selenium
  • Loading branch information
takluyver authored Feb 14, 2019
2 parents 8a4cbd0 + bc3a8cb commit 132f273
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 116 deletions.
5 changes: 4 additions & 1 deletion notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,8 @@ define([
$.proxy(that.save_notebook_success, that, start),
function (error) {
that.events.trigger('notebook_save_failed.Notebook', error);
// This hasn't handled the error, so propagate it up
return Promise.reject(error);
}
);
};
Expand Down Expand Up @@ -2845,6 +2847,7 @@ define([
this.create_checkpoint();
this._checkpoint_after_save = false;
}
return data;
};

Notebook.prototype.save_notebook_as = function() {
Expand Down Expand Up @@ -3309,7 +3312,7 @@ define([
*/
Notebook.prototype.save_checkpoint = function () {
this._checkpoint_after_save = true;
this.save_notebook(true);
return this.save_notebook(true);
};

/**
Expand Down
112 changes: 0 additions & 112 deletions notebook/tests/notebook/save.js

This file was deleted.

65 changes: 65 additions & 0 deletions notebook/tests/selenium/test_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Test saving a notebook with escaped characters
"""

from urllib.parse import quote
from .utils import wait_for_selector, new_window

promise_js = """
var done = arguments[arguments.length - 1];
%s.then(
data => { done(["success", data]); },
error => { done(["error", error]); }
);
"""

def execute_promise(js, browser):
state, data = browser.execute_async_script(promise_js % js)
if state == 'success':
return data
raise Exception(data)


def test_save(notebook):
# don't use unicode with ambiguous composed/decomposed normalization
# because the filesystem may use a different normalization than literals.
# This causes no actual problems, but will break string comparison.
nbname = "has#hash and space and unicø∂e.ipynb"
escaped_name = quote(nbname)

notebook.edit_cell(index=0, content="s = '??'")

notebook.browser.execute_script("Jupyter.notebook.set_notebook_name(arguments[0])", nbname)

model = execute_promise("Jupyter.notebook.save_notebook()", notebook.browser)
assert model['name'] == nbname

current_name = notebook.browser.execute_script("return Jupyter.notebook.notebook_name")
assert current_name == nbname

current_path = notebook.browser.execute_script("return Jupyter.notebook.notebook_path")
assert current_path == nbname

displayed_name = notebook.browser.find_element_by_id('notebook_name').text
assert displayed_name + '.ipynb' == nbname

execute_promise("Jupyter.notebook.save_checkpoint()", notebook.browser)

checkpoints = notebook.browser.execute_script("return Jupyter.notebook.checkpoints")
assert len(checkpoints) == 1

notebook.browser.find_element_by_css_selector('#ipython_notebook a').click()
hrefs_nonmatch = []
for link in wait_for_selector(notebook.browser, 'a.item_link'):
href = link.get_attribute('href')
if escaped_name in href:
print("Opening", href)
notebook.browser.get(href)
wait_for_selector(notebook.browser, '.cell')
break
hrefs_nonmatch.append(href)
else:
raise AssertionError("{!r} not found in {!r}"
.format(escaped_name, hrefs_nonmatch))

current_name = notebook.browser.execute_script("return Jupyter.notebook.notebook_name")
assert current_name == nbname
8 changes: 5 additions & 3 deletions notebook/tests/selenium/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,11 @@ def new_window(browser, selector=None):
"""
initial_window_handles = browser.window_handles
yield
new_window_handle = next(window for window in browser.window_handles
if window not in initial_window_handles)
browser.switch_to.window(new_window_handle)
new_window_handles = [window for window in browser.window_handles
if window not in initial_window_handles]
if not new_window_handles:
raise Exception("No new windows opened during context")
browser.switch_to.window(new_window_handles[0])
if selector is not None:
wait_for_selector(browser, selector)

Expand Down

0 comments on commit 132f273

Please sign in to comment.