33
44import traceback
55
6- from .utils import EDITOR_PAGE , TimeoutError as TestingTimeout
7- from playwright .sync_api import sync_playwright , TimeoutError as PlaywrightTimeoutError
6+ from .utils import EDITOR_PAGE , EndToEndTimeout
87
98
109def save_as (nb ):
@@ -24,14 +23,39 @@ def set_notebook_name(nb, name):
2423
2524def test_save_readonly_as (notebook_frontend ):
2625 print ('[Test] [test_save_readonly_as]' )
27- notebook_frontend .edit_cell (index = 0 , content = 'a=10; print(a)' )
2826 notebook_frontend .wait_for_kernel_ready ()
29- notebook_frontend .wait_for_selector (".input" , page = EDITOR_PAGE )
3027
31- # Set a name for comparison later
32- print ('[Test] Set notebook name' )
33- set_notebook_name (notebook_frontend , name = "nb1.ipynb" )
34- assert get_notebook_name (notebook_frontend ) == "nb1.ipynb"
28+ print ('[Test] Make notebook read-only' )
29+ cell_text = (
30+ 'import os\n import stat\n os.chmod("'
31+ + notebook_frontend .get_page_url (EDITOR_PAGE ).split ('?' )[0 ].split ('/' )[- 1 ]
32+ + '", stat.S_IREAD)\n print(0)'
33+ )
34+ notebook_frontend .edit_cell (index = 0 , content = cell_text )
35+ notebook_frontend .wait_for_condition (
36+ lambda : notebook_frontend .get_cell_contents (0 ).strip () == cell_text
37+ )
38+ notebook_frontend .evaluate ("Jupyter.notebook.get_cell(0).execute();" , page = EDITOR_PAGE )
39+ notebook_frontend .wait_for_cell_output (0 )
40+ notebook_frontend .reload (EDITOR_PAGE )
41+ notebook_frontend .wait_for_kernel_ready ()
42+
43+ print ('[Test] Check that the notebook is read-only' )
44+ notebook_frontend .wait_for_condition (
45+ lambda : notebook_frontend .evaluate ('() => { return Jupyter.notebook.writable }' , page = EDITOR_PAGE ) is False ,
46+ timeout = 150 ,
47+ period = 1
48+ )
49+
50+ # Add some content
51+ print ('[Test] Add cell' )
52+ test_content_0 = "print('a simple')\n print('test script')"
53+ notebook_frontend .edit_cell (index = 0 , content = test_content_0 )
54+ notebook_frontend .wait_for_condition (
55+ lambda : notebook_frontend .get_cell_contents (0 ).strip () == test_content_0 ,
56+ timeout = 150 ,
57+ period = 1
58+ )
3559
3660 # Wait for Save As modal, save
3761 print ('[Test] Save' )
@@ -57,7 +81,7 @@ def attempt_form_fill_and_save():
5781 # ....................
5882 # This may be a retry, check if the application state reflects a successful save operation
5983 nonlocal fill_attempts
60- if fill_attempts and get_notebook_name (notebook_frontend ) == "new_notebook .ipynb" :
84+ if fill_attempts and get_notebook_name (notebook_frontend ) == "writable_notebook .ipynb" :
6185 print ('[Test] Success from previous save attempt!' )
6286 return True
6387 fill_attempts += 1
@@ -70,18 +94,18 @@ def attempt_form_fill_and_save():
7094
7195 # Set the notebook name field in the save dialog
7296 print ('[Test] Fill the input field' )
73- name_input_element .evaluate (f'(elem) => {{ elem.value = "new_notebook .ipynb"; return elem.value; }}' )
97+ name_input_element .evaluate (f'(elem) => {{ elem.value = "writable_notebook .ipynb"; return elem.value; }}' )
7498 notebook_frontend .wait_for_condition (
7599 lambda : name_input_element .evaluate (
76- f'(elem) => {{ elem.value = "new_notebook .ipynb"; return elem.value; }}' ) == 'new_notebook .ipynb' ,
100+ f'(elem) => {{ elem.value = "writable_notebook .ipynb"; return elem.value; }}' ) == 'writable_notebook .ipynb' ,
77101 timeout = 120 ,
78102 period = .25
79103 )
80104 # Show the input field value
81105 print ('[Test] Name input field contents:' )
82106 field_value = name_input_element .evaluate (f'(elem) => {{ return elem.value; }}' )
83107 print ('[Test] ' + field_value )
84- if field_value != 'new_notebook .ipynb' :
108+ if field_value != 'writable_notebook .ipynb' :
85109 return False
86110
87111 print ('[Test] Locate and click the save button' )
@@ -96,13 +120,13 @@ def attempt_form_fill_and_save():
96120 print ('[Test] Save element still visible after save, wait for hidden' )
97121 try :
98122 save_element .expect_not_to_be_visible (timeout = 120 )
99- except PlaywrightTimeoutError as err :
123+ except EndToEndTimeout as err :
100124 traceback .print_exc ()
101125 print ('[Test] Save button failed to hide...' )
102126
103127 # Check if the save operation succeeded (by checking notebook name change)
104128 notebook_frontend .wait_for_condition (
105- lambda : get_notebook_name (notebook_frontend ) == "new_notebook .ipynb" , timeout = 120 , period = 5
129+ lambda : get_notebook_name (notebook_frontend ) == "writable_notebook .ipynb" , timeout = 120 , period = 5
106130 )
107131 print (f'[Test] Notebook name: { get_notebook_name (notebook_frontend )} ' )
108132 print ('[Test] Notebook name was changed!' )
@@ -113,4 +137,41 @@ def attempt_form_fill_and_save():
113137
114138 # Test that address bar was updated
115139 print ('[Test] Test address bar' )
116- assert "new_notebook.ipynb" in notebook_frontend .get_page_url (page = EDITOR_PAGE )
140+ assert "writable_notebook.ipynb" in notebook_frontend .get_page_url (page = EDITOR_PAGE )
141+
142+ print ('[Test] Check that the notebook is no longer read only' )
143+ notebook_frontend .wait_for_condition (
144+ lambda : notebook_frontend .evaluate ('() => { return Jupyter.notebook.writable }' , page = EDITOR_PAGE ) is True ,
145+ timeout = 150 ,
146+ period = 1
147+ )
148+
149+ print ('[Test] Add some more content' )
150+ test_content_1 = "print('a second simple')\n print('script to test save feature')"
151+ notebook_frontend .add_and_execute_cell (content = test_content_1 )
152+ # and save the notebook
153+ notebook_frontend .evaluate ("Jupyter.notebook.save_notebook()" , page = EDITOR_PAGE )
154+
155+ print ('[Test] Test that it still contains the content' )
156+ notebook_frontend .wait_for_condition (
157+ lambda : notebook_frontend .get_cell_contents (index = 0 ) == test_content_0 ,
158+ timeout = 150 ,
159+ period = 1
160+ )
161+ notebook_frontend .wait_for_condition (
162+ lambda : notebook_frontend .get_cell_contents (index = 1 ) == test_content_1 ,
163+ timeout = 150 ,
164+ period = 1
165+ )
166+ print ('[Test] Test that it persists even after a refresh' )
167+ notebook_frontend .reload (EDITOR_PAGE )
168+ notebook_frontend .wait_for_condition (
169+ lambda : notebook_frontend .get_cell_contents (index = 0 ) == test_content_0 ,
170+ timeout = 150 ,
171+ period = 1
172+ )
173+ notebook_frontend .wait_for_condition (
174+ lambda : notebook_frontend .get_cell_contents (index = 1 ) == test_content_1 ,
175+ timeout = 150 ,
176+ period = 1
177+ )
0 commit comments