Skip to content

Commit 6d76481

Browse files
authored
Merge pull request #4622 from TeresaPartidaS/convert_execute_code_js_to_selenium
Migrate execute code test to selenium
2 parents 0fba208 + 257e712 commit 6d76481

File tree

3 files changed

+71
-115
lines changed

3 files changed

+71
-115
lines changed

notebook/tests/notebook/execute_code.js

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from selenium.webdriver.common.keys import Keys
2+
from .utils import shift, cmdtrl
3+
4+
5+
def test_execute_code(notebook):
6+
browser = notebook.browser
7+
8+
def clear_outputs():
9+
return notebook.browser.execute_script(
10+
"Jupyter.notebook.clear_all_output();")
11+
12+
# Execute cell with Javascript API
13+
notebook.edit_cell(index=0, content='a=10; print(a)')
14+
browser.execute_script("Jupyter.notebook.get_cell(0).execute();")
15+
outputs = notebook.wait_for_cell_output(0)
16+
assert outputs[0].text == '10'
17+
18+
# Execute cell with Shift-Enter
19+
notebook.edit_cell(index=0, content='a=11; print(a)')
20+
clear_outputs()
21+
shift(notebook.browser, Keys.ENTER)
22+
outputs = notebook.wait_for_cell_output(0)
23+
assert outputs[0].text == '11'
24+
notebook.delete_cell(index=1)
25+
26+
# Execute cell with Ctrl-Enter
27+
notebook.edit_cell(index=0, content='a=12; print(a)')
28+
clear_outputs()
29+
cmdtrl(notebook.browser, Keys.ENTER)
30+
outputs = notebook.wait_for_cell_output(0)
31+
assert outputs[0].text == '12'
32+
33+
# Execute cell with toolbar button
34+
notebook.edit_cell(index=0, content='a=13; print(a)')
35+
clear_outputs()
36+
notebook.browser.find_element_by_css_selector(
37+
"button[data-jupyter-action='jupyter-notebook:run-cell-and-select-next']").click()
38+
outputs = notebook.wait_for_cell_output(0)
39+
assert outputs[0].text == '13'
40+
41+
# Set up two cells to test stopping on error
42+
notebook.edit_cell(index=0, content='raise IOError')
43+
notebook.edit_cell(index=1, content='a=14; print(a)')
44+
45+
# Default behaviour: stop on error
46+
clear_outputs()
47+
browser.execute_script("""
48+
var cell0 = Jupyter.notebook.get_cell(0);
49+
var cell1 = Jupyter.notebook.get_cell(1);
50+
cell0.execute();
51+
cell1.execute();
52+
""")
53+
outputs = notebook.wait_for_cell_output(0)
54+
assert notebook.get_cell_output(1) == []
55+
56+
# Execute a cell with stop_on_error=false
57+
clear_outputs()
58+
browser.execute_script("""
59+
var cell0 = Jupyter.notebook.get_cell(0);
60+
var cell1 = Jupyter.notebook.get_cell(1);
61+
cell0.execute(false);
62+
cell1.execute();
63+
""")
64+
outputs = notebook.wait_for_cell_output(1)
65+
assert outputs[0].text == '14'
66+

notebook/tests/selenium/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ def get_cell_contents(self, index=0, selector='div .CodeMirror-code'):
218218
def get_cell_output(self, index=0, output='output_subarea'):
219219
return self.cells[index].find_elements_by_class_name(output)
220220

221+
def wait_for_cell_output(self, index=0, timeout=10):
222+
return WebDriverWait(self.browser, timeout).until(
223+
lambda b: self.get_cell_output(index)
224+
)
225+
221226
def set_cell_metadata(self, index, key, value):
222227
JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value)
223228
return self.browser.execute_script(JS)

0 commit comments

Comments
 (0)