|
|
|
|
@ -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
|
|
|
|
|
|