From 3cdc4187997f55cf19504bf4ad166d9cd628a336 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 8 May 2019 11:07:11 -0700 Subject: [PATCH 01/13] Add alt command, gets, and validate_notebook_state --- notebook/tests/selenium/utils.py | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 4b16f92ee..89cd47aed 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -222,6 +222,14 @@ class Notebook: JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) return self.browser.execute_script(JS) + def get_cells_mode(self): + JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' + return self.browser.execute_script(JS) + + def get_cells_length(self): + JS = 'return IPython.notebook.get_cells().length;' + 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) @@ -363,6 +371,10 @@ def cmdtrl(browser, k): """Send key combination Ctrl+(k) or Command+(k) for MacOS""" trigger_keystrokes(browser, "command-%s"%k) if os.uname()[0] == "Darwin" else trigger_keystrokes(browser, "control-%s"%k) +def alt(browser, k): + """Send key combination Alt+(k)""" + trigger_keystrokes(browser, 'alt-%s'%k) + def trigger_keystrokes(browser, *keys): """ Send the keys in sequence to the browser. Handles following key combinations @@ -382,3 +394,71 @@ def trigger_keystrokes(browser, *keys): ac.perform() else: # single key stroke. Check if modifier eg. "up" browser.send_keys(getattr(Keys, keys[0].upper(), keys[0])) + +def validate_notebook_state(notebook, mode, index): + def is_only_cell_edit(index): + cells_mode = notebook.get_cells_mode() + #None of the cells are in edit mode + if(index == None): + for mode in cells_mode: + if(mode == 'edit'): + return False + return True + #Only the index cell is on edit mode + i = 0 + for mode in cells_mode: + if(i == index): + if(mode != 'edit'): + return False + else: + if(mode == 'edit'): + return False + i += 1 + return True + + def is_focused_on(index): + JS = "return $('#notebook .CodeMirror-focused textarea').length;" + focused_cells = notebook.browser.execute_script(JS) + if(index == None): + return focused_cells == 0 + assert focused_cells == 1 + JS = "return $('#notebook .CodeMirror-focused textarea')[0];" + focused_cell = notebook.browser.execute_script(JS) + JS = "return IPython.notebook.get_cell(%s).code_mirror.getInputField()"%index + cell = notebook.browser.execute_script(JS) + assert focused_cell == cell + + + #general test + JS = "return IPython.keyboard_manager.mode;" + keyboard_mode = notebook.browser.execute_script(JS) + JS = "return IPython.notebook.mode;" + notebook_mode = notebook.browser.execute_script(JS) + + #validate selected cell + if (index != None): + JS = "return Jupyter.notebook.get_selected_cells_indices();" + cell_index = notebook.browser.execute_script(JS) + assert cell_index == [index] #only the index cell is selected + + if(mode == 'command'): + #validate mode + assert mode == keyboard_mode #keyboard mode is correct + + assert is_focused_on(None)#no focused cells + + assert is_only_cell_edit(None)#no cells in edit mode + + elif(mode == 'edit'): + #Are the notebook and keyboard manager in edit mode? + assert mode == keyboard_mode #keyboard mode is correct + + is_focused_on(index)#The specified cell is focused + + assert is_only_cell_edit(index) #The specified cell is the only one in edit mode + + else: + assert False #An unknown mode is send + + + \ No newline at end of file From 5b162cfdf9aa9e7e0261e5ad1f740426956d81df Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 8 May 2019 11:08:09 -0700 Subject: [PATCH 02/13] Convert dualmode_clipboard to selenium --- notebook/tests/notebook/dualmode_clipboard.js | 55 ----------------- .../tests/selenium/test_dualmode_clipboard.py | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 55 deletions(-) delete mode 100644 notebook/tests/notebook/dualmode_clipboard.js create mode 100644 notebook/tests/selenium/test_dualmode_clipboard.py diff --git a/notebook/tests/notebook/dualmode_clipboard.js b/notebook/tests/notebook/dualmode_clipboard.js deleted file mode 100644 index b7fe9857f..000000000 --- a/notebook/tests/notebook/dualmode_clipboard.js +++ /dev/null @@ -1,55 +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.then(function () { - // Copy/paste/cut - var num_cells = this.get_cells_length(); - this.test.assertEquals(this.get_cell_text(1), a, 'Verify that cell 1 is a'); - this.select_cell(1); - this.trigger_keydown('x'); // Cut - this.validate_notebook_state('x', 'command', 1); - this.test.assertEquals(this.get_cells_length(), num_cells-1, 'Verify that a cell was removed.'); - this.test.assertEquals(this.get_cell_text(1), b, 'Verify that cell 2 is now where cell 1 was.'); - this.select_cell(2); - this.trigger_keydown('v'); // Paste - this.validate_notebook_state('v', 'command', 3); // Selection should move to pasted cell, below current cell. - this.test.assertEquals(this.get_cell_text(3), a, 'Verify that cell 3 has the cut contents.'); - this.test.assertEquals(this.get_cells_length(), num_cells, 'Verify a the cell was added.'); - this.trigger_keydown('v'); // Paste - this.validate_notebook_state('v', 'command', 4); // Selection should move to pasted cell, below current cell. - this.test.assertEquals(this.get_cell_text(4), a, 'Verify that cell 4 has the cut contents.'); - this.test.assertEquals(this.get_cells_length(), num_cells+1, 'Verify a the cell was added.'); - this.select_cell(1); - this.trigger_keydown('c'); // Copy - this.validate_notebook_state('c', 'command', 1); - this.test.assertEquals(this.get_cell_text(1), b, 'Verify that cell 1 is b'); - this.select_cell(2); - this.trigger_keydown('c'); // Copy - this.validate_notebook_state('c', 'command', 2); - this.test.assertEquals(this.get_cell_text(2), c, 'Verify that cell 2 is c'); - this.select_cell(4); - this.trigger_keydown('v'); // Paste - this.validate_notebook_state('v', 'command', 5); - this.test.assertEquals(this.get_cell_text(2), c, 'Verify that cell 2 still has the copied contents.'); - this.test.assertEquals(this.get_cell_text(5), c, 'Verify that cell 5 has the copied contents.'); - this.test.assertEquals(this.get_cells_length(), num_cells+2, 'Verify a the cell was added.'); - this.select_cell(0); - this.trigger_keydown('shift-v'); // Paste - this.validate_notebook_state('shift-v', 'command', 0); - this.test.assertEquals(this.get_cell_text(0), c, 'Verify that cell 0 has the copied contents.'); - this.test.assertEquals(this.get_cells_length(), num_cells+3, 'Verify a the cell was added.'); - }); -}); diff --git a/notebook/tests/selenium/test_dualmode_clipboard.py b/notebook/tests/selenium/test_dualmode_clipboard.py new file mode 100644 index 000000000..729f1a595 --- /dev/null +++ b/notebook/tests/selenium/test_dualmode_clipboard.py @@ -0,0 +1,59 @@ +"""Test""" +from .utils import shift, validate_notebook_state + +def test_dualmode_clipboard(notebook): + a = 'print("a")' + notebook.append(a) + notebook.execute_cell(1) + + b = 'print("b")' + notebook.append(b) + notebook.execute_cell(2) + + c = 'print("c")' + notebook.append(c) + notebook.execute_cell(3) + + #Copy/past/cut + num_cells = len(notebook.cells) + assert notebook.get_cell_contents(1) == a #Cell 1 is a + + notebook.focus_cell(1) + notebook.body.send_keys("x") #Cut + validate_notebook_state(notebook, 'command', 1) + assert notebook.get_cell_contents(1) == b #Cell 2 is now where cell 1 was + assert len(notebook.cells) == num_cells-1 #A cell was removed + + notebook.focus_cell(2) + notebook.body.send_keys("v") #Paste + validate_notebook_state(notebook, 'command', 3) + assert notebook.get_cell_contents(3) == a #Cell 3 has the cut contents + assert len(notebook.cells) == num_cells #A cell was added + + notebook.body.send_keys("v") #Paste + validate_notebook_state(notebook, 'command', 4) + assert notebook.get_cell_contents(4) == a #Cell a has the cut contents + assert len(notebook.cells) == num_cells+1 #A cell was added + + notebook.focus_cell(1) + notebook.body.send_keys("c") #Copy + validate_notebook_state(notebook, 'command', 1) + assert notebook.get_cell_contents(1) == b #Cell 1 is b + + notebook.focus_cell(2) + notebook.body.send_keys("c") #Copy + validate_notebook_state(notebook, 'command', 2) + assert notebook.get_cell_contents(2) == c #Cell 2 is c + + notebook.focus_cell(4) + notebook.body.send_keys("v") #Paste + validate_notebook_state(notebook, 'command', 5) + assert notebook.get_cell_contents(2) == c #Cell 2 has the copied contents + assert notebook.get_cell_contents(5) == c #Cell 5 has the copied contents + assert len(notebook.cells) == num_cells+2 #A cell was added + + notebook.focus_cell(0) + shift(notebook.browser, 'v') #Paste + validate_notebook_state(notebook, 'command', 0) + assert notebook.get_cell_contents(0) == c #Cell 0 has the copied contents + assert len(notebook.cells) == num_cells+3 #A cell was added \ No newline at end of file From 9158a79ec4443dfe0f3ed84068b3d079e88cead5 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 8 May 2019 11:08:31 -0700 Subject: [PATCH 03/13] Convert dualmode_execute to selenium --- notebook/tests/notebook/dualmode_execute.js | 72 ----------------- .../tests/selenium/test_dualmode_execute.py | 81 +++++++++++++++++++ 2 files changed, 81 insertions(+), 72 deletions(-) delete mode 100644 notebook/tests/notebook/dualmode_execute.js create mode 100644 notebook/tests/selenium/test_dualmode_execute.py diff --git a/notebook/tests/notebook/dualmode_execute.js b/notebook/tests/notebook/dualmode_execute.js deleted file mode 100644 index f4cd9542f..000000000 --- a/notebook/tests/notebook/dualmode_execute.js +++ /dev/null @@ -1,72 +0,0 @@ -// Test keyboard invoked execution. - -// 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.then(function () { - - // shift-enter - // last cell in notebook - var base_index = 3; - this.select_cell(base_index); - this.trigger_keydown('shift-enter'); // Creates one cell - this.validate_notebook_state('shift-enter (no cell below)', 'edit', base_index + 1); - // not last cell in notebook & starts in edit mode - this.click_cell_editor(base_index); - this.validate_notebook_state('click cell ' + base_index, 'edit', base_index); - this.trigger_keydown('shift-enter'); - this.validate_notebook_state('shift-enter (cell exists below)', 'command', base_index + 1); - // starts in command mode - this.trigger_keydown('k'); - this.validate_notebook_state('k in comand mode', 'command', base_index); - this.trigger_keydown('shift-enter'); - this.validate_notebook_state('shift-enter (start in command mode)', 'command', base_index + 1); - - // ctrl-enter - // last cell in notebook - base_index++; - this.trigger_keydown('ctrl-enter'); - this.validate_notebook_state('ctrl-enter (no cell below)', 'command', base_index); - // not last cell in notebook & starts in edit mode - this.click_cell_editor(base_index-1); - this.validate_notebook_state('click cell ' + (base_index-1), 'edit', base_index-1); - this.trigger_keydown('ctrl-enter'); - this.validate_notebook_state('ctrl-enter (cell exists below)', 'command', base_index-1); - // starts in command mode - this.trigger_keydown('j'); - this.validate_notebook_state('j in comand mode', 'command', base_index); - this.trigger_keydown('ctrl-enter'); - this.validate_notebook_state('ctrl-enter (start in command mode)', 'command', base_index); - - // alt-enter - // last cell in notebook - this.trigger_keydown('alt-enter'); // Creates one cell - this.validate_notebook_state('alt-enter (no cell below)', 'edit', base_index + 1); - // not last cell in notebook & starts in edit mode - this.click_cell_editor(base_index); - this.validate_notebook_state('click cell ' + base_index, 'edit', base_index); - this.trigger_keydown('alt-enter'); // Creates one cell - this.validate_notebook_state('alt-enter (cell exists below)', 'edit', base_index + 1); - // starts in command mode - this.trigger_keydown('esc', 'k'); - this.validate_notebook_state('k in comand mode', 'command', base_index); - this.trigger_keydown('alt-enter'); // Creates one cell - this.validate_notebook_state('alt-enter (start in command mode)', 'edit', base_index + 1); - - // Notebook will now have 8 cells, the index of the last cell will be 7. - this.test.assertEquals(this.get_cells_length(), 8, '*-enter commands added cells where needed.'); - this.select_cell(7); - this.validate_notebook_state('click cell ' + 7 + ' and esc', 'command', 7); - }); -}); \ No newline at end of file diff --git a/notebook/tests/selenium/test_dualmode_execute.py b/notebook/tests/selenium/test_dualmode_execute.py new file mode 100644 index 000000000..e3cae9039 --- /dev/null +++ b/notebook/tests/selenium/test_dualmode_execute.py @@ -0,0 +1,81 @@ +''' Test keyboard invoked execution ''' + +from selenium.webdriver.common.keys import Keys + +from .utils import shift, cmdtrl, alt, validate_notebook_state + + +def test_dualmode_execute(notebook): + a = 'print("a")' + notebook.append(a) + notebook.execute_cell(1) + + b = 'print("b")' + notebook.append(b) + notebook.execute_cell(2) + + c = 'print("c")' + notebook.append(c) + notebook.execute_cell(3) + + #shift-enter + #last cell in notebook + base_index = 3 + notebook.focus_cell(base_index) + shift(notebook.browser, Keys.ENTER) #creates one cell + validate_notebook_state(notebook, 'edit', base_index + 1) + + #Not last cell in notebook & starts in edit mode + notebook.focus_cell(base_index) + notebook.body.send_keys(Keys.ENTER) #Enter edit mode + validate_notebook_state(notebook, 'edit', base_index) + shift(notebook.browser, Keys.ENTER) #creates one cell + validate_notebook_state(notebook, 'command', base_index + 1) + + #Starts in command mode + notebook.body.send_keys('k') + validate_notebook_state(notebook, 'command', base_index) + shift(notebook.browser, Keys.ENTER) #creates one cell + validate_notebook_state(notebook, 'command', base_index + 1) + + + #Ctrl-enter + #Last cell in notebook + base_index += 1 + cmdtrl(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'command', base_index) + + #Not last cell in notebook & stats in edit mode + notebook.focus_cell(base_index - 1) + notebook.body.send_keys(Keys.ENTER) #Enter edit mode + validate_notebook_state(notebook, 'edit', base_index - 1) + cmdtrl(notebook.browser, Keys.ENTER) + + #Starts in command mode + notebook.body.send_keys('j') + validate_notebook_state(notebook, 'command', base_index) + cmdtrl(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'command', base_index) + + + #Alt-enter + #Last cell in notebook + alt(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'edit', base_index + 1) + #Not last cell in notebook &starts in edit mode + notebook.focus_cell(base_index) + notebook.body.send_keys(Keys.ENTER) #Enter edit mode + validate_notebook_state(notebook, 'edit', base_index) + alt(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'edit', base_index + 1) + #starts in command mode + notebook.body.send_keys(Keys.ESCAPE, 'k') + validate_notebook_state(notebook, 'command', base_index) + alt(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'edit', base_index + 1) + + + #Notebook will now have 8 cells, the index of the last cell will be 7 + assert notebook.get_cells_length() == 8 #Cells where added + notebook.focus_cell(7) + validate_notebook_state(notebook, 'command', 7) From 849b7def18659374ef8b3112067cf14792664403 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 8 May 2019 12:30:11 -0700 Subject: [PATCH 04/13] Convert dualmode_markdown to selenium --- notebook/tests/notebook/dualmode_markdown.js | 37 ------------- .../tests/selenium/test_dualmode_markdown.py | 53 +++++++++++++++++++ 2 files changed, 53 insertions(+), 37 deletions(-) delete mode 100644 notebook/tests/notebook/dualmode_markdown.js create mode 100644 notebook/tests/selenium/test_dualmode_markdown.py diff --git a/notebook/tests/notebook/dualmode_markdown.js b/notebook/tests/notebook/dualmode_markdown.js deleted file mode 100644 index c9ecd6df7..000000000 --- a/notebook/tests/notebook/dualmode_markdown.js +++ /dev/null @@ -1,37 +0,0 @@ - -// Test -casper.notebook_test(function () { - var a = 'print("a")'; - var index = this.append_cell(a); - this.execute_cell_then(index, function(index) { - // Markdown rendering / unredering - this.select_cell(index); - this.validate_notebook_state('select ' + index, 'command', index); - this.trigger_keydown('m'); - this.test.assertEquals(this.get_cell(index).cell_type, 'markdown', 'm; cell is markdown'); - this.test.assert(!this.is_cell_rendered(index), 'm; cell is unrendered'); - this.trigger_keydown('enter'); - this.test.assert(!this.is_cell_rendered(index), 'enter; cell is unrendered'); - this.validate_notebook_state('enter', 'edit', index); - this.trigger_keydown('ctrl-enter'); - this.test.assert(this.is_cell_rendered(index), 'ctrl-enter; cell is rendered'); - this.validate_notebook_state('enter', 'command', index); - this.trigger_keydown('enter'); - this.test.assert(!this.is_cell_rendered(index), 'enter; cell is unrendered'); - this.select_cell(index-1); - this.test.assert(!this.is_cell_rendered(index), 'select ' + (index-1) + '; cell ' + index + ' is still unrendered'); - this.validate_notebook_state('select ' + (index-1), 'command', index-1); - this.select_cell(index); - this.validate_notebook_state('select ' + index, 'command', index); - this.trigger_keydown('ctrl-enter'); - this.test.assert(this.is_cell_rendered(index), 'ctrl-enter; cell is rendered'); - this.select_cell(index-1); - this.validate_notebook_state('select ' + (index-1), 'command', index-1); - this.trigger_keydown('shift-enter'); - this.validate_notebook_state('shift-enter', 'command', index); - this.test.assert(this.is_cell_rendered(index), 'shift-enter; cell is rendered'); - this.trigger_keydown('shift-enter'); // Creates one cell - this.validate_notebook_state('shift-enter', 'edit', index+1); - this.test.assert(this.is_cell_rendered(index), 'shift-enter; cell is rendered'); - }); -}); \ No newline at end of file diff --git a/notebook/tests/selenium/test_dualmode_markdown.py b/notebook/tests/selenium/test_dualmode_markdown.py new file mode 100644 index 000000000..b982ad6ed --- /dev/null +++ b/notebook/tests/selenium/test_dualmode_markdown.py @@ -0,0 +1,53 @@ +'''Test''' + +from selenium.webdriver.common.keys import Keys + +from .utils import cmdtrl, shift, validate_notebook_state + +def test_dualmode_markdown(notebook): + def is_cell_rendered(index): + JS = 'return !!IPython.notebook.get_cell(%s).rendered;'%index + return notebook.browser.execute_script(JS) + + + a = 'print("a")' + index = 1 + notebook.append(a) + + #Markdown rendering / unrendering + notebook.focus_cell(index) + validate_notebook_state(notebook, 'command', index) + notebook.body.send_keys("m") + assert notebook.get_cell_type(index) == 'markdown' + assert not is_cell_rendered(index) #cell is not rendered + + notebook.body.send_keys(Keys.ENTER)#cell is unrendered + assert not is_cell_rendered(index) #cell is not rendered + validate_notebook_state(notebook, 'edit', index) + + cmdtrl(notebook.browser, Keys.ENTER) + assert is_cell_rendered(index) #cell is rendered with crtl+enter + validate_notebook_state(notebook, 'command', index) + + notebook.body.send_keys(Keys.ENTER)#cell is unrendered + assert not is_cell_rendered(index) #cell is not rendered + + notebook.focus_cell(index - 1) + assert not is_cell_rendered(index) #Select index-1; cell index is still not rendered + validate_notebook_state(notebook, 'command', index - 1) + + notebook.focus_cell(index) + validate_notebook_state(notebook, 'command', index) + cmdtrl(notebook.browser, Keys.ENTER) + assert is_cell_rendered(index)#Cell is rendered + + notebook.focus_cell(index - 1) + validate_notebook_state(notebook, 'command', index - 1) + + shift(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'command', index) + assert is_cell_rendered(index)#Cell is rendered + + shift(notebook.browser, Keys.ENTER) + validate_notebook_state(notebook, 'edit', index + 1) + assert is_cell_rendered(index)#Cell is rendered From fccc7d7420bc5ffe97cfc3c2da0cebc59828c3d4 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 15 May 2019 08:32:06 -0700 Subject: [PATCH 05/13] Resolve notebook len duplicate method --- notebook/tests/selenium/test_dualmode_execute.py | 2 +- notebook/tests/selenium/utils.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_execute.py b/notebook/tests/selenium/test_dualmode_execute.py index e3cae9039..fc2d40cd4 100644 --- a/notebook/tests/selenium/test_dualmode_execute.py +++ b/notebook/tests/selenium/test_dualmode_execute.py @@ -76,6 +76,6 @@ def test_dualmode_execute(notebook): #Notebook will now have 8 cells, the index of the last cell will be 7 - assert notebook.get_cells_length() == 8 #Cells where added + assert len(notebook) == 8 #Cells where added notebook.focus_cell(7) validate_notebook_state(notebook, 'command', 7) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 89cd47aed..97478e4f0 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -226,10 +226,6 @@ class Notebook: JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' return self.browser.execute_script(JS) - def get_cells_length(self): - JS = 'return IPython.notebook.get_cells().length;' - 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) From 3a0cb7b11350747b0413bab3d08492450fbdf031 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Wed, 15 May 2019 13:36:44 -0700 Subject: [PATCH 06/13] Refactor validate_dualmode_mode Previously know as validate_notebook_mode changed name to validate_dualmode_mode to represent better the method. Added a docstring. Removed the handling of index being None. --- .../tests/selenium/test_dualmode_clipboard.py | 16 +++++----- .../tests/selenium/test_dualmode_execute.py | 32 +++++++++---------- .../tests/selenium/test_dualmode_markdown.py | 18 +++++------ notebook/tests/selenium/utils.py | 22 +++++++------ 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/notebook/tests/selenium/test_dualmode_clipboard.py b/notebook/tests/selenium/test_dualmode_clipboard.py index 729f1a595..bec4a4657 100644 --- a/notebook/tests/selenium/test_dualmode_clipboard.py +++ b/notebook/tests/selenium/test_dualmode_clipboard.py @@ -1,5 +1,5 @@ """Test""" -from .utils import shift, validate_notebook_state +from .utils import shift, validate_dualmode_state def test_dualmode_clipboard(notebook): a = 'print("a")' @@ -20,40 +20,40 @@ def test_dualmode_clipboard(notebook): notebook.focus_cell(1) notebook.body.send_keys("x") #Cut - validate_notebook_state(notebook, 'command', 1) + validate_dualmode_state(notebook, 'command', 1) assert notebook.get_cell_contents(1) == b #Cell 2 is now where cell 1 was assert len(notebook.cells) == num_cells-1 #A cell was removed notebook.focus_cell(2) notebook.body.send_keys("v") #Paste - validate_notebook_state(notebook, 'command', 3) + validate_dualmode_state(notebook, 'command', 3) assert notebook.get_cell_contents(3) == a #Cell 3 has the cut contents assert len(notebook.cells) == num_cells #A cell was added notebook.body.send_keys("v") #Paste - validate_notebook_state(notebook, 'command', 4) + validate_dualmode_state(notebook, 'command', 4) assert notebook.get_cell_contents(4) == a #Cell a has the cut contents assert len(notebook.cells) == num_cells+1 #A cell was added notebook.focus_cell(1) notebook.body.send_keys("c") #Copy - validate_notebook_state(notebook, 'command', 1) + validate_dualmode_state(notebook, 'command', 1) assert notebook.get_cell_contents(1) == b #Cell 1 is b notebook.focus_cell(2) notebook.body.send_keys("c") #Copy - validate_notebook_state(notebook, 'command', 2) + validate_dualmode_state(notebook, 'command', 2) assert notebook.get_cell_contents(2) == c #Cell 2 is c notebook.focus_cell(4) notebook.body.send_keys("v") #Paste - validate_notebook_state(notebook, 'command', 5) + validate_dualmode_state(notebook, 'command', 5) assert notebook.get_cell_contents(2) == c #Cell 2 has the copied contents assert notebook.get_cell_contents(5) == c #Cell 5 has the copied contents assert len(notebook.cells) == num_cells+2 #A cell was added notebook.focus_cell(0) shift(notebook.browser, 'v') #Paste - validate_notebook_state(notebook, 'command', 0) + validate_dualmode_state(notebook, 'command', 0) assert notebook.get_cell_contents(0) == c #Cell 0 has the copied contents assert len(notebook.cells) == num_cells+3 #A cell was added \ No newline at end of file diff --git a/notebook/tests/selenium/test_dualmode_execute.py b/notebook/tests/selenium/test_dualmode_execute.py index fc2d40cd4..4b646bb52 100644 --- a/notebook/tests/selenium/test_dualmode_execute.py +++ b/notebook/tests/selenium/test_dualmode_execute.py @@ -2,7 +2,7 @@ from selenium.webdriver.common.keys import Keys -from .utils import shift, cmdtrl, alt, validate_notebook_state +from .utils import shift, cmdtrl, alt, validate_dualmode_state def test_dualmode_execute(notebook): @@ -23,59 +23,59 @@ def test_dualmode_execute(notebook): base_index = 3 notebook.focus_cell(base_index) shift(notebook.browser, Keys.ENTER) #creates one cell - validate_notebook_state(notebook, 'edit', base_index + 1) + validate_dualmode_state(notebook, 'edit', base_index + 1) #Not last cell in notebook & starts in edit mode notebook.focus_cell(base_index) notebook.body.send_keys(Keys.ENTER) #Enter edit mode - validate_notebook_state(notebook, 'edit', base_index) + validate_dualmode_state(notebook, 'edit', base_index) shift(notebook.browser, Keys.ENTER) #creates one cell - validate_notebook_state(notebook, 'command', base_index + 1) + validate_dualmode_state(notebook, 'command', base_index + 1) #Starts in command mode notebook.body.send_keys('k') - validate_notebook_state(notebook, 'command', base_index) + validate_dualmode_state(notebook, 'command', base_index) shift(notebook.browser, Keys.ENTER) #creates one cell - validate_notebook_state(notebook, 'command', base_index + 1) + validate_dualmode_state(notebook, 'command', base_index + 1) #Ctrl-enter #Last cell in notebook base_index += 1 cmdtrl(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'command', base_index) + validate_dualmode_state(notebook, 'command', base_index) #Not last cell in notebook & stats in edit mode notebook.focus_cell(base_index - 1) notebook.body.send_keys(Keys.ENTER) #Enter edit mode - validate_notebook_state(notebook, 'edit', base_index - 1) + validate_dualmode_state(notebook, 'edit', base_index - 1) cmdtrl(notebook.browser, Keys.ENTER) #Starts in command mode notebook.body.send_keys('j') - validate_notebook_state(notebook, 'command', base_index) + validate_dualmode_state(notebook, 'command', base_index) cmdtrl(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'command', base_index) + validate_dualmode_state(notebook, 'command', base_index) #Alt-enter #Last cell in notebook alt(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'edit', base_index + 1) + validate_dualmode_state(notebook, 'edit', base_index + 1) #Not last cell in notebook &starts in edit mode notebook.focus_cell(base_index) notebook.body.send_keys(Keys.ENTER) #Enter edit mode - validate_notebook_state(notebook, 'edit', base_index) + validate_dualmode_state(notebook, 'edit', base_index) alt(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'edit', base_index + 1) + validate_dualmode_state(notebook, 'edit', base_index + 1) #starts in command mode notebook.body.send_keys(Keys.ESCAPE, 'k') - validate_notebook_state(notebook, 'command', base_index) + validate_dualmode_state(notebook, 'command', base_index) alt(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'edit', base_index + 1) + validate_dualmode_state(notebook, 'edit', base_index + 1) #Notebook will now have 8 cells, the index of the last cell will be 7 assert len(notebook) == 8 #Cells where added notebook.focus_cell(7) - validate_notebook_state(notebook, 'command', 7) + validate_dualmode_state(notebook, 'command', 7) diff --git a/notebook/tests/selenium/test_dualmode_markdown.py b/notebook/tests/selenium/test_dualmode_markdown.py index b982ad6ed..af5ce3122 100644 --- a/notebook/tests/selenium/test_dualmode_markdown.py +++ b/notebook/tests/selenium/test_dualmode_markdown.py @@ -2,7 +2,7 @@ from selenium.webdriver.common.keys import Keys -from .utils import cmdtrl, shift, validate_notebook_state +from .utils import cmdtrl, shift, validate_dualmode_state def test_dualmode_markdown(notebook): def is_cell_rendered(index): @@ -16,38 +16,38 @@ def test_dualmode_markdown(notebook): #Markdown rendering / unrendering notebook.focus_cell(index) - validate_notebook_state(notebook, 'command', index) + validate_dualmode_state(notebook, 'command', index) notebook.body.send_keys("m") assert notebook.get_cell_type(index) == 'markdown' assert not is_cell_rendered(index) #cell is not rendered notebook.body.send_keys(Keys.ENTER)#cell is unrendered assert not is_cell_rendered(index) #cell is not rendered - validate_notebook_state(notebook, 'edit', index) + validate_dualmode_state(notebook, 'edit', index) cmdtrl(notebook.browser, Keys.ENTER) assert is_cell_rendered(index) #cell is rendered with crtl+enter - validate_notebook_state(notebook, 'command', index) + validate_dualmode_state(notebook, 'command', index) notebook.body.send_keys(Keys.ENTER)#cell is unrendered assert not is_cell_rendered(index) #cell is not rendered notebook.focus_cell(index - 1) assert not is_cell_rendered(index) #Select index-1; cell index is still not rendered - validate_notebook_state(notebook, 'command', index - 1) + validate_dualmode_state(notebook, 'command', index - 1) notebook.focus_cell(index) - validate_notebook_state(notebook, 'command', index) + validate_dualmode_state(notebook, 'command', index) cmdtrl(notebook.browser, Keys.ENTER) assert is_cell_rendered(index)#Cell is rendered notebook.focus_cell(index - 1) - validate_notebook_state(notebook, 'command', index - 1) + validate_dualmode_state(notebook, 'command', index - 1) shift(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'command', index) + validate_dualmode_state(notebook, 'command', index) assert is_cell_rendered(index)#Cell is rendered shift(notebook.browser, Keys.ENTER) - validate_notebook_state(notebook, 'edit', index + 1) + validate_dualmode_state(notebook, 'edit', index + 1) assert is_cell_rendered(index)#Cell is rendered diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 97478e4f0..efd0d43cf 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -222,10 +222,6 @@ class Notebook: JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value) return self.browser.execute_script(JS) - def get_cells_mode(self): - JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' - 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) @@ -391,9 +387,14 @@ def trigger_keystrokes(browser, *keys): else: # single key stroke. Check if modifier eg. "up" browser.send_keys(getattr(Keys, keys[0].upper(), keys[0])) -def validate_notebook_state(notebook, mode, index): +def validate_dualmode_state(notebook, mode, index): + '''Validate the entire dual mode state of the notebook. + Make sure no more than one cell is selected, focused, in edit mode, etc... + + ''' def is_only_cell_edit(index): - cells_mode = notebook.get_cells_mode() + JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' + cells_mode = notebook.browser.execute_script(JS) #None of the cells are in edit mode if(index == None): for mode in cells_mode: @@ -417,7 +418,9 @@ def validate_notebook_state(notebook, mode, index): focused_cells = notebook.browser.execute_script(JS) if(index == None): return focused_cells == 0 + assert focused_cells == 1 + JS = "return $('#notebook .CodeMirror-focused textarea')[0];" focused_cell = notebook.browser.execute_script(JS) JS = "return IPython.notebook.get_cell(%s).code_mirror.getInputField()"%index @@ -432,10 +435,9 @@ def validate_notebook_state(notebook, mode, index): notebook_mode = notebook.browser.execute_script(JS) #validate selected cell - if (index != None): - JS = "return Jupyter.notebook.get_selected_cells_indices();" - cell_index = notebook.browser.execute_script(JS) - assert cell_index == [index] #only the index cell is selected + JS = "return Jupyter.notebook.get_selected_cells_indices();" + cell_index = notebook.browser.execute_script(JS) + assert cell_index == [index] #only the index cell is selected if(mode == 'command'): #validate mode From c276d819145cab3aa3e151e50d4011c2609c85c3 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Thu, 16 May 2019 09:00:49 -0700 Subject: [PATCH 07/13] Update docstring Added more information about what the checks are --- notebook/tests/selenium/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index efd0d43cf..5507394b8 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -389,8 +389,10 @@ def trigger_keystrokes(browser, *keys): def validate_dualmode_state(notebook, mode, index): '''Validate the entire dual mode state of the notebook. - Make sure no more than one cell is selected, focused, in edit mode, etc... - + Checks if the specified cell is selected, and the mode and keyboard mode are the same. + Depending on the mode given: + Command: Checks that no cells are in focus or in edit mode. + Edit: Checks that only the specified cell is in focus and in edit mode. ''' def is_only_cell_edit(index): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' From b294bf57f86fde6c9b57d3be1c8cb8030d1c08c7 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Thu, 16 May 2019 09:04:58 -0700 Subject: [PATCH 08/13] Refactor of ifs Removed parentheses Changed '==' for 'is' on None --- notebook/tests/selenium/utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 5507394b8..ea5e11669 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -398,19 +398,19 @@ def validate_dualmode_state(notebook, mode, index): JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})' cells_mode = notebook.browser.execute_script(JS) #None of the cells are in edit mode - if(index == None): + if index is None: for mode in cells_mode: - if(mode == 'edit'): + if mode == 'edit': return False return True #Only the index cell is on edit mode i = 0 for mode in cells_mode: - if(i == index): - if(mode != 'edit'): + if i == index: + if mode != 'edit': return False else: - if(mode == 'edit'): + if mode == 'edit': return False i += 1 return True @@ -418,7 +418,7 @@ def validate_dualmode_state(notebook, mode, index): def is_focused_on(index): JS = "return $('#notebook .CodeMirror-focused textarea').length;" focused_cells = notebook.browser.execute_script(JS) - if(index == None): + if index is None: return focused_cells == 0 assert focused_cells == 1 @@ -441,7 +441,7 @@ def validate_dualmode_state(notebook, mode, index): cell_index = notebook.browser.execute_script(JS) assert cell_index == [index] #only the index cell is selected - if(mode == 'command'): + if mode == 'command': #validate mode assert mode == keyboard_mode #keyboard mode is correct @@ -449,7 +449,7 @@ def validate_dualmode_state(notebook, mode, index): assert is_only_cell_edit(None)#no cells in edit mode - elif(mode == 'edit'): + elif mode == 'edit': #Are the notebook and keyboard manager in edit mode? assert mode == keyboard_mode #keyboard mode is correct From 83b3c19de33e38fd508cb95b554c4714b8a14964 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Thu, 16 May 2019 09:12:10 -0700 Subject: [PATCH 09/13] Solve insonsistensy on is_focused_on Changed asserts for returns Changed how the function is called on edit mode check --- notebook/tests/selenium/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index ea5e11669..62f6dff66 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -421,13 +421,14 @@ def validate_dualmode_state(notebook, mode, index): if index is None: return focused_cells == 0 - assert focused_cells == 1 + if focused_cells != 1: #only one cell is focused + return False JS = "return $('#notebook .CodeMirror-focused textarea')[0];" focused_cell = notebook.browser.execute_script(JS) JS = "return IPython.notebook.get_cell(%s).code_mirror.getInputField()"%index cell = notebook.browser.execute_script(JS) - assert focused_cell == cell + return focused_cell == cell #general test @@ -453,7 +454,7 @@ def validate_dualmode_state(notebook, mode, index): #Are the notebook and keyboard manager in edit mode? assert mode == keyboard_mode #keyboard mode is correct - is_focused_on(index)#The specified cell is focused + assert is_focused_on(index)#The specified cell is focused assert is_only_cell_edit(index) #The specified cell is the only one in edit mode From 6b7d179697e889d6bc806b7b6fe9d57096e77300 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Thu, 16 May 2019 09:28:00 -0700 Subject: [PATCH 10/13] Change assert for Exception --- notebook/tests/selenium/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 62f6dff66..1097915e2 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -459,7 +459,7 @@ def validate_dualmode_state(notebook, mode, index): assert is_only_cell_edit(index) #The specified cell is the only one in edit mode else: - assert False #An unknown mode is send + raise Exception('An unknown mode was send: mode = "%s"'%mode) #An unknown mode is send \ No newline at end of file From 6679891464d5d9d0c9ef8d99da86fa44148f1713 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Thu, 16 May 2019 09:31:22 -0700 Subject: [PATCH 11/13] Give space to inline comments --- notebook/tests/selenium/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 1097915e2..0e95d230a 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -446,15 +446,15 @@ def validate_dualmode_state(notebook, mode, index): #validate mode assert mode == keyboard_mode #keyboard mode is correct - assert is_focused_on(None)#no focused cells + assert is_focused_on(None) #no focused cells - assert is_only_cell_edit(None)#no cells in edit mode + assert is_only_cell_edit(None) #no cells in edit mode elif mode == 'edit': #Are the notebook and keyboard manager in edit mode? assert mode == keyboard_mode #keyboard mode is correct - assert is_focused_on(index)#The specified cell is focused + assert is_focused_on(index) #The specified cell is focused assert is_only_cell_edit(index) #The specified cell is the only one in edit mode From 799b8be841a773d311d50d0edb77e6a5f22edd65 Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Fri, 17 May 2019 09:03:15 -0700 Subject: [PATCH 12/13] Move repeated assert mode Moved 'assert mode == keyboard_mode' ouside the branches. This means that if a unknown mode comes its gonna get catched by the assert and never gona get to the else statement. For this, the else stament moved to before the assert, caching the mode error before the assert. --- notebook/tests/selenium/utils.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 0e95d230a..00db7af9c 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -442,24 +442,19 @@ def validate_dualmode_state(notebook, mode, index): cell_index = notebook.browser.execute_script(JS) assert cell_index == [index] #only the index cell is selected - if mode == 'command': - #validate mode - assert mode == keyboard_mode #keyboard mode is correct + if mode != 'command' and mode != 'edit': + raise Exception('An unknown mode was send: mode = "%s"'%mode) #An unknown mode is send + + #validate mode + assert mode == keyboard_mode #keyboard mode is correct + if mode == 'command': assert is_focused_on(None) #no focused cells assert is_only_cell_edit(None) #no cells in edit mode elif mode == 'edit': - #Are the notebook and keyboard manager in edit mode? - assert mode == keyboard_mode #keyboard mode is correct - assert is_focused_on(index) #The specified cell is focused assert is_only_cell_edit(index) #The specified cell is the only one in edit mode - - else: - raise Exception('An unknown mode was send: mode = "%s"'%mode) #An unknown mode is send - - \ No newline at end of file From cd2ea150a0bd1ff5e208e4931a0d460bad35e91d Mon Sep 17 00:00:00 2001 From: Luis Rodriguez Date: Fri, 17 May 2019 09:11:11 -0700 Subject: [PATCH 13/13] Refactor for Changed 'for mode in cells_mode' for 'for i, mode in enumerate(cells_mode)' Removed the manual 'i' increment and declaration. --- notebook/tests/selenium/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/notebook/tests/selenium/utils.py b/notebook/tests/selenium/utils.py index 00db7af9c..169853e45 100644 --- a/notebook/tests/selenium/utils.py +++ b/notebook/tests/selenium/utils.py @@ -404,15 +404,13 @@ def validate_dualmode_state(notebook, mode, index): return False return True #Only the index cell is on edit mode - i = 0 - for mode in cells_mode: + for i, mode in enumerate(cells_mode): if i == index: if mode != 'edit': return False else: if mode == 'edit': return False - i += 1 return True def is_focused_on(index):