Merge pull request #4615 from LuisARodr/dualmode_execute

Convert dualmode_execute, dualmode_clipboard & dualmode_markdown to selenium
Thomas Kluyver 7 years ago committed by GitHub
commit 9237c3732e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.');
});
});

@ -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);
});
});

@ -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');
});
});

@ -0,0 +1,59 @@
"""Test"""
from .utils import shift, validate_dualmode_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_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_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_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_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_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_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_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

@ -0,0 +1,81 @@
''' Test keyboard invoked execution '''
from selenium.webdriver.common.keys import Keys
from .utils import shift, cmdtrl, alt, validate_dualmode_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_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_dualmode_state(notebook, 'edit', base_index)
shift(notebook.browser, Keys.ENTER) #creates one cell
validate_dualmode_state(notebook, 'command', base_index + 1)
#Starts in command mode
notebook.body.send_keys('k')
validate_dualmode_state(notebook, 'command', base_index)
shift(notebook.browser, Keys.ENTER) #creates one cell
validate_dualmode_state(notebook, 'command', base_index + 1)
#Ctrl-enter
#Last cell in notebook
base_index += 1
cmdtrl(notebook.browser, Keys.ENTER)
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_dualmode_state(notebook, 'edit', base_index - 1)
cmdtrl(notebook.browser, Keys.ENTER)
#Starts in command mode
notebook.body.send_keys('j')
validate_dualmode_state(notebook, 'command', base_index)
cmdtrl(notebook.browser, Keys.ENTER)
validate_dualmode_state(notebook, 'command', base_index)
#Alt-enter
#Last cell in notebook
alt(notebook.browser, Keys.ENTER)
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_dualmode_state(notebook, 'edit', base_index)
alt(notebook.browser, Keys.ENTER)
validate_dualmode_state(notebook, 'edit', base_index + 1)
#starts in command mode
notebook.body.send_keys(Keys.ESCAPE, 'k')
validate_dualmode_state(notebook, 'command', base_index)
alt(notebook.browser, Keys.ENTER)
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_dualmode_state(notebook, 'command', 7)

@ -0,0 +1,53 @@
'''Test'''
from selenium.webdriver.common.keys import Keys
from .utils import cmdtrl, shift, validate_dualmode_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_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_dualmode_state(notebook, 'edit', index)
cmdtrl(notebook.browser, Keys.ENTER)
assert is_cell_rendered(index) #cell is rendered with crtl+enter
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_dualmode_state(notebook, 'command', index - 1)
notebook.focus_cell(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_dualmode_state(notebook, 'command', index - 1)
shift(notebook.browser, Keys.ENTER)
validate_dualmode_state(notebook, 'command', index)
assert is_cell_rendered(index)#Cell is rendered
shift(notebook.browser, Keys.ENTER)
validate_dualmode_state(notebook, 'edit', index + 1)
assert is_cell_rendered(index)#Cell is rendered

@ -363,6 +363,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 +386,73 @@ 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_dualmode_state(notebook, mode, index):
'''Validate the entire dual mode state of the notebook.
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;})'
cells_mode = notebook.browser.execute_script(JS)
#None of the cells are in edit mode
if index is None:
for mode in cells_mode:
if mode == 'edit':
return False
return True
#Only the index cell is on edit mode
for i, mode in enumerate(cells_mode):
if i == index:
if mode != 'edit':
return False
else:
if mode == 'edit':
return False
return True
def is_focused_on(index):
JS = "return $('#notebook .CodeMirror-focused textarea').length;"
focused_cells = notebook.browser.execute_script(JS)
if index is None:
return focused_cells == 0
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)
return 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
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' 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':
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
Loading…
Cancel
Save