|
| 1 | +def test_multiselect(notebook): |
| 2 | + def extend_selection_by(delta): |
| 3 | + notebook.browser.execute_script( |
| 4 | + "Jupyter.notebook.extend_selection_by(arguments[0]);", delta) |
| 5 | + |
| 6 | + def n_selected_cells(): |
| 7 | + return notebook.browser.execute_script( |
| 8 | + "return Jupyter.notebook.get_selected_cells().length;") |
| 9 | + |
| 10 | + a = 'print("a")' |
| 11 | + b = 'print("b")' |
| 12 | + c = 'print("c")' |
| 13 | + notebook.edit_cell(index=0, content=a) |
| 14 | + notebook.append(b, c) |
| 15 | + |
| 16 | + notebook.focus_cell(0) |
| 17 | + assert n_selected_cells() == 1 |
| 18 | + |
| 19 | + # Check that only one cell is selected according to CSS classes as well |
| 20 | + selected_css = notebook.browser.find_elements_by_css_selector( |
| 21 | + '.cell.jupyter-soft-selected, .cell.selected') |
| 22 | + assert len(selected_css) == 1 |
| 23 | + |
| 24 | + # Extend the selection down one |
| 25 | + extend_selection_by(1) |
| 26 | + assert n_selected_cells() == 2 |
| 27 | + |
| 28 | + # Contract the selection up one |
| 29 | + extend_selection_by(-1) |
| 30 | + assert n_selected_cells() == 1 |
| 31 | + |
| 32 | + # Extend the selection up one |
| 33 | + notebook.focus_cell(1) |
| 34 | + extend_selection_by(-1) |
| 35 | + assert n_selected_cells() == 2 |
| 36 | + |
| 37 | + # Convert selected cells to Markdown |
| 38 | + notebook.browser.execute_script("Jupyter.notebook.cells_to_markdown();") |
| 39 | + cell_types = notebook.browser.execute_script( |
| 40 | + "return Jupyter.notebook.get_cells().map(c => c.cell_type)") |
| 41 | + assert cell_types == ['markdown', 'markdown', 'code'] |
| 42 | + # One cell left selected after conversion |
| 43 | + assert n_selected_cells() == 1 |
| 44 | + |
| 45 | + # Convert selected cells to raw |
| 46 | + notebook.focus_cell(1) |
| 47 | + extend_selection_by(1) |
| 48 | + assert n_selected_cells() == 2 |
| 49 | + notebook.browser.execute_script("Jupyter.notebook.cells_to_raw();") |
| 50 | + cell_types = notebook.browser.execute_script( |
| 51 | + "return Jupyter.notebook.get_cells().map(c => c.cell_type)") |
| 52 | + assert cell_types == ['markdown', 'raw', 'raw'] |
| 53 | + # One cell left selected after conversion |
| 54 | + assert n_selected_cells() == 1 |
| 55 | + |
| 56 | + # Convert selected cells to code |
| 57 | + notebook.focus_cell(0) |
| 58 | + extend_selection_by(2) |
| 59 | + assert n_selected_cells() == 3 |
| 60 | + notebook.browser.execute_script("Jupyter.notebook.cells_to_code();") |
| 61 | + cell_types = notebook.browser.execute_script( |
| 62 | + "return Jupyter.notebook.get_cells().map(c => c.cell_type)") |
| 63 | + assert cell_types == ['code'] * 3 |
| 64 | + # One cell left selected after conversion |
| 65 | + assert n_selected_cells() == 1 |
0 commit comments