Merge pull request #4622 from TeresaPartidaS/convert_execute_code_js_to_selenium

Migrate execute code test to selenium
Thomas Kluyver 7 years ago committed by GitHub
commit 6d76481ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,115 +0,0 @@
//
// Test code cell execution.
//
casper.notebook_test(function () {
this.evaluate(function () {
var cell = IPython.notebook.get_cell(0);
cell.set_text('a=10; print(a)');
cell.execute();
});
this.wait_for_output(0);
// refactor this into just a get_output(0)
this.then(function () {
var result = this.get_output_cell(0);
this.test.assertEquals(result.text, '10\n', 'cell execute (using js)');
});
// do it again with the keyboard shortcut
this.thenEvaluate(function () {
var cell = IPython.notebook.get_cell(0);
cell.set_text('a=11; print(a)');
cell.clear_output();
});
this.then(function(){
this.trigger_keydown('shift-enter');
});
this.wait_for_output(0);
this.then(function () {
var result = this.get_output_cell(0);
var num_cells = this.get_cells_length();
this.test.assertEquals(result.text, '11\n', 'cell execute (using ctrl-enter)');
this.test.assertEquals(num_cells, 2, 'shift-enter adds a new cell at the bottom')
});
// do it again with the keyboard shortcut
this.thenEvaluate(function () {
IPython.notebook.select(1);
IPython.notebook.delete_cell();
var cell = IPython.notebook.get_cell(0);
cell.set_text('a=12; print(a)');
cell.clear_output();
});
this.then(function(){
this.trigger_keydown('ctrl-enter');
});
this.wait_for_output(0);
this.then(function () {
var result = this.get_output_cell(0);
var num_cells = this.get_cells_length();
this.test.assertEquals(result.text, '12\n', 'cell execute (using shift-enter)');
this.test.assertEquals(num_cells, 1, 'ctrl-enter adds no new cell at the bottom')
});
// press the "play" triangle button in the toolbar
this.thenEvaluate(function () {
var cell = IPython.notebook.get_cell(0);
IPython.notebook.select(0);
cell.clear_output();
cell.set_text('a=13; print(a)');
$("button[data-jupyter-action='jupyter-notebook:run-cell-and-select-next']")[0].click()
});
this.wait_for_output(0);
this.then(function () {
var result = this.get_output_cell(0);
this.test.assertEquals(result.text, '13\n', 'cell execute (using "play" toolbar button)')
});
// run code with skip_exception
this.thenEvaluate(function () {
var cell0 = IPython.notebook.get_cell(0);
cell0.set_text('raise IOError');
IPython.notebook.insert_cell_below('code',0);
var cell1 = IPython.notebook.get_cell(1);
cell1.set_text('a=14; print(a)');
cell0.execute(false);
cell1.execute();
});
this.wait_for_output(1);
this.then(function () {
var result = this.get_output_cell(1);
this.test.assertEquals(result.text, '14\n', "cell execute, don't stop on error");
});
this.thenEvaluate(function () {
var cell0 = IPython.notebook.get_cell(0);
cell0.set_text('raise IOError');
IPython.notebook.insert_cell_below('code',0);
var cell1 = IPython.notebook.get_cell(1);
cell1.set_text('a=14; print(a)');
cell0.execute();
cell1.execute();
});
this.wait_for_output(0);
this.then(function () {
var outputs = this.evaluate(function() {
return IPython.notebook.get_cell(1).output_area.outputs;
})
this.test.assertEquals(outputs.length, 0, 'cell execute, stop on error (default)');
});
});

@ -0,0 +1,66 @@
from selenium.webdriver.common.keys import Keys
from .utils import shift, cmdtrl
def test_execute_code(notebook):
browser = notebook.browser
def clear_outputs():
return notebook.browser.execute_script(
"Jupyter.notebook.clear_all_output();")
# Execute cell with Javascript API
notebook.edit_cell(index=0, content='a=10; print(a)')
browser.execute_script("Jupyter.notebook.get_cell(0).execute();")
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '10'
# Execute cell with Shift-Enter
notebook.edit_cell(index=0, content='a=11; print(a)')
clear_outputs()
shift(notebook.browser, Keys.ENTER)
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '11'
notebook.delete_cell(index=1)
# Execute cell with Ctrl-Enter
notebook.edit_cell(index=0, content='a=12; print(a)')
clear_outputs()
cmdtrl(notebook.browser, Keys.ENTER)
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '12'
# Execute cell with toolbar button
notebook.edit_cell(index=0, content='a=13; print(a)')
clear_outputs()
notebook.browser.find_element_by_css_selector(
"button[data-jupyter-action='jupyter-notebook:run-cell-and-select-next']").click()
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '13'
# Set up two cells to test stopping on error
notebook.edit_cell(index=0, content='raise IOError')
notebook.edit_cell(index=1, content='a=14; print(a)')
# Default behaviour: stop on error
clear_outputs()
browser.execute_script("""
var cell0 = Jupyter.notebook.get_cell(0);
var cell1 = Jupyter.notebook.get_cell(1);
cell0.execute();
cell1.execute();
""")
outputs = notebook.wait_for_cell_output(0)
assert notebook.get_cell_output(1) == []
# Execute a cell with stop_on_error=false
clear_outputs()
browser.execute_script("""
var cell0 = Jupyter.notebook.get_cell(0);
var cell1 = Jupyter.notebook.get_cell(1);
cell0.execute(false);
cell1.execute();
""")
outputs = notebook.wait_for_cell_output(1)
assert outputs[0].text == '14'

@ -218,6 +218,11 @@ class Notebook:
def get_cell_output(self, index=0, output='output_subarea'):
return self.cells[index].find_elements_by_class_name(output)
def wait_for_cell_output(self, index=0, timeout=10):
return WebDriverWait(self.browser, timeout).until(
lambda b: self.get_cell_output(index)
)
def set_cell_metadata(self, index, key, value):
JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value)
return self.browser.execute_script(JS)

Loading…
Cancel
Save