diff --git a/notebook/tests/notebook/empty_arrow_keys.js b/notebook/tests/notebook/empty_arrow_keys.js deleted file mode 100644 index a949ce53a7..0000000000 --- a/notebook/tests/notebook/empty_arrow_keys.js +++ /dev/null @@ -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.'); -}); diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 7c4eebada1..3b262fe949 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -3,10 +3,9 @@ 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 remove_all_cells(notebook): + for i in range(len(notebook.cells)): + notebook.delete_cell(0) def test_delete_cells(notebook): a = 'print("a")' @@ -30,22 +29,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 @@ -56,3 +55,7 @@ def test_delete_cells(notebook): notebook.current_cell.send_keys('cv') assert len(notebook.cells) == 2 assert cell_is_deletable(notebook, 1) + + notebook.set_cell_metadata(0, 'deletable', 'true') # to perform below test, remove all the cells + remove_all_cells(notebook) + assert len(notebook.cells) == 1 # notebook should create one automatically on empty notebook diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 1960b200a2..2c18ec49b1 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -194,6 +194,11 @@ def add_cell(self, index=-1, cell_type="code", content=""): 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) @@ -213,6 +218,9 @@ def run_all(self): 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"): @@ -261,11 +269,28 @@ 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 = [getattr(Keys, x.upper()) for x in 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) + ac.perform() + else: # single key stroke. Check if modifier eg. "up" + browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))