undelete.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from selenium.webdriver.common.keys import Keys
  2. from .utils import shift
  3. def get_cells_contents(nb):
  4. JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})'
  5. return nb.browser.execute_script(JS)
  6. def undelete(nb):
  7. nb.browser.execute_script('Jupyter.notebook.undelete_cell();')
  8. def test_undelete_cells(notebook):
  9. a = 'print("a")'
  10. b = 'print("b")'
  11. c = 'print("c")'
  12. d = 'print("d")'
  13. notebook.edit_cell(index=0, content=a)
  14. notebook.append(b, c, d)
  15. notebook.to_command_mode()
  16. # Verify initial state
  17. assert get_cells_contents(notebook) == [a, b, c, d]
  18. # Delete cells [1, 2]
  19. notebook.focus_cell(1)
  20. shift(notebook.browser, Keys.DOWN)
  21. notebook.current_cell.send_keys('dd')
  22. assert get_cells_contents(notebook) == [a, d]
  23. # Delete new cell 1 (which contains d)
  24. notebook.focus_cell(1)
  25. notebook.current_cell.send_keys('dd')
  26. assert get_cells_contents(notebook) == [a]
  27. # Undelete d
  28. undelete(notebook)
  29. assert get_cells_contents(notebook) == [a, d]
  30. # Undelete b, c
  31. undelete(notebook)
  32. assert get_cells_contents(notebook) == [a, b, c, d]
  33. # Nothing more to undelete
  34. undelete(notebook)
  35. assert get_cells_contents(notebook) == [a, b, c, d]
  36. # Delete first two cells and restore
  37. notebook.focus_cell(0)
  38. shift(notebook.browser, Keys.DOWN)
  39. notebook.current_cell.send_keys('dd')
  40. assert get_cells_contents(notebook) == [c, d]
  41. undelete(notebook)
  42. assert get_cells_contents(notebook) == [a, b, c, d]
  43. # Delete last two cells and restore
  44. notebook.focus_cell(-1)
  45. shift(notebook.browser, Keys.UP)
  46. notebook.current_cell.send_keys('dd')
  47. assert get_cells_contents(notebook) == [a, b]
  48. undelete(notebook)
  49. assert get_cells_contents(notebook) == [a, b, c, d]
  50. # Merge cells [1, 2], restore the deleted one
  51. bc = b + "\n\n" + c
  52. notebook.focus_cell(1)
  53. shift(notebook.browser, 'j')
  54. shift(notebook.browser, 'm')
  55. assert get_cells_contents(notebook) == [a, bc, d]
  56. undelete(notebook)
  57. assert get_cells_contents(notebook) == [a, bc, c, d]
  58. # Merge cells [2, 3], restore the deleted one
  59. cd = c + "\n\n" + d
  60. notebook.focus_cell(-1)
  61. shift(notebook.browser, 'k')
  62. shift(notebook.browser, 'm')
  63. assert get_cells_contents(notebook) == [a, bc, cd]
  64. undelete(notebook)
  65. assert get_cells_contents(notebook) == [a, bc, cd, d]
  66. # Reset contents to [a, b, c, d] --------------------------------------
  67. notebook.edit_cell(index=1, content=b)
  68. notebook.edit_cell(index=2, content=c)
  69. assert get_cells_contents(notebook) == [a, b, c, d]
  70. # Merge cell below, restore the deleted one
  71. ab = a + "\n\n" + b
  72. notebook.focus_cell(0)
  73. notebook.browser.execute_script("Jupyter.notebook.merge_cell_below();")
  74. assert get_cells_contents(notebook) == [ab, c, d]
  75. undelete(notebook)
  76. assert get_cells_contents(notebook) == [ab, b, c, d]
  77. # Merge cell above, restore the deleted one
  78. cd = c + "\n\n" + d
  79. notebook.focus_cell(-1)
  80. notebook.browser.execute_script("Jupyter.notebook.merge_cell_above();")
  81. assert get_cells_contents(notebook) == [ab, b, cd]
  82. undelete(notebook)
  83. assert get_cells_contents(notebook) == [ab, b, c, cd]