From 8daa5f51b458c3d9d1ce42b34cce793e28dfd306 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 14 Apr 2018 01:42:48 -0400 Subject: [PATCH] Add a couple of utility functions to help get a single cell's content and type --- .../selenium/test_dualmode_insertcell.py | 36 +++++++++++++++++-- notebook/tests/selenium/utils.py | 7 ++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py index 53b4964e0..2982bcab2 100644 --- a/notebook/tests/selenium/test_dualmode_insertcell.py +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -1,4 +1,34 @@ -# TODO: Tests for dualmode insert cell functionality -def test_insert_cell(): - pass + +def test_insert_cell(notebook): + a = 'print("a")' + b = 'print("b")' + c = 'print("c")' + + notebook.edit_cell(index=0, content=a) + notebook.append(b, c) + notebook.to_command_mode() + + assert notebook.get_cells_contents() == [a, b, c] + + notebook.to_command_mode() + notebook.focus_cell(2) + notebook.convert_cell_type(2, "markdown") + notebook.current_cell.send_keys("a") + assert notebook.get_cell_contents(2) == '' + assert notebook.get_cell_type(2) == 'code' + assert len(notebook.cells) == 4 + + notebook.current_cell.send_keys('b') + assert notebook.get_cell_contents(2) == '' + assert notebook.get_cell_contents(3) == '' + assert notebook.get_cell_type(3) == 'code' + assert len(notebook.cells) == 5 + + notebook.focus_cell(2) + notebook.convert_cell_type(2, "markdown") + assert notebook.get_cell_type(2) == "markdown" + notebook.current_cell.send_keys("a") + assert notebook.get_cell_type(3) == "markdown" + notebook.current_cell.send_keys("b") + assert notebook.get_cell_type(4) == "markdown" diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index ef4092772..97d25bb97 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -131,10 +131,17 @@ class Notebook: JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' return self.browser.execute_script(JS) + def get_cell_contents(self, index=0, selector='div .CodeMirror-code'): + return self.cells[index].find_element_by_css_selector(selector).text + def set_cell_metadata(self, index, key, value): JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) return self.browser.execute_script(JS) + def get_cell_type(self, index=0): + JS = 'return Jupyter.notebook.get_cell({}).cell_type'.format(index) + return self.browser.execute_script(JS) + 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 """