|
1 | 1 | """Tests arrow keys on both command and edit mode"""
|
| 2 | +import time |
2 | 3 |
|
| 4 | +from .utils import EDITOR_PAGE, EndToEndTimeout |
3 | 5 |
|
4 |
| -from .utils import EDITOR_PAGE |
5 | 6 |
|
| 7 | +INITIAL_CELLS = ['AAA', 'BBB', 'CCC'] |
| 8 | +JS_HAS_SELECTED = "(element) => { return element.classList.contains('selected'); }" |
6 | 9 |
|
7 |
| -def test_dualmode_arrows(notebook_frontend): |
8 | 10 |
|
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 |
13 | 15 |
|
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) |
30 | 18 |
|
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 | + ) |
35 | 24 | notebook_frontend.to_command_mode()
|
36 |
| - assert notebook_frontend.get_cells_contents() == ["0 edit #1", "1", "2", ""] |
37 | 25 |
|
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') |
41 | 42 | 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 | + ) |
42 | 46 |
|
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') |
45 | 63 | 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 | + ) |
54 | 67 | 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 | + ) |
59 | 75 |
|
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 |
66 | 77 |
|
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)] |
82 | 82 | [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 |
83 | 85 | 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') |
84 | 89 | 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 | + ) |
85 | 95 |
|
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') |
89 | 99 | 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') |
90 | 106 | notebook_frontend.press("1", page=EDITOR_PAGE)
|
91 | 107 | 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') |
93 | 124 | notebook_frontend.press("0", page=EDITOR_PAGE)
|
94 | 125 |
|
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') |
98 | 131 | notebook_frontend.press("ArrowDown", page=EDITOR_PAGE)
|
99 | 132 | notebook_frontend.press("ArrowRight", page=EDITOR_PAGE)
|
100 | 133 | 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') |
101 | 136 | 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') |
103 | 154 | notebook_frontend.press("3", page=EDITOR_PAGE)
|
104 | 155 | 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 | + ) |
0 commit comments