From 95f02a7437802894658dc7c5b18308c4dc75fda3 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 5 Apr 2018 20:00:53 -0400 Subject: [PATCH 1/8] Initial commit: Add selenium tests for dualmode insert cell --- notebook/tests/selenium/test_dualmode_insertcell.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 notebook/tests/selenium/test_dualmode_insertcell.py diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py new file mode 100644 index 0000000000..53b4964e09 --- /dev/null +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -0,0 +1,4 @@ +# TODO: Tests for dualmode insert cell functionality + +def test_insert_cell(): + pass From 8daa5f51b458c3d9d1ce42b34cce793e28dfd306 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 14 Apr 2018 01:42:48 -0400 Subject: [PATCH 2/8] 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 53b4964e09..2982bcab28 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 ef40927721..97d25bb97b 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -131,10 +131,17 @@ def get_cells_contents(self): 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 """ From a46f5b81a199f9022fb8e5894803cc0860a8e260 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 26 Apr 2018 14:40:19 -0400 Subject: [PATCH 3/8] Add assertions for insert below and above cell commands in command mode and also after editing cells in edit mode --- .../selenium/test_dualmode_insertcell.py | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py index 2982bcab28..b22ecbaaa1 100644 --- a/notebook/tests/selenium/test_dualmode_insertcell.py +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -1,9 +1,9 @@ def test_insert_cell(notebook): - a = 'print("a")' - b = 'print("b")' - c = 'print("c")' + a = "print('a')" + b = "print('b')" + c = "print('c')" notebook.edit_cell(index=0, content=a) notebook.append(b, c) @@ -14,15 +14,18 @@ def test_insert_cell(notebook): notebook.to_command_mode() notebook.focus_cell(2) notebook.convert_cell_type(2, "markdown") + + # insert code cell above notebook.current_cell.send_keys("a") - assert notebook.get_cell_contents(2) == '' - assert notebook.get_cell_type(2) == 'code' + 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' + # insert code cell below + 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) @@ -32,3 +35,19 @@ def test_insert_cell(notebook): assert notebook.get_cell_type(3) == "markdown" notebook.current_cell.send_keys("b") assert notebook.get_cell_type(4) == "markdown" + + notebook.edit_cell(index=1, content="cell1") + notebook.focus_cell(1) + notebook.current_cell.send_keys("a") + assert notebook.get_cell_contents(1) == "" + assert notebook.get_cell_contents(2) == "cell1" + + notebook.edit_cell(index=1, content='cell1') + notebook.edit_cell(index=2, content='cell2') + notebook.edit_cell(index=3, content='cell3') + notebook.focus_cell(2) + notebook.current_cell.send_keys("b") + assert notebook.get_cell_contents(1) == "cell1" + assert notebook.get_cell_contents(2) == "cell2" + assert notebook.get_cell_contents(3) == "" + assert notebook.get_cell_contents(4) == "cell3" From ebf27aa94c8090a1ce4ade75fb77ffede37ae8f8 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 26 Apr 2018 14:40:39 -0400 Subject: [PATCH 4/8] Remove unnecessary imports --- notebook/tests/selenium/test_deletecell.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 388dd2ad83..7c4eebada1 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,5 +1,3 @@ -import os -import pytest def cell_is_deletable(nb, index): JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index) From 0acdea70d9950ce255eb615372e8b5447f9b249a Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 26 Apr 2018 15:53:26 -0400 Subject: [PATCH 5/8] Remove markdown cells assertions since they're not useful anymore --- notebook/tests/selenium/test_dualmode_insertcell.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py index b22ecbaaa1..3619f48281 100644 --- a/notebook/tests/selenium/test_dualmode_insertcell.py +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -28,14 +28,6 @@ def test_insert_cell(notebook): 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" - notebook.edit_cell(index=1, content="cell1") notebook.focus_cell(1) notebook.current_cell.send_keys("a") From 1283f27908042c60ac011b550c202b944eacf83e Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 26 Apr 2018 18:30:30 -0400 Subject: [PATCH 6/8] Add tests for insertion above/below multiple selected cells --- .../tests/selenium/test_dualmode_insertcell.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py index 3619f48281..48a977c98f 100644 --- a/notebook/tests/selenium/test_dualmode_insertcell.py +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -1,4 +1,5 @@ - +from selenium.webdriver.common.keys import Keys +from .utils import shift def test_insert_cell(notebook): a = "print('a')" @@ -43,3 +44,17 @@ def test_insert_cell(notebook): assert notebook.get_cell_contents(2) == "cell2" assert notebook.get_cell_contents(3) == "" assert notebook.get_cell_contents(4) == "cell3" + + # insert above multiple selected cells + notebook.focus_cell(1) + shift(notebook.browser, Keys.DOWN) + notebook.current_cell.send_keys('a') + + # insert below multiple selected cells + notebook.focus_cell(2) + shift(notebook.browser, Keys.DOWN) + notebook.current_cell.send_keys('b') + assert notebook.get_cell_contents(1) == "" + assert notebook.get_cell_contents(2) == "cell1" + assert notebook.get_cell_contents(3) == "cell2" + assert notebook.get_cell_contents(4) == "" From 17e46a7cd62fd39589cdfc80b51c797f8cb3c16b Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 28 Apr 2018 01:45:58 -0400 Subject: [PATCH 7/8] Remove js test file --- .../tests/notebook/dualmode_cellinsert.js | 82 ------------------- 1 file changed, 82 deletions(-) delete mode 100644 notebook/tests/notebook/dualmode_cellinsert.js diff --git a/notebook/tests/notebook/dualmode_cellinsert.js b/notebook/tests/notebook/dualmode_cellinsert.js deleted file mode 100644 index f066b16cc9..0000000000 --- a/notebook/tests/notebook/dualmode_cellinsert.js +++ /dev/null @@ -1,82 +0,0 @@ - -// Test -casper.notebook_test(function () { - var a = 'print("a")'; - var index = this.append_cell(a); - this.execute_cell_then(index); - - var b = 'print("b")'; - index = this.append_cell(b); - this.execute_cell_then(index); - - var c = 'print("c")'; - index = this.append_cell(c); - this.execute_cell_then(index); - - this.thenEvaluate(function() { - IPython.notebook.default_cell_type = 'code'; - }); - - this.then(function () { - // Cell insertion - this.select_cell(2); - this.trigger_keydown('m'); // Make it markdown - this.trigger_keydown('a'); // Creates one cell - this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty'); - this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'a; inserts a code cell'); - this.validate_notebook_state('a', 'command', 2); - this.trigger_keydown('b'); // Creates one cell - this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty'); - this.test.assertEquals(this.get_cell_text(3), '', 'b; New cell 3 text is empty'); - this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell'); - this.validate_notebook_state('b', 'command', 3); - }); - - this.thenEvaluate(function() { - IPython.notebook.class_config.set('default_cell_type', 'selected'); - }); - - this.then(function () { - this.select_cell(2); - this.trigger_keydown('m'); // switch it to markdown for the next test - this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'test cell is markdown'); - this.trigger_keydown('a'); // new cell above - this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'a; inserts a markdown cell when markdown selected'); - this.trigger_keydown('b'); // new cell below - this.test.assertEquals(this.get_cell(3).cell_type, 'markdown', 'b; inserts a markdown cell when markdown selected'); - }); - - this.thenEvaluate(function() { - IPython.notebook.class_config.set('default_cell_type', 'above'); - }); - - this.then(function () { - this.select_cell(2); - this.trigger_keydown('y'); // switch it to code for the next test - this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'test cell is code'); - this.trigger_keydown('b'); // new cell below - this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell below code cell'); - this.trigger_keydown('a'); // new cell above - this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'a; inserts a code cell above code cell'); - }); - - this.then(function () { - this.set_cell_text(1, 'cell1'); - this.select_cell(1); - this.select_cell(2, false); - this.trigger_keydown('a'); - this.test.assertEquals(this.get_cell_text(1), '', 'a; New cell 1 text is empty'); - this.test.assertEquals(this.get_cell_text(2), 'cell1', 'a; Cell 2 text is old cell 1'); - - this.set_cell_text(1, 'cell1'); - this.set_cell_text(2, 'cell2'); - this.set_cell_text(3, 'cell3'); - this.select_cell(1); - this.select_cell(2, false); - this.trigger_keydown('b'); - this.test.assertEquals(this.get_cell_text(1), 'cell1', 'b; Cell 1 remains'); - this.test.assertEquals(this.get_cell_text(2), 'cell2', 'b; Cell 2 remains'); - this.test.assertEquals(this.get_cell_text(3), '', 'b; Cell 3 is new'); - this.test.assertEquals(this.get_cell_text(4), 'cell3', 'b; Cell 4 text is old cell 3'); - }); -}); From b25a14222e8c9e97c4dd0439709b5006999ca4ef Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 28 Apr 2018 12:50:09 -0400 Subject: [PATCH 8/8] Update assert statement to use a list for more descriptive errors and less requests to the browser --- notebook/tests/selenium/test_dualmode_insertcell.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_insertcell.py b/notebook/tests/selenium/test_dualmode_insertcell.py index 48a977c98f..4ab100ced2 100644 --- a/notebook/tests/selenium/test_dualmode_insertcell.py +++ b/notebook/tests/selenium/test_dualmode_insertcell.py @@ -54,7 +54,4 @@ def test_insert_cell(notebook): notebook.focus_cell(2) shift(notebook.browser, Keys.DOWN) notebook.current_cell.send_keys('b') - assert notebook.get_cell_contents(1) == "" - assert notebook.get_cell_contents(2) == "cell1" - assert notebook.get_cell_contents(3) == "cell2" - assert notebook.get_cell_contents(4) == "" + assert notebook.get_cells_contents()[1:5] == ["", "cell1", "cell2", ""]