From 5677e22f5945a38d36a7801296d1c2550221a21e Mon Sep 17 00:00:00 2001 From: hkshesht Date: Sat, 24 Mar 2018 16:04:34 -0400 Subject: [PATCH 01/14] Initial commit to convert deletecell.js to selenium --- notebook/tests/selenium/test_deletecell.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 notebook/tests/selenium/test_deletecell.py diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py new file mode 100644 index 000000000..f8c8d98b0 --- /dev/null +++ b/notebook/tests/selenium/test_deletecell.py @@ -0,0 +1,16 @@ +import os + +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +pjoin = os.path.join + +def cell_is_deletable(index): + pass + +def test_deletable_cells(browser): + pass + +def test_non_deletable_cells(browser): + pass From 7b8db48f1bc4c37b317aa31219f41fb2021cf55b Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 31 Mar 2018 16:58:47 -0400 Subject: [PATCH 02/14] Add tests for deletable cells and non-deleteable cells --- notebook/tests/selenium/test_deletecell.py | 67 ++++++++++++++++++---- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index f8c8d98b0..7d6a96a69 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,16 +1,63 @@ import os +import pytest +from .utils import Notebook -from selenium.webdriver.common.by import By -from selenium.webdriver.support.ui import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC +def get_cells_contents(nb): + JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' + return nb.browser.execute_script(JS) -pjoin = os.path.join +def set_cell_metadata(nb, index, key, value): + JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) + return nb.browser.execute_script(JS) -def cell_is_deletable(index): - pass +def cell_is_deletable(nb, index): + JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index) + return nb.browser.execute_script(JS) -def test_deletable_cells(browser): - pass +def delete_cell(notebook, index): + notebook.focus_cell(index) + notebook.to_command_mode + notebook.current_cell.send_keys('dd') -def test_non_deletable_cells(browser): - pass +def test_delete_cells(notebook): + print('testing deleteable cells') + a = 'print("a")' + b = 'print("b")' + c = 'print("c")' + + notebook.edit_cell(index=0, content=a) + notebook.append(b, c) + notebook.to_command_mode() + + # Validate initial state + assert get_cells_contents(notebook) == [a, b, c] + for cell in range(0, 3): + assert cell_is_deletable(notebook, cell) + + set_cell_metadata(notebook, 0, 'deletable', 'false') + set_cell_metadata(notebook, 1, 'deletable', 0 + ) + assert not cell_is_deletable(notebook, 0) + assert cell_is_deletable(notebook, 1) + assert cell_is_deletable(notebook, 2) + + # Try to delete cell a (should not be deleted) + delete_cell(notebook, 0) + assert get_cells_contents(notebook) == [a, b, c] + + # Try to delete cell b (should succeed) + delete_cell(notebook, 1) + assert get_cells_contents(notebook) == [a, c] + + # Try to delete cell c + delete_cell(notebook, 1) + assert get_cells_contents(notebook) == [a] + + # Change the deletable state of cell a + set_cell_metadata(notebook, 0, 'deletable', 'true') + + # Try to delete cell a (should succeed) + delete_cell(notebook, 0) + assert len(notebook.cells) == 1 # it contains an empty cell +# NOTE: APIs that will be useful for testing +# http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From 6b1691d3a57dd66c3e9d3ff799b2ce5c97228756 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 31 Mar 2018 17:04:51 -0400 Subject: [PATCH 03/14] Add tests to make sure copied cells are deletable --- notebook/tests/selenium/test_deletecell.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 7d6a96a69..68d162bf5 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -59,5 +59,15 @@ def test_delete_cells(notebook): # Try to delete cell a (should succeed) delete_cell(notebook, 0) assert len(notebook.cells) == 1 # it contains an empty cell + + # Make sure copied cells are deletable + notebook.edit_cell(index=0, content=a) + set_cell_metadata(notebook, 0, 'deletable', 'false') + assert not cell_is_deletable(notebook, 0) + notebook.to_command_mode() + notebook.current_cell.send_keys('cv') + assert len(notebook.cells) == 2 + assert cell_is_deletable(notebook, 1) + # NOTE: APIs that will be useful for testing # http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From 5dbb2ac97552c9e603ebe26f05a5612714c37adf Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 2 Apr 2018 16:49:02 -0400 Subject: [PATCH 04/14] Move notebook fixture to deletetest file to minimize the dependency on changes in PR #3475. This should be update once that PR is merged --- notebook/tests/selenium/test_deletecell.py | 4 ++++ notebook/tests/selenium/utils.py | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 68d162bf5..0b7efb975 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -2,6 +2,10 @@ import os import pytest from .utils import Notebook +@pytest.fixture +def notebook(authenticated_browser): + return Notebook.new_notebook(authenticated_browser) + def get_cells_contents(nb): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' return nb.browser.execute_script(JS) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 3aed1d554..99b547f7f 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -154,7 +154,8 @@ class Notebook: new_index = index + 1 if index >= 0 else index if content: self.edit_cell(index=index, content=content) - self.convert_cell_type(index=new_index, cell_type=cell_type) + if cell_type != 'code': + self.convert_cell_type(index=new_index, cell_type=cell_type) def add_markdown_cell(self, index=-1, content="", render=True): self.add_cell(index, cell_type="markdown") From 2c5748f82ebc5c3f5f591226ac51af9956a5525b Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 2 Apr 2018 17:21:50 -0400 Subject: [PATCH 05/14] Remove unnecessary comments --- notebook/tests/selenium/test_deletecell.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 0b7efb975..6b297fd31 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -53,7 +53,7 @@ def test_delete_cells(notebook): delete_cell(notebook, 1) assert get_cells_contents(notebook) == [a, c] - # Try to delete cell c + # Try to delete cell c (should succeed) delete_cell(notebook, 1) assert get_cells_contents(notebook) == [a] @@ -72,6 +72,3 @@ def test_delete_cells(notebook): notebook.current_cell.send_keys('cv') assert len(notebook.cells) == 2 assert cell_is_deletable(notebook, 1) - -# NOTE: APIs that will be useful for testing -# http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From 413cca8cbd80af7e408f8d7c52652bf4aebd77d5 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Tue, 3 Apr 2018 16:23:23 -0400 Subject: [PATCH 06/14] Delete tests Update to use the updated notebook fixture --- notebook/tests/notebook/deletecell.js | 107 --------------------- notebook/tests/selenium/test_deletecell.py | 6 -- 2 files changed, 113 deletions(-) delete mode 100644 notebook/tests/notebook/deletecell.js diff --git a/notebook/tests/notebook/deletecell.js b/notebook/tests/notebook/deletecell.js deleted file mode 100644 index 6c98f28cc..000000000 --- a/notebook/tests/notebook/deletecell.js +++ /dev/null @@ -1,107 +0,0 @@ - -// Test -casper.notebook_test(function () { - var that = this; - var cell_is_deletable = function (index) { - // Get the deletable status of a cell. - return that.evaluate(function (index) { - var cell = IPython.notebook.get_cell(index); - return cell.is_deletable(); - }, index); - }; - - var a = 'print("a")'; - var index = this.append_cell(a); - - var b = 'print("b")'; - index = this.append_cell(b); - - var c = 'print("c")'; - index = this.append_cell(c); - - this.thenEvaluate(function() { - IPython.notebook.get_cell(1).metadata.deletable = false; - IPython.notebook.get_cell(2).metadata.deletable = 0; // deletable only when exactly false - IPython.notebook.get_cell(3).metadata.deletable = true; - }); - - this.then(function () { - // Check deletable status of the cells - this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable'); - this.test.assert(!cell_is_deletable(1), 'Cell 1 is not deletable'); - this.test.assert(cell_is_deletable(2), 'Cell 2 is deletable'); - this.test.assert(cell_is_deletable(3), 'Cell 3 is deletable'); - }); - - // Try to delete cell 0 (should succeed) - this.then(function () { - this.select_cell(0); - this.trigger_keydown('esc'); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are now 3 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 1 is now cell 0'); - this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 2 is now cell 1'); - this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 3 is now cell 2'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Try to delete cell 0 (should fail) - this.then(function () { - this.select_cell(0); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are still 3 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 0 was not deleted'); - this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 1 was not affected'); - this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 2 was not affected'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Try to delete cell 1 (should succeed) - this.then(function () { - this.select_cell(1); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 2, 'Delete cell 1: There are now 2 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 1: Cell 0 was not affected'); - this.test.assertEquals(this.get_cell_text(1), c, 'Delete cell 1: Cell 1 was not affected'); - this.validate_notebook_state('dd', 'command', 1); - }); - - // Try to delete cell 1 (should succeed) - this.then(function () { - this.select_cell(1); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 1, 'Delete cell 1: There is now 1 cell'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 2: Cell 0 was not affected'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Change the deletable status of the last cells - this.thenEvaluate(function() { - IPython.notebook.get_cell(0).metadata.deletable = true; - }); - - this.then(function () { - // Check deletable status of the cell - this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable'); - - // Try to delete the last cell (should succeed) - this.select_cell(0); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 1, 'Delete last cell: There is still 1 cell'); - this.test.assertEquals(this.get_cell_text(0), "", 'Delete last cell: Cell 0 was deleted'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Make sure copied cells are deletable - this.thenEvaluate(function() { - IPython.notebook.get_cell(0).metadata.deletable = false; - }); - this.then(function () { - this.select_cell(0); - this.trigger_keydown('c', 'v'); - this.test.assertEquals(this.get_cells_length(), 2, 'Copy cell: There are 2 cells'); - this.test.assert(!cell_is_deletable(0), 'Cell 0 is not deletable'); - this.test.assert(cell_is_deletable(1), 'Cell 1 is deletable'); - this.validate_notebook_state('cv', 'command', 1); - }); -}); diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 6b297fd31..60f7d8127 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,10 +1,5 @@ import os import pytest -from .utils import Notebook - -@pytest.fixture -def notebook(authenticated_browser): - return Notebook.new_notebook(authenticated_browser) def get_cells_contents(nb): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' @@ -24,7 +19,6 @@ def delete_cell(notebook, index): notebook.current_cell.send_keys('dd') def test_delete_cells(notebook): - print('testing deleteable cells') a = 'print("a")' b = 'print("b")' c = 'print("c")' From 14acb8d7e9b7c6a211e0127dc455552f7870480d Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Wed, 4 Apr 2018 10:39:11 -0400 Subject: [PATCH 07/14] Move some utility functions from the test module to the utils.py module --- notebook/tests/selenium/test_deletecell.py | 24 ++++++++-------------- notebook/tests/selenium/utils.py | 8 ++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 60f7d8127..388dd2ad8 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,14 +1,6 @@ import os import pytest -def get_cells_contents(nb): - JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' - return nb.browser.execute_script(JS) - -def set_cell_metadata(nb, index, key, value): - JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) - return nb.browser.execute_script(JS) - def cell_is_deletable(nb, index): JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index) return nb.browser.execute_script(JS) @@ -28,12 +20,12 @@ def test_delete_cells(notebook): notebook.to_command_mode() # Validate initial state - assert get_cells_contents(notebook) == [a, b, c] + assert notebook.get_cells_contents() == [a, b, c] for cell in range(0, 3): assert cell_is_deletable(notebook, cell) - set_cell_metadata(notebook, 0, 'deletable', 'false') - set_cell_metadata(notebook, 1, 'deletable', 0 + notebook.set_cell_metadata(0, 'deletable', 'false') + notebook.set_cell_metadata(1, 'deletable', 0 ) assert not cell_is_deletable(notebook, 0) assert cell_is_deletable(notebook, 1) @@ -41,18 +33,18 @@ def test_delete_cells(notebook): # Try to delete cell a (should not be deleted) delete_cell(notebook, 0) - assert get_cells_contents(notebook) == [a, b, c] + assert notebook.get_cells_contents() == [a, b, c] # Try to delete cell b (should succeed) delete_cell(notebook, 1) - assert get_cells_contents(notebook) == [a, c] + assert notebook.get_cells_contents() == [a, c] # Try to delete cell c (should succeed) delete_cell(notebook, 1) - assert get_cells_contents(notebook) == [a] + assert notebook.get_cells_contents() == [a] # Change the deletable state of cell a - set_cell_metadata(notebook, 0, 'deletable', 'true') + notebook.set_cell_metadata(0, 'deletable', 'true') # Try to delete cell a (should succeed) delete_cell(notebook, 0) @@ -60,7 +52,7 @@ def test_delete_cells(notebook): # Make sure copied cells are deletable notebook.edit_cell(index=0, content=a) - set_cell_metadata(notebook, 0, 'deletable', 'false') + notebook.set_cell_metadata(0, 'deletable', 'false') assert not cell_is_deletable(notebook, 0) notebook.to_command_mode() notebook.current_cell.send_keys('cv') diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index efe49fb4c..ef4092772 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -127,6 +127,14 @@ class Notebook: wait = WebDriverWait(self.browser, 10) element = wait.until(EC.staleness_of(cell)) + 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 set_cell_metadata(self, index, key, value): + JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) + 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 835211f03985d1e17dd0b62cf6f9c3fd67c5a7ad Mon Sep 17 00:00:00 2001 From: hkshesht Date: Sat, 24 Mar 2018 16:04:34 -0400 Subject: [PATCH 08/14] Initial commit to convert deletecell.js to selenium --- notebook/tests/selenium/test_deletecell.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 notebook/tests/selenium/test_deletecell.py diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py new file mode 100644 index 000000000..f8c8d98b0 --- /dev/null +++ b/notebook/tests/selenium/test_deletecell.py @@ -0,0 +1,16 @@ +import os + +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +pjoin = os.path.join + +def cell_is_deletable(index): + pass + +def test_deletable_cells(browser): + pass + +def test_non_deletable_cells(browser): + pass From a0e89b618d7f75243e3540f85f5331dafdb6a0ff Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 31 Mar 2018 16:58:47 -0400 Subject: [PATCH 09/14] Add tests for deletable cells and non-deleteable cells --- notebook/tests/selenium/test_deletecell.py | 67 ++++++++++++++++++---- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index f8c8d98b0..7d6a96a69 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,16 +1,63 @@ import os +import pytest +from .utils import Notebook -from selenium.webdriver.common.by import By -from selenium.webdriver.support.ui import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC +def get_cells_contents(nb): + JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' + return nb.browser.execute_script(JS) -pjoin = os.path.join +def set_cell_metadata(nb, index, key, value): + JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) + return nb.browser.execute_script(JS) -def cell_is_deletable(index): - pass +def cell_is_deletable(nb, index): + JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index) + return nb.browser.execute_script(JS) -def test_deletable_cells(browser): - pass +def delete_cell(notebook, index): + notebook.focus_cell(index) + notebook.to_command_mode + notebook.current_cell.send_keys('dd') -def test_non_deletable_cells(browser): - pass +def test_delete_cells(notebook): + print('testing deleteable cells') + a = 'print("a")' + b = 'print("b")' + c = 'print("c")' + + notebook.edit_cell(index=0, content=a) + notebook.append(b, c) + notebook.to_command_mode() + + # Validate initial state + assert get_cells_contents(notebook) == [a, b, c] + for cell in range(0, 3): + assert cell_is_deletable(notebook, cell) + + set_cell_metadata(notebook, 0, 'deletable', 'false') + set_cell_metadata(notebook, 1, 'deletable', 0 + ) + assert not cell_is_deletable(notebook, 0) + assert cell_is_deletable(notebook, 1) + assert cell_is_deletable(notebook, 2) + + # Try to delete cell a (should not be deleted) + delete_cell(notebook, 0) + assert get_cells_contents(notebook) == [a, b, c] + + # Try to delete cell b (should succeed) + delete_cell(notebook, 1) + assert get_cells_contents(notebook) == [a, c] + + # Try to delete cell c + delete_cell(notebook, 1) + assert get_cells_contents(notebook) == [a] + + # Change the deletable state of cell a + set_cell_metadata(notebook, 0, 'deletable', 'true') + + # Try to delete cell a (should succeed) + delete_cell(notebook, 0) + assert len(notebook.cells) == 1 # it contains an empty cell +# NOTE: APIs that will be useful for testing +# http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From 2ef45d5577f0bbea2e3d8ff31fa344c4983920e3 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Sat, 31 Mar 2018 17:04:51 -0400 Subject: [PATCH 10/14] Add tests to make sure copied cells are deletable --- notebook/tests/selenium/test_deletecell.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 7d6a96a69..68d162bf5 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -59,5 +59,15 @@ def test_delete_cells(notebook): # Try to delete cell a (should succeed) delete_cell(notebook, 0) assert len(notebook.cells) == 1 # it contains an empty cell + + # Make sure copied cells are deletable + notebook.edit_cell(index=0, content=a) + set_cell_metadata(notebook, 0, 'deletable', 'false') + assert not cell_is_deletable(notebook, 0) + notebook.to_command_mode() + notebook.current_cell.send_keys('cv') + assert len(notebook.cells) == 2 + assert cell_is_deletable(notebook, 1) + # NOTE: APIs that will be useful for testing # http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From a47ecd4c074c8b5d2054a8c145cb385adf1f2fda Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 2 Apr 2018 16:49:02 -0400 Subject: [PATCH 11/14] Move notebook fixture to deletetest file to minimize the dependency on changes in PR #3475. This should be update once that PR is merged --- notebook/tests/selenium/test_deletecell.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 68d162bf5..0b7efb975 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -2,6 +2,10 @@ import os import pytest from .utils import Notebook +@pytest.fixture +def notebook(authenticated_browser): + return Notebook.new_notebook(authenticated_browser) + def get_cells_contents(nb): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' return nb.browser.execute_script(JS) From 5e57a510557df86fa4304db55a10c773802ca113 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Mon, 2 Apr 2018 17:21:50 -0400 Subject: [PATCH 12/14] Remove unnecessary comments --- notebook/tests/selenium/test_deletecell.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 0b7efb975..6b297fd31 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -53,7 +53,7 @@ def test_delete_cells(notebook): delete_cell(notebook, 1) assert get_cells_contents(notebook) == [a, c] - # Try to delete cell c + # Try to delete cell c (should succeed) delete_cell(notebook, 1) assert get_cells_contents(notebook) == [a] @@ -72,6 +72,3 @@ def test_delete_cells(notebook): notebook.current_cell.send_keys('cv') assert len(notebook.cells) == 2 assert cell_is_deletable(notebook, 1) - -# NOTE: APIs that will be useful for testing -# http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains From b097da2867ff24a55e36691f24362385b67e938c Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Tue, 3 Apr 2018 16:23:23 -0400 Subject: [PATCH 13/14] Delete tests Update to use the updated notebook fixture --- notebook/tests/notebook/deletecell.js | 107 --------------------- notebook/tests/selenium/test_deletecell.py | 6 -- 2 files changed, 113 deletions(-) delete mode 100644 notebook/tests/notebook/deletecell.js diff --git a/notebook/tests/notebook/deletecell.js b/notebook/tests/notebook/deletecell.js deleted file mode 100644 index 6c98f28cc..000000000 --- a/notebook/tests/notebook/deletecell.js +++ /dev/null @@ -1,107 +0,0 @@ - -// Test -casper.notebook_test(function () { - var that = this; - var cell_is_deletable = function (index) { - // Get the deletable status of a cell. - return that.evaluate(function (index) { - var cell = IPython.notebook.get_cell(index); - return cell.is_deletable(); - }, index); - }; - - var a = 'print("a")'; - var index = this.append_cell(a); - - var b = 'print("b")'; - index = this.append_cell(b); - - var c = 'print("c")'; - index = this.append_cell(c); - - this.thenEvaluate(function() { - IPython.notebook.get_cell(1).metadata.deletable = false; - IPython.notebook.get_cell(2).metadata.deletable = 0; // deletable only when exactly false - IPython.notebook.get_cell(3).metadata.deletable = true; - }); - - this.then(function () { - // Check deletable status of the cells - this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable'); - this.test.assert(!cell_is_deletable(1), 'Cell 1 is not deletable'); - this.test.assert(cell_is_deletable(2), 'Cell 2 is deletable'); - this.test.assert(cell_is_deletable(3), 'Cell 3 is deletable'); - }); - - // Try to delete cell 0 (should succeed) - this.then(function () { - this.select_cell(0); - this.trigger_keydown('esc'); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are now 3 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 1 is now cell 0'); - this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 2 is now cell 1'); - this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 3 is now cell 2'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Try to delete cell 0 (should fail) - this.then(function () { - this.select_cell(0); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are still 3 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 0 was not deleted'); - this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 1 was not affected'); - this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 2 was not affected'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Try to delete cell 1 (should succeed) - this.then(function () { - this.select_cell(1); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 2, 'Delete cell 1: There are now 2 cells'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 1: Cell 0 was not affected'); - this.test.assertEquals(this.get_cell_text(1), c, 'Delete cell 1: Cell 1 was not affected'); - this.validate_notebook_state('dd', 'command', 1); - }); - - // Try to delete cell 1 (should succeed) - this.then(function () { - this.select_cell(1); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 1, 'Delete cell 1: There is now 1 cell'); - this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 2: Cell 0 was not affected'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Change the deletable status of the last cells - this.thenEvaluate(function() { - IPython.notebook.get_cell(0).metadata.deletable = true; - }); - - this.then(function () { - // Check deletable status of the cell - this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable'); - - // Try to delete the last cell (should succeed) - this.select_cell(0); - this.trigger_keydown('d', 'd'); - this.test.assertEquals(this.get_cells_length(), 1, 'Delete last cell: There is still 1 cell'); - this.test.assertEquals(this.get_cell_text(0), "", 'Delete last cell: Cell 0 was deleted'); - this.validate_notebook_state('dd', 'command', 0); - }); - - // Make sure copied cells are deletable - this.thenEvaluate(function() { - IPython.notebook.get_cell(0).metadata.deletable = false; - }); - this.then(function () { - this.select_cell(0); - this.trigger_keydown('c', 'v'); - this.test.assertEquals(this.get_cells_length(), 2, 'Copy cell: There are 2 cells'); - this.test.assert(!cell_is_deletable(0), 'Cell 0 is not deletable'); - this.test.assert(cell_is_deletable(1), 'Cell 1 is deletable'); - this.validate_notebook_state('cv', 'command', 1); - }); -}); diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 6b297fd31..60f7d8127 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,10 +1,5 @@ import os import pytest -from .utils import Notebook - -@pytest.fixture -def notebook(authenticated_browser): - return Notebook.new_notebook(authenticated_browser) def get_cells_contents(nb): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' @@ -24,7 +19,6 @@ def delete_cell(notebook, index): notebook.current_cell.send_keys('dd') def test_delete_cells(notebook): - print('testing deleteable cells') a = 'print("a")' b = 'print("b")' c = 'print("c")' From bdfdd24c7e9bad8a2b3f32d78df9631ad259c321 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Wed, 4 Apr 2018 10:39:11 -0400 Subject: [PATCH 14/14] Move some utility functions from the test module to the utils.py module --- notebook/tests/selenium/test_deletecell.py | 24 ++++++++-------------- notebook/tests/selenium/utils.py | 8 ++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/notebook/tests/selenium/test_deletecell.py b/notebook/tests/selenium/test_deletecell.py index 60f7d8127..388dd2ad8 100644 --- a/notebook/tests/selenium/test_deletecell.py +++ b/notebook/tests/selenium/test_deletecell.py @@ -1,14 +1,6 @@ import os import pytest -def get_cells_contents(nb): - JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})' - return nb.browser.execute_script(JS) - -def set_cell_metadata(nb, index, key, value): - JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) - return nb.browser.execute_script(JS) - def cell_is_deletable(nb, index): JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index) return nb.browser.execute_script(JS) @@ -28,12 +20,12 @@ def test_delete_cells(notebook): notebook.to_command_mode() # Validate initial state - assert get_cells_contents(notebook) == [a, b, c] + assert notebook.get_cells_contents() == [a, b, c] for cell in range(0, 3): assert cell_is_deletable(notebook, cell) - set_cell_metadata(notebook, 0, 'deletable', 'false') - set_cell_metadata(notebook, 1, 'deletable', 0 + notebook.set_cell_metadata(0, 'deletable', 'false') + notebook.set_cell_metadata(1, 'deletable', 0 ) assert not cell_is_deletable(notebook, 0) assert cell_is_deletable(notebook, 1) @@ -41,18 +33,18 @@ def test_delete_cells(notebook): # Try to delete cell a (should not be deleted) delete_cell(notebook, 0) - assert get_cells_contents(notebook) == [a, b, c] + assert notebook.get_cells_contents() == [a, b, c] # Try to delete cell b (should succeed) delete_cell(notebook, 1) - assert get_cells_contents(notebook) == [a, c] + assert notebook.get_cells_contents() == [a, c] # Try to delete cell c (should succeed) delete_cell(notebook, 1) - assert get_cells_contents(notebook) == [a] + assert notebook.get_cells_contents() == [a] # Change the deletable state of cell a - set_cell_metadata(notebook, 0, 'deletable', 'true') + notebook.set_cell_metadata(0, 'deletable', 'true') # Try to delete cell a (should succeed) delete_cell(notebook, 0) @@ -60,7 +52,7 @@ def test_delete_cells(notebook): # Make sure copied cells are deletable notebook.edit_cell(index=0, content=a) - set_cell_metadata(notebook, 0, 'deletable', 'false') + notebook.set_cell_metadata(0, 'deletable', 'false') assert not cell_is_deletable(notebook, 0) notebook.to_command_mode() notebook.current_cell.send_keys('cv') diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index efe49fb4c..ef4092772 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -127,6 +127,14 @@ class Notebook: wait = WebDriverWait(self.browser, 10) element = wait.until(EC.staleness_of(cell)) + 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 set_cell_metadata(self, index, key, value): + JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) + 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 """