Skip to content

Commit be4cb63

Browse files
Rewrite/refactors for test_dualmode_arrows. (#232)
1 parent d2b5fe7 commit be4cb63

File tree

2 files changed

+145
-74
lines changed

2 files changed

+145
-74
lines changed
Lines changed: 128 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,159 @@
11
"""Tests arrow keys on both command and edit mode"""
2+
import time
23

4+
from .utils import EDITOR_PAGE, EndToEndTimeout
35

4-
from .utils import EDITOR_PAGE
56

7+
INITIAL_CELLS = ['AAA', 'BBB', 'CCC']
8+
JS_HAS_SELECTED = "(element) => { return element.classList.contains('selected'); }"
69

7-
def test_dualmode_arrows(notebook_frontend):
810

9-
# Tests in command mode.
10-
# Setting up the cells to test the keys to move up.
11-
notebook_frontend.to_command_mode()
12-
[notebook_frontend.press("b", page=EDITOR_PAGE) for i in range(3)]
11+
def test_dualmode_arrows(prefill_notebook):
12+
# Tests functionality related to up/down arrows and
13+
# the "j"/"k" shortcuts for up and down, in command
14+
# mode and in edit mode
1315

14-
# Use both "k" and up arrow keys to moving up and enter a value.
15-
# Once located on the top cell, use the up arrow keys to prove the top cell is still selected.
16-
notebook_frontend.press("k", page=EDITOR_PAGE)
17-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
18-
notebook_frontend.press("2", page=EDITOR_PAGE)
19-
notebook_frontend.to_command_mode()
20-
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
21-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
22-
notebook_frontend.press("1", page=EDITOR_PAGE)
23-
notebook_frontend.to_command_mode()
24-
notebook_frontend.press("k", page=EDITOR_PAGE)
25-
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
26-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
27-
notebook_frontend.press("0", page=EDITOR_PAGE)
28-
notebook_frontend.to_command_mode()
29-
assert notebook_frontend.get_cells_contents() == ["0", "1", "2", ""]
16+
print('[Test] [test_dualmode_arrows] Start!')
17+
notebook_frontend = prefill_notebook(INITIAL_CELLS)
3018

31-
# Use the "k" key on the top cell as well
32-
notebook_frontend.press("k", page=EDITOR_PAGE)
33-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
34-
notebook_frontend.type(" edit #1", page=EDITOR_PAGE)
19+
# Make sure the top cell is selected
20+
print('[Test] Ensure top cell is selected')
21+
notebook_frontend.wait_for_condition(
22+
lambda: notebook_frontend.cells[0].evaluate(JS_HAS_SELECTED) is True
23+
)
3524
notebook_frontend.to_command_mode()
36-
assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", ""]
3725

38-
# Setting up the cells to test the keys to move down
39-
[notebook_frontend.press("j", page=EDITOR_PAGE) for i in range(3)]
40-
[notebook_frontend.press("a", page=EDITOR_PAGE) for i in range(2)]
26+
# Move down (shortcut j) to the second cell and check that it's selected
27+
print('[Test] Move down ("j") to second cell')
28+
notebook_frontend.press("j", page=EDITOR_PAGE)
29+
notebook_frontend.wait_for_condition(
30+
lambda: notebook_frontend.cells[1].evaluate(JS_HAS_SELECTED) is True
31+
)
32+
33+
# Move down to the third cell and check that it's selected
34+
print('[Test] Move down to third cell')
35+
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
36+
notebook_frontend.wait_for_condition(
37+
lambda: notebook_frontend.cells[2].evaluate(JS_HAS_SELECTED) is True
38+
)
39+
40+
# Move back up (shortcut k) to the second cell
41+
print('[Test] Move back up ("k") to second cell')
4142
notebook_frontend.press("k", page=EDITOR_PAGE)
43+
notebook_frontend.wait_for_condition(
44+
lambda: notebook_frontend.cells[1].evaluate(JS_HAS_SELECTED) is True
45+
)
4246

43-
# Use both "j" key and down arrow keys to moving down and enter a value.
44-
# Once located on the bottom cell, use the down arrow key to prove the bottom cell is still selected.
47+
# Move up to the top cell
48+
print('[Test] Move to top')
49+
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
50+
notebook_frontend.wait_for_condition(
51+
lambda: notebook_frontend.cells[0].evaluate(JS_HAS_SELECTED) is True
52+
)
53+
54+
# Move up while already on the top cell and ensure it stays selected
55+
print('[Test] Move up while already on top')
56+
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
57+
notebook_frontend.wait_for_condition(
58+
lambda: notebook_frontend.cells[0].evaluate(JS_HAS_SELECTED) is True
59+
)
60+
61+
# Move down to the last cell + press down to ensure it's still selected
62+
print('[Test] Move to bottom and press down')
4563
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
46-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
47-
notebook_frontend.press("3", page=EDITOR_PAGE)
48-
notebook_frontend.to_command_mode()
49-
notebook_frontend.press("j", page=EDITOR_PAGE)
50-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
51-
notebook_frontend.press("4", page=EDITOR_PAGE)
52-
notebook_frontend.to_command_mode()
53-
notebook_frontend.press("j", page=EDITOR_PAGE)
64+
notebook_frontend.wait_for_condition(
65+
lambda: notebook_frontend.cells[1].evaluate(JS_HAS_SELECTED) is True
66+
)
5467
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
55-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
56-
notebook_frontend.press("5", page=EDITOR_PAGE)
57-
notebook_frontend.to_command_mode()
58-
assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5"]
68+
notebook_frontend.wait_for_condition(
69+
lambda: notebook_frontend.cells[2].evaluate(JS_HAS_SELECTED) is True
70+
)
71+
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
72+
notebook_frontend.wait_for_condition(
73+
lambda: notebook_frontend.cells[2].evaluate(JS_HAS_SELECTED) is True
74+
)
5975

60-
# Use the "j" key on the top cell as well
61-
notebook_frontend.press("j", page=EDITOR_PAGE)
62-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
63-
notebook_frontend.type(" edit #1", page=EDITOR_PAGE)
64-
notebook_frontend.to_command_mode()
65-
assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1"]
76+
# EDIT MODE TESTS
6677

67-
# On the bottom cell, use both left and right arrow keys to prove the bottom cell is still selected.
68-
notebook_frontend.press("ArrowLeft", page=EDITOR_PAGE)
69-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
70-
notebook_frontend.type(", #2", page=EDITOR_PAGE)
71-
notebook_frontend.to_command_mode()
72-
assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1, #2"]
73-
notebook_frontend.press("ArrowRight", page=EDITOR_PAGE)
74-
notebook_frontend.press("Enter", page=EDITOR_PAGE)
75-
notebook_frontend.type(" and #3", page=EDITOR_PAGE)
76-
notebook_frontend.to_command_mode()
77-
assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1, #2 and #3"]
78-
79-
# Tests in edit mode.
80-
# First, erase the previous content and then setup the cells to test the keys to move up.
81-
[notebook_frontend.locate(".fa-cut.fa", page=EDITOR_PAGE).click() for i in range(6)]
78+
# Delete all the cells, then add new ones to test
79+
# arrow key behaviors in edit mode on empty cells
80+
print('[Test] Prep cells for edit mode tests')
81+
[notebook_frontend.locate(".fa-cut.fa", page=EDITOR_PAGE).click() for i in range(4)]
8282
[notebook_frontend.press("b", page=EDITOR_PAGE) for i in range(2)]
83+
# Add a cell above, which will leave us selected
84+
# on the third cell out of 4 empty cells
8385
notebook_frontend.press("a", page=EDITOR_PAGE)
86+
87+
# Start editing the third empty cell
88+
print('[Test] Enter edit mode on the third cell')
8489
notebook_frontend.press("Enter", page=EDITOR_PAGE)
90+
# Check that the cell is being edited
91+
notebook_frontend.wait_for_selector('.CodeMirror-focused', page=EDITOR_PAGE)
92+
notebook_frontend.wait_for_condition(
93+
lambda: notebook_frontend.cells[2].locate('.CodeMirror-focused')
94+
)
8595

86-
# Use the up arrow key to move down and enter a value.
87-
# We will use the left arrow key to move one char to the left since moving up on last character only moves selector to the first one.
88-
# Once located on the top cell, use the up arrow key to prove the top cell is still selected.
96+
# Arrow up in edit mode on this empty cell (should move to edit move
97+
# on the cell above when a cell is empty)
98+
print('[Test] Arrow up in edit mode to the second cell')
8999
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
100+
notebook_frontend.wait_for_condition(
101+
lambda: notebook_frontend.cells[1].evaluate(JS_HAS_SELECTED) is True
102+
)
103+
# Type a 1 in edit mode, then arrow left (to the beginning of the cell)
104+
# and then up, which should then move to edit mode in the cell above
105+
print('[Test] Enter a "1" in the second cell')
90106
notebook_frontend.press("1", page=EDITOR_PAGE)
91107
notebook_frontend.press("ArrowLeft", page=EDITOR_PAGE)
92-
[notebook_frontend.press("ArrowUp", page=EDITOR_PAGE) for i in range(2)]
108+
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
109+
notebook_frontend.wait_for_condition(
110+
lambda: notebook_frontend.cells[0].evaluate(JS_HAS_SELECTED) is True,
111+
)
112+
notebook_frontend.wait_for_condition(
113+
lambda: notebook_frontend.get_cells_contents() == ['', '1', '', ''],
114+
)
115+
116+
print('[Test] Move to the top cell and edit')
117+
# Arrow up again while on the top cell, it should still be selected
118+
notebook_frontend.press("ArrowUp", page=EDITOR_PAGE)
119+
notebook_frontend.wait_for_condition(
120+
lambda: notebook_frontend.cells[0].evaluate(JS_HAS_SELECTED) is True
121+
)
122+
# Enter a 0 in the top cell (we're still in edit mode)
123+
print('[Test] Enter a "0" in the top cell')
93124
notebook_frontend.press("0", page=EDITOR_PAGE)
94125

95-
# Use the down arrow key to move down and enter a value.
96-
# We will use the right arrow key to move one char to the right since moving down puts selector to the last character.
97-
# Once located on the bottom cell, use the down arrow key to prove the bottom cell is still selected.
126+
# Move down, right, down, while the edit mode cursor is on the top cell,
127+
# after the 0 char...this should move down a cell (to the second cell),
128+
# then right to the end of the 1 char in the second cell, then down to
129+
# the third empty cell
130+
print('[Test] Move down to the third cell and edit')
98131
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
99132
notebook_frontend.press("ArrowRight", page=EDITOR_PAGE)
100133
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
134+
# Put a 2 in the third cell
135+
print('[Test] Enter a "2" in the third cell')
101136
notebook_frontend.press("2", page=EDITOR_PAGE)
102-
[notebook_frontend.press("ArrowDown", page=EDITOR_PAGE) for i in range(2)]
137+
138+
# Move down to the last cell, then down again while on the bottom cell
139+
# (which should stay in the bottom cell), then enter a 3 in the bottom
140+
# (fourth) cell
141+
print('[Test] Move down to the bottom cell and edit')
142+
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
143+
notebook_frontend.wait_for_condition(
144+
lambda: notebook_frontend.cells[3].evaluate(JS_HAS_SELECTED) is True
145+
)
146+
notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
147+
notebook_frontend.wait_for_condition(
148+
lambda: notebook_frontend.cells[3].evaluate(JS_HAS_SELECTED) is True
149+
)
150+
notebook_frontend.wait_for_condition( # Ensure it's in edit mode
151+
lambda: notebook_frontend.cells[3].locate('.CodeMirror-focused'),
152+
) # If it's not located, the FrontendElement will be Falsy
153+
print('[Test] Enter a "3" in the fourth cell')
103154
notebook_frontend.press("3", page=EDITOR_PAGE)
104155
notebook_frontend.to_command_mode()
105-
assert notebook_frontend.get_cells_contents() == ["0", "1", "2", "3"]
156+
print('[Test] Check the results match expectations')
157+
notebook_frontend.wait_for_condition(
158+
lambda: notebook_frontend.get_cells_contents() == ["0", "1", "2", "3"]
159+
)

nbclassic/tests/end_to_end/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ def cells(self):
285285
def current_index(self):
286286
return self.index(self.current_cell)
287287

288+
def sshot_edit(self):
289+
return self.screenshot(EDITOR_PAGE)
290+
291+
def sshot_tree(self):
292+
return self.screenshot(TREE_PAGE)
293+
294+
def screenshot(self, page):
295+
"""Returns bytes of a PNG screenshot of the page"""
296+
if page == TREE_PAGE:
297+
specified_page = self._tree_page
298+
elif page == EDITOR_PAGE:
299+
specified_page = self._editor_page
300+
else:
301+
raise Exception('Error, provide a valid page to evaluate from!')
302+
303+
return specified_page.screenshot()
304+
288305
def index(self, cell):
289306
return self._cells.index(cell)
290307

0 commit comments

Comments
 (0)