1. Converting 'empty_arrows_keys.js' into selenium based test

2. Moved "delete_cell" method to utils.py and modified references to use it from there
3. added a generalized method "trigger_keystrokes" to send keystrokes to browser
arovitn 8 years ago
parent 55be52b8a3
commit 031c90b933

@ -1,21 +0,0 @@
//
// Check for errors with up and down arrow presses in an empty notebook.
//
casper.notebook_test(function () {
var result = this.evaluate(function() {
var ncells = IPython.notebook.ncells();
var i;
// Delete all cells.
for (i = 0; i < ncells; i++) {
IPython.notebook.delete_cell();
}
return true;
});
// Simulate the "up arrow" and "down arrow" keys.
this.trigger_keydown('up');
this.trigger_keydown('down');
this.test.assertTrue(result, 'Up/down arrow okay in empty notebook.');
});

@ -3,11 +3,6 @@ def cell_is_deletable(nb, index):
JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index)
return nb.browser.execute_script(JS)
def delete_cell(notebook, index):
notebook.focus_cell(index)
notebook.to_command_mode
notebook.current_cell.send_keys('dd')
def test_delete_cells(notebook):
a = 'print("a")'
b = 'print("b")'
@ -30,22 +25,22 @@ def test_delete_cells(notebook):
assert cell_is_deletable(notebook, 2)
# Try to delete cell a (should not be deleted)
delete_cell(notebook, 0)
notebook.delete_cell(0)
assert notebook.get_cells_contents() == [a, b, c]
# Try to delete cell b (should succeed)
delete_cell(notebook, 1)
notebook.delete_cell(1)
assert notebook.get_cells_contents() == [a, c]
# Try to delete cell c (should succeed)
delete_cell(notebook, 1)
notebook.delete_cell(1)
assert notebook.get_cells_contents() == [a]
# Change the deletable state of cell a
notebook.set_cell_metadata(0, 'deletable', 'true')
# Try to delete cell a (should succeed)
delete_cell(notebook, 0)
notebook.delete_cell(0)
assert len(notebook.cells) == 1 # it contains an empty cell
# Make sure copied cells are deletable

@ -0,0 +1,13 @@
# selenium test version for 'empty_arrow_keys.js'
def remove_cells(notebook):
for i in notebook.cells:
notebook.delete_cell(notebook.index(i))
return True
def test_empty_arrows_keys(notebook):
# delete all the cells
notebook.trigger_keydown('up')
notebook.trigger_keydown('down')
assert remove_cells(notebook);

@ -184,6 +184,11 @@ class Notebook:
if cell_type != 'code':
self.convert_cell_type(index=new_index, cell_type=cell_type)
def delete_cell(self, index):
self.focus_cell(index)
self.to_command_mode
self.current_cell.send_keys('dd')
def add_markdown_cell(self, index=-1, content="", render=True):
self.add_cell(index, cell_type="markdown")
self.edit_cell(index=index, content=content, render=render)
@ -203,6 +208,9 @@ class Notebook:
for cell in self:
self.execute_cell(cell)
def trigger_keydown(self, keys):
trigger_keystrokes(self.body, keys)
@classmethod
def new_notebook(cls, browser, kernel_name='kernel-python3'):
with new_window(browser, selector=".cell"):
@ -251,10 +259,27 @@ def new_window(browser, selector=None):
def shift(browser, k):
"""Send key combination Shift+(k)"""
ActionChains(browser)\
.key_down(Keys.SHIFT).send_keys(k).key_up(Keys.SHIFT).perform()
trigger_keystrokes(browser, "shift-%s"%k)
def ctrl(browser, k):
"""Send key combination Ctrl+(k)"""
ActionChains(browser)\
.key_down(Keys.CONTROL).send_keys(k).key_up(Keys.CONTROL).perform()
trigger_keystrokes(browser, "control-%s"%k)
def trigger_keystrokes(browser, *keys):
""" Send the keys in sequence to the browser.
Handles following key combinations
1. with modifiers eg. 'control-alt-a', 'shift-c'
2. just modifiers eg. 'alt', 'esc'
3. non-modifiers eg. 'abc'
Modifiers : http://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
"""
for each_key_combination in keys:
keys = each_key_combination.split('-')
if len(keys) > 1: # key has modifiers eg. control, alt, shift
modifiers_keys = list(map(lambda x:getattr(Keys, x.upper()), keys[:-1]))
ac = ActionChains(browser)
for i in modifiers_keys: ac = ac.key_down(i)
ac.send_keys(keys[-1])
for i in modifiers_keys[::-1]: ac = ac.key_up(i)
else: # single key stroke. Check if modifier eg. "up"
browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))

Loading…
Cancel
Save