Simplify test for interrupting running code

Thomas Kluyver 7 years ago
parent 17176de1f2
commit 46614c3772

@ -255,8 +255,8 @@ data-notebook-path="{{notebook_path | urlencode}}"
</li>
</ul>
</li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">{% trans %}Kernel{% endtrans %}</a>
<ul id="kernel_menu" class="dropdown-menu">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" id="kernellink">{% trans %}Kernel{% endtrans %}</a>
<ul id="kernel_menu" class="dropdown-menu" aria-labelledby="kernellink">
<li id="int_kernel"
title="{% trans %}Send Keyboard Interrupt (CTRL-C) to the Kernel{% endtrans %}">
<a href="#">{% trans %}Interrupt{% endtrans %}</a>

@ -1,32 +1,14 @@
from selenium.webdriver.common.keys import Keys
from .utils import wait_for_selector
from functools import wraps
import errno
import os
import signal
class TimeoutError(Exception):
pass
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
def decorator(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
def interrupt_from_menu(notebook):
# Click interrupt button in kernel menu
notebook.browser.find_element_by_id('kernellink').click()
wait_for_selector(notebook.browser, '#int_kernel', single=True).click()
return wraps(func)(wrapper)
def interrupt_from_keyboard(notebook):
notebook.body.send_keys("ii")
return decorator
@timeout(15)
def test_interrupt(notebook):
""" Test the interrupt function using both the button in the Kernel menu and the keyboard shortcut "ii"
@ -36,29 +18,19 @@ def test_interrupt(notebook):
Hope this is a good start.
"""
text = 'import time'+'\nwhile(1):'+'\n for x in range(3):'+'\n time.sleep(1)'
text = ('import time\n'
'for x in range(3):\n'
' time.sleep(1)')
notebook.edit_cell(index=0, content=text)
notebook.to_command_mode()
notebook.execute_cell(0)
# Click interrupt button in kernel menu
kernel_button = wait_for_selector(notebook.browser, '#menus > div > div > ul > li:nth-child(6) > a', single=True)
kernel_button.click()
int_button = wait_for_selector(notebook.browser, '#int_kernel > a', single=True)
int_button.click()
# outputs = get_cells_output(notebook, 0)
# assert outputs.find("KeyboardInterrupt") != -1
for interrupt_method in (interrupt_from_menu, interrupt_from_keyboard):
notebook.clear_cell_output(0)
notebook.to_command_mode()
notebook.execute_cell(0)
# Clear output and repeat execution
notebook.clear_cells_output(0)
notebook.to_command_mode()
notebook.execute_cell(0)
# Send interrupt shortcut via keyboard
notebook.body.send_keys(Keys.ESCAPE)
notebook.body.send_keys("ii")
interrupt_method(notebook)
# outputs = get_cells_output(notebook, 0)
# assert outputs.find("KeyboardInterrupt") != -1
# Wait for an output to appear
output = wait_for_selector(notebook.browser, '.output_subarea', single=True)
assert 'KeyboardInterrupt' in output.text

@ -230,14 +230,6 @@ class Notebook:
JS = 'Jupyter.notebook.get_cell({}).set_input_prompt({})'.format(index, prmpt_val)
self.browser.execute_script(JS)
def get_cells_output(self, index):
""" Returns JSON object that contains the "outputs" field for the cell.
"""
JS = 'return Jupyter.notebook.get_cell({}).toJSON();'.format(index)
result = self.browser.execute_script(JS)
return result["outputs"]
def edit_cell(self, cell=None, index=0, content="", render=False):
"""Set the contents of a cell to *content*, by cell object or by index
"""
@ -312,7 +304,7 @@ class Notebook:
"return Jupyter.notebook.kernel && Jupyter.notebook.kernel.is_connected()"
)
def clear_cells_output(self, index):
def clear_cell_output(self, index):
JS = 'Jupyter.notebook.clear_output({})'.format(index)
self.browser.execute_script(JS)

Loading…
Cancel
Save