This makes several tests shorter and (hopefully) more efficientpull/5043/head
parent
9640e1f943
commit
7fb461ddf7
@ -1,24 +1,16 @@
|
||||
import pytest
|
||||
INITIAL_CELLS = ["hello", "hellohello", "abc", "ello"]
|
||||
|
||||
|
||||
def test_find_and_replace(notebook):
|
||||
def test_find_and_replace(prefill_notebook):
|
||||
""" test find and replace on all the cells """
|
||||
cell_0, cell_1, cell_2, cell_3 = "hello", "hellohello", "abc", "ello"
|
||||
|
||||
find_str = "ello" # string to replace
|
||||
replace_str = "foo" # string to replace to
|
||||
notebook = prefill_notebook(INITIAL_CELLS)
|
||||
|
||||
# set the contents of the cells
|
||||
notebook.add_cell(index=0, content=cell_0);
|
||||
notebook.add_cell(index=1, content=cell_1);
|
||||
notebook.add_cell(index=2, content=cell_2);
|
||||
notebook.add_cell(index=3, content=cell_3);
|
||||
find_str = "ello"
|
||||
replace_str = "foo"
|
||||
|
||||
# replace the strings
|
||||
notebook.find_and_replace(index=0, find_txt=find_str, replace_txt=replace_str)
|
||||
|
||||
# check content of the cells
|
||||
assert notebook.get_cell_contents(0) == cell_0.replace(find_str, replace_str)
|
||||
assert notebook.get_cell_contents(1) == cell_1.replace(find_str, replace_str)
|
||||
assert notebook.get_cell_contents(2) == cell_2.replace(find_str, replace_str)
|
||||
assert notebook.get_cell_contents(3) == cell_3.replace(find_str, replace_str)
|
||||
assert notebook.get_cells_contents() == [
|
||||
s.replace(find_str, replace_str) for s in INITIAL_CELLS
|
||||
]
|
||||
|
||||
@ -1,28 +1,30 @@
|
||||
import os
|
||||
import pytest
|
||||
import time
|
||||
|
||||
# selenium test version for 'prompt_numbers.js'
|
||||
|
||||
def get_prompt(nb, index):
|
||||
cell = nb.cells[0]
|
||||
return cell.find_element_by_class_name('input').find_element_by_class_name('input_prompt').get_attribute('innerHTML').strip()
|
||||
|
||||
def set_prompt(nb, index, value):
|
||||
nb.set_cell_input_prompt(index, value)
|
||||
|
||||
def test_prompt_numbers(notebook):
|
||||
cell_index = 0
|
||||
a = 'print("a")'
|
||||
notebook.edit_cell(index=cell_index, content=a)
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [ ]:"
|
||||
set_prompt(notebook, cell_index, 2);
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [2]:"
|
||||
set_prompt(notebook, cell_index, 0);
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [0]:"
|
||||
set_prompt(notebook, cell_index, "'*'");
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [*]:"
|
||||
set_prompt(notebook, cell_index, "undefined");
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [ ]:"
|
||||
set_prompt(notebook, cell_index, "null");
|
||||
assert get_prompt(notebook, cell_index) == "<bdi>In</bdi> [ ]:"
|
||||
|
||||
def test_prompt_numbers(prefill_notebook):
|
||||
notebook = prefill_notebook(['print("a")'])
|
||||
|
||||
def get_prompt():
|
||||
return (
|
||||
notebook.cells[0].find_element_by_class_name('input')
|
||||
.find_element_by_class_name('input_prompt')
|
||||
.get_attribute('innerHTML').strip()
|
||||
)
|
||||
|
||||
def set_prompt(value):
|
||||
notebook.set_cell_input_prompt(0, value)
|
||||
|
||||
assert get_prompt() == "<bdi>In</bdi> [ ]:"
|
||||
|
||||
set_prompt(2)
|
||||
assert get_prompt() == "<bdi>In</bdi> [2]:"
|
||||
|
||||
set_prompt(0)
|
||||
assert get_prompt() == "<bdi>In</bdi> [0]:"
|
||||
|
||||
set_prompt("'*'")
|
||||
assert get_prompt() == "<bdi>In</bdi> [*]:"
|
||||
|
||||
set_prompt("undefined")
|
||||
assert get_prompt() == "<bdi>In</bdi> [ ]:"
|
||||
|
||||
set_prompt("null")
|
||||
assert get_prompt() == "<bdi>In</bdi> [ ]:"
|
||||
|
||||
@ -1,100 +1,92 @@
|
||||
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")'
|
||||
INITIAL_CELLS = ['print("a")', 'print("b")', 'print("c")', 'print("d")']
|
||||
|
||||
notebook.edit_cell(index=0, content=a)
|
||||
notebook.append(b, c, d)
|
||||
notebook.to_command_mode()
|
||||
def test_undelete_cells(prefill_notebook):
|
||||
notebook = prefill_notebook(INITIAL_CELLS)
|
||||
a, b, c, d = INITIAL_CELLS
|
||||
|
||||
# Verify initial state
|
||||
assert get_cells_contents(notebook) == [a, b, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [a]
|
||||
|
||||
# Undelete d
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, d]
|
||||
assert notebook.get_cells_contents() == [a, d]
|
||||
|
||||
# Undelete b, c
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, b, c, d]
|
||||
assert notebook.get_cells_contents() == [a, b, c, d]
|
||||
|
||||
# Nothing more to undelete
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, b, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [c, d]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, b, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [a, b]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, b, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [a, bc, d]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, bc, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [a, bc, cd]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [a, bc, cd, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [ab, c, d]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [ab, b, c, d]
|
||||
assert notebook.get_cells_contents() == [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]
|
||||
assert notebook.get_cells_contents() == [ab, b, cd]
|
||||
undelete(notebook)
|
||||
assert get_cells_contents(notebook) == [ab, b, c, cd]
|
||||
assert notebook.get_cells_contents() == [ab, b, c, cd]
|
||||
|
||||
Loading…
Reference in new issue