|
| 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 | + |
0 commit comments