Merge pull request #3475 from takluyver/selenium-undelete

Convert undelete cell tests to Selenium
pull/3494/head
Thomas Kluyver 8 years ago committed by GitHub
commit 596020307b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,156 +0,0 @@
//
// Test undeleting cells.
//
casper.notebook_test(function () {
var that = this;
var assert_selected_cells = function (action, indices) {
var selected = that.evaluate(function () {
return IPython.notebook.get_selected_cells_indices();
});
that.test.assertEquals( selected, indices, action + "; verify selected cells");
};
var assert_cells = function (action, cells, index) {
var msg = action + "; there are " + cells.length + " cells";
that.test.assertEquals(that.get_cells_length(), cells.length, msg);
var i;
for (i = 0; i < cells.length; i++) {
msg = action + "; cell " + i + " has correct text";
that.test.assertEquals(that.get_cell_text(i), cells[i], msg);
}
that.validate_notebook_state(action, 'command', index);
assert_selected_cells(action, [index]);
};
var a = 'print("a")';
this.set_cell_text(0, a);
var b = 'print("b")';
this.append_cell(b);
var c = 'print("c")';
this.append_cell(c);
var d = 'print("d")';
this.append_cell(d);
// Verify initial state
this.select_cell(0);
this.trigger_keydown('esc');
assert_cells("initial state", [a, b, c, d], 0);
// Delete cells 1,2
this.select_cells(1,3);
this.trigger_keydown('esc');
this.trigger_keydown('d', 'd');
assert_cells("delete cells 1,2", [a, d], 1);
// Delete cell 1 (3)
this.select_cell(1);
this.trigger_keydown('esc');
this.trigger_keydown('d', 'd');
assert_cells("delete cell 1 (3)", [a], 0);
// Undelete cell 1 (3)
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undelete cell 1 (3)", [a, d], 0);
this.select_cell(1); // select after undelete, to test cursor movement
// Undelete cell 1
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undelete cell 1,2", [a, b, c, d], 3);
// Undelete cell (none)
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undelete cell (none)", [a, b, c, d], 3);
this.select_cells(0,2);
this.trigger_keydown('esc');
this.trigger_keydown('d', 'd');
assert_cells("delete first two cells", [c, d], 0);
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undelete first two cells", [a, b, c, d], 2);
this.select_cells(2, 4);
this.trigger_keydown('esc');
this.trigger_keydown('d', 'd');
assert_cells("delete last two cells", [a, b], 1);
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undelete last two cells", [a, b, c, d], 1);
// Merge cells 1-2
var bc = b + "\n\n" + c;
this.select_cell(1);
this.trigger_keydown('esc');
this.trigger_keydown('shift-j');
assert_selected_cells("select cells 1-2", [1, 2]);
this.trigger_keydown('shift-m');
this.trigger_keydown('esc');
assert_cells("merge cells 1-2", [a, bc, d], 1);
// Undo merge
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undo merge", [a, bc, c, d], 1);
// Merge cells 3-2
var cd = c + "\n\n" + d;
this.select_cell(3);
this.trigger_keydown('esc');
this.trigger_keydown('shift-k');
assert_selected_cells("select cells 3-2", [2, 3]);
this.trigger_keydown('shift-m');
this.trigger_keydown('esc');
assert_cells("merge cells 3-2", [a, bc, cd], 2);
// Undo merge
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undo merge", [a, bc, cd, d], 2);
// Merge below
var abc = a + "\n\n" + bc;
this.select_cell(0);
this.trigger_keydown('esc');
this.evaluate(function () {
IPython.notebook.merge_cell_below();
});
assert_cells("merge cell below", [abc, cd, d], 0);
// Undo merge
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undo merge", [abc, bc, cd, d], 0);
// Merge above
var bccd = bc + "\n\n" + cd;
this.select_cell(2);
this.trigger_keydown('esc');
this.evaluate(function () {
IPython.notebook.merge_cell_above();
});
assert_cells("merge cell above", [abc, bccd, d], 1);
// Undo merge
this.evaluate(function () {
IPython.notebook.undelete_cell();
});
assert_cells("undo merge", [abc, bc, bccd, d], 2);
});

@ -9,6 +9,7 @@ import time
from urllib.parse import urljoin
from selenium.webdriver import Firefox, Remote, Chrome
from .utils import Notebook
pjoin = os.path.join
@ -114,3 +115,7 @@ def authenticated_browser(selenium_driver, notebook_server):
selenium_driver.jupyter_server_info = notebook_server
selenium_driver.get("{url}?token={token}".format(**notebook_server))
return selenium_driver
@pytest.fixture
def notebook(authenticated_browser):
return Notebook.new_notebook(authenticated_browser)

@ -1,17 +1,3 @@
import os
import pytest
from selenium.webdriver.common.keys import Keys
from .utils import wait_for_selector, Notebook
pjoin = os.path.join
@pytest.fixture
def notebook(authenticated_browser):
return Notebook.new_notebook(authenticated_browser)
def get_rendered_contents(nb):
cl = ["text_cell", "render"]

@ -0,0 +1,100 @@
from selenium.webdriver.common.keys import Keys
from .utils import shift
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 undelete(nb):
nb.browser.execute_script('Jupyter.notebook.undelete_cell();')
def test_undelete_cells(notebook):
a = 'print("a")'
b = 'print("b")'
c = 'print("c")'
d = 'print("d")'
notebook.edit_cell(index=0, content=a)
notebook.append(b, c, d)
notebook.to_command_mode()
# Verify initial state
assert get_cells_contents(notebook) == [a, b, c, d]
# Delete cells [1, 2]
notebook.focus_cell(1)
shift(notebook.browser, Keys.DOWN)
notebook.current_cell.send_keys('dd')
assert get_cells_contents(notebook) == [a, d]
# Delete new cell 1 (which contains d)
notebook.focus_cell(1)
notebook.current_cell.send_keys('dd')
assert get_cells_contents(notebook) == [a]
# Undelete d
undelete(notebook)
assert get_cells_contents(notebook) == [a, d]
# Undelete b, c
undelete(notebook)
assert get_cells_contents(notebook) == [a, b, c, d]
# Nothing more to undelete
undelete(notebook)
assert get_cells_contents(notebook) == [a, b, c, d]
# Delete first two cells and restore
notebook.focus_cell(0)
shift(notebook.browser, Keys.DOWN)
notebook.current_cell.send_keys('dd')
assert get_cells_contents(notebook) == [c, d]
undelete(notebook)
assert get_cells_contents(notebook) == [a, b, c, d]
# Delete last two cells and restore
notebook.focus_cell(-1)
shift(notebook.browser, Keys.UP)
notebook.current_cell.send_keys('dd')
assert get_cells_contents(notebook) == [a, b]
undelete(notebook)
assert get_cells_contents(notebook) == [a, b, c, d]
# Merge cells [1, 2], restore the deleted one
bc = b + "\n\n" + c
notebook.focus_cell(1)
shift(notebook.browser, 'j')
shift(notebook.browser, 'm')
assert get_cells_contents(notebook) == [a, bc, d]
undelete(notebook)
assert get_cells_contents(notebook) == [a, bc, c, d]
# Merge cells [2, 3], restore the deleted one
cd = c + "\n\n" + d
notebook.focus_cell(-1)
shift(notebook.browser, 'k')
shift(notebook.browser, 'm')
assert get_cells_contents(notebook) == [a, bc, cd]
undelete(notebook)
assert get_cells_contents(notebook) == [a, bc, cd, d]
# Reset contents to [a, b, c, d] --------------------------------------
notebook.edit_cell(index=1, content=b)
notebook.edit_cell(index=2, content=c)
assert get_cells_contents(notebook) == [a, b, c, d]
# Merge cell below, restore the deleted one
ab = a + "\n\n" + b
notebook.focus_cell(0)
notebook.browser.execute_script("Jupyter.notebook.merge_cell_below();")
assert get_cells_contents(notebook) == [ab, c, d]
undelete(notebook)
assert get_cells_contents(notebook) == [ab, b, c, d]
# Merge cell above, restore the deleted one
cd = c + "\n\n" + d
notebook.focus_cell(-1)
notebook.browser.execute_script("Jupyter.notebook.merge_cell_above();")
assert get_cells_contents(notebook) == [ab, b, cd]
undelete(notebook)
assert get_cells_contents(notebook) == [ab, b, c, cd]

@ -1,5 +1,6 @@
import os
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
@ -127,10 +128,17 @@ class Notebook:
element = wait.until(EC.staleness_of(cell))
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
"""
if cell is not None:
index = self.index(cell)
self.focus_cell(index)
# Select & delete anything already in the cell
self.current_cell.send_keys(Keys.ENTER)
ctrl(self.browser, 'a')
self.current_cell.send_keys(Keys.DELETE)
for line_no, line in enumerate(content.splitlines()):
if line_no != 0:
self.current_cell.send_keys(Keys.ENTER, "\n")
@ -154,7 +162,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")
@ -220,3 +229,13 @@ def new_window(browser, selector=None):
browser.switch_to_window(new_window_handle)
if selector is not None:
wait_for_selector(browser, selector)
def shift(browser, k):
"""Send key combination Shift+(k)"""
ActionChains(browser)\
.key_down(Keys.SHIFT).send_keys(k).key_up(Keys.SHIFT).perform()
def ctrl(browser, k):
"""Send key combination Ctrl+(k)"""
ActionChains(browser)\
.key_down(Keys.CONTROL).send_keys(k).key_up(Keys.CONTROL).perform()

Loading…
Cancel
Save