-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_offlinenotebook.py
More file actions
365 lines (308 loc) · 12.7 KB
/
test_offlinenotebook.py
File metadata and controls
365 lines (308 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import json
import os
import pytest
from shutil import copyfile
import subprocess
from time import sleep
from urllib.request import urlopen
from urllib.error import URLError
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.options import Options
PORT = 18888
CONFIG_URL = f"http://localhost:{PORT}/offlinenotebook/config"
JUPYTERLAB_URL = f"http://localhost:{PORT}/lab/tree/example.ipynb"
JUPYTERNOTEBOOK_URL = f"http://localhost:{PORT}/tree/example.ipynb"
EXPECTED_SIZE = 1700
EXPECTED_EMPTY_SIZE = 450
EXPECTED_NUM_CELLS = 5
HEADLESS = True
# If 'firefox' is a script selenium may not work, if this happens set this to
# the binary e.g. '/usr/lib64/firefox/firefox'
FIREFOX_BIN = None
TIMEOUT = 10
# Size of downloaded notebook varies depending on jupyter versions as
# it's exported from the browser JSON model, not the original ipynb
# Allow ± 100
def assert_expected_size(size):
assert abs(EXPECTED_SIZE - size) < 100
def assert_empty_size(size):
assert abs(EXPECTED_EMPTY_SIZE - size) < 100
class FirefoxTestBase:
def setup_method(self):
self.jupyter_proc = None
self.major_version = None
self.driver = None
self.wait = None
def teardown_method(self):
try:
if self.driver:
self.driver.quit()
finally:
if self.jupyter_proc:
self.jupyter_proc.kill()
def start_jupyter(self, jupyterdir, app):
# Use unique JupyterLab dirs to avoid unexpected interactions
# https://jupyterlab.readthedocs.io/en/stable/user/directories.html
settings_dir = jupyterdir / "settings"
settings_dir.mkdir()
workspaces_dir = jupyterdir / "workspaces"
workspaces_dir.mkdir()
version = subprocess.check_output([f"jupyter-{app}", "--version"])
self.major_version = int(version.split(b".", 1)[0])
command = [
f"jupyter-{app}",
"--no-browser",
"--ServerApp.token=",
f"--port={PORT}",
]
env = {
"BINDER_LAUNCH_HOST": "http://localhost/",
"BINDER_REPO_URL": "https://github.com/manics/jupyter-offlinenotebook",
"BINDER_PERSISTENT_REQUEST": "v2/gh/repo",
"BINDER_REF_URL": (
"https://github.com/manics/jupyter-offlinenotebook/tree/main"
),
"PATH": os.getenv("PATH"),
"JUPYTERLAB_SETTINGS_DIR": str(settings_dir),
"JUPYTERLAB_WORKSPACES_DIR": str(workspaces_dir),
}
self.jupyter_proc = subprocess.Popen(command, cwd=str(jupyterdir), env=env)
# Wait for Jupyter to start
for n in range(10):
sleep(1)
try:
with urlopen(f"http://localhost:{PORT}", timeout=2) as _:
pass
except URLError:
continue
print(f"jupyter-{app} started")
def initialise_firefox(self, downloaddir, url):
profile = FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", "false")
profile.set_preference("browser.download.alwaysOpenPanel", False)
profile.set_preference("browser.download.dir", downloaddir)
profile.set_preference(
"browser.helperApps.neverAsk.saveToDisk", "application/x-ipynb+json"
)
options = Options()
# Comment this out to see the browser window
options.headless = HEADLESS
options.profile = profile
if FIREFOX_BIN:
options.binary_location = FIREFOX_BIN
self.driver = webdriver.Firefox(options=options)
self.wait = WebDriverWait(self.driver, TIMEOUT)
self.driver.get(url)
print("Firefox Initialized")
def initialise(self, tmpdir, app, url):
jupyterdir = (tmpdir / "jupyter").mkdir()
downloaddir = (tmpdir / "download").mkdir()
copyfile("example.ipynb", str(jupyterdir / "example.ipynb"))
copyfile(
"offline-notebook-buttons.png",
str(jupyterdir / "offline-notebook-buttons.png"),
)
self.expected_download = str(downloaddir / "example.ipynb")
self.start_jupyter(jupyterdir, app)
self.initialise_firefox(str(downloaddir), url)
class TestOfflineNotebook(FirefoxTestBase):
def download_visible(self):
self.wait.until(
EC.element_to_be_clickable(
(By.XPATH, "//button[@title='Download visible']")
)
).click()
size = os.stat(self.expected_download).st_size
with open(self.expected_download) as f:
nb = json.load(f)
ncells = len(nb["cells"])
os.remove(self.expected_download)
return size, ncells
def save_to_browser_storage(self):
self.driver.find_element(
By.XPATH, "//button[@title='Save to browser storage']"
).click()
dialog = self.wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-dialog"))
)
assert dialog.find_element(By.CSS_SELECTOR, "h4.modal-title").text == (
"Notebook saved to browser storage"
)
assert dialog.find_element(By.CSS_SELECTOR, "div.modal-body").text == (
"repoid: https://github.com/manics/jupyter-offlinenotebook\n"
"path: example.ipynb"
)
dialog.find_element(By.CSS_SELECTOR, "button.btn-default").click()
def restore_from_browser_storage(self):
self.driver.find_element(
By.XPATH, "//button[@title='Restore from browser storage']"
).click()
dialog = self.wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-dialog"))
)
assert dialog.find_element(By.CSS_SELECTOR, "h4.modal-title").text == (
"This will replace your current notebook with"
)
assert dialog.find_element(By.CSS_SELECTOR, "div.modal-body").text == (
"repoid: https://github.com/manics/jupyter-offlinenotebook\n"
"path: example.ipynb"
)
buttons = dialog.find_elements(By.CSS_SELECTOR, "button.btn-default")
assert buttons[0].text == "OK"
assert buttons[1].text == "Cancel"
buttons[0].click()
def wait_for_modal_dialog(self):
# element_to_be_clickable doesn't actually mean clickable
# so need to make sure the modal dialog has cleared
# https://stackoverflow.com/a/51842120
self.wait.until(
EC.invisibility_of_element_located(
(By.XPATH, "//div[@class='modal-backdrop']")
)
)
# Still doesn't work so force a pause
sleep(0.5)
@pytest.mark.flaky(max_runs=3)
def test_offline_notebook(self, tmpdir):
# Selenium can't access IndexedDB so instead check save/load by
# downloading the updated notebook
self.initialise(tmpdir, "nbclassic", JUPYTERNOTEBOOK_URL)
size, ncells = self.download_visible()
assert_expected_size(size)
assert ncells == EXPECTED_NUM_CELLS
self.save_to_browser_storage()
print("Saved to browser storage")
# Delete some cells and download
# element_to_be_clickable doesn't actually mean clickable
self.wait_for_modal_dialog()
for n in range(EXPECTED_NUM_CELLS):
self.wait.until(
EC.element_to_be_clickable(
(By.XPATH, "//button[@title='cut selected cells']")
)
).click()
size, ncells = self.download_visible()
assert_empty_size(size)
assert ncells == 1
self.restore_from_browser_storage()
# download_visible uses element_to_be_clickable but that doesn't
# actually mean clickable
self.wait_for_modal_dialog()
size, ncells = self.download_visible()
assert_expected_size(size)
assert ncells == EXPECTED_NUM_CELLS
class TestOfflineLab(FirefoxTestBase):
def download_visible(self):
self.wait.until(
EC.invisibility_of_element((By.XPATH, "//div[@class='modal-backdrop']"))
)
self.wait.until(
EC.element_to_be_clickable(
(By.XPATH, f"//{self.toolbar_button}[@title='Download visible']")
)
).click()
# Allow time for the downloaded file to be saved
sleep(2)
size = os.stat(self.expected_download).st_size
assert size
with open(self.expected_download) as f:
nb = json.load(f)
ncells = len(nb["cells"])
os.remove(self.expected_download)
return size, ncells
def save_to_browser_storage(self):
self.driver.find_element(
By.XPATH, f"//{self.toolbar_button}[@title='Save to browser storage']"
).click()
dialog = self.wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.jp-Dialog-content"))
)
assert dialog.find_element(By.CSS_SELECTOR, "div.jp-Dialog-header").text == (
"Notebook saved to browser storage"
)
assert dialog.find_element(By.CSS_SELECTOR, "span.jp-Dialog-body").text == (
"repoid: https://github.com/manics/jupyter-offlinenotebook "
"path: example.ipynb"
)
dialog.find_element(By.CSS_SELECTOR, "button.jp-Dialog-button").click()
def restore_from_browser_storage(self):
self.driver.find_element(
By.XPATH, f"//{self.toolbar_button}[@title='Restore from browser storage']"
).click()
dialog = self.wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.jp-Dialog-content"))
)
assert dialog.find_element(By.CSS_SELECTOR, "div.jp-Dialog-header").text == (
"This will replace your current notebook with"
)
assert dialog.find_element(By.CSS_SELECTOR, "span.jp-Dialog-body").text == (
"repoid: https://github.com/manics/jupyter-offlinenotebook "
"path: example.ipynb"
)
buttons = dialog.find_elements(By.CSS_SELECTOR, "button.jp-Dialog-button")
assert buttons[0].text == "Cancel"
assert buttons[1].text == "OK"
buttons[1].click()
@pytest.mark.flaky(max_runs=3)
def test_offline_lab(self, tmpdir):
# Selenium can't access IndexedDB so instead check save/load by
# downloading the updated notebook
self.initialise(tmpdir, "lab", JUPYTERLAB_URL)
assert self.major_version in (3, 4)
if self.major_version == 3:
self.toolbar_button = "button"
else:
self.toolbar_button = "jp-button"
# Wait for the loading logo to appear, then disappear
try:
self.wait.until(
EC.visibility_of_element_located((By.XPATH, "//div[@id='main-logo']"))
)
except TimeoutException:
# Maybe JupyterLab loaded too quickly for selenium to see the logo?
pass
self.wait.until(
EC.invisibility_of_element((By.XPATH, "//div[@id='main-logo']"))
)
size, ncells = self.download_visible()
assert_expected_size(size)
assert ncells == EXPECTED_NUM_CELLS
self.save_to_browser_storage()
# Delete some cells and download
for n in range(EXPECTED_NUM_CELLS):
self.wait.until(
EC.element_to_be_clickable(
# Cut the selected cells
# Cut the selected cells (X)
# Cut this cell
(
By.XPATH,
f"//{self.toolbar_button}[starts-with(@title, 'Cut ')]",
)
)
).click()
size, ncells = self.download_visible()
assert_empty_size(size)
assert ncells == 1
self.restore_from_browser_storage()
size, ncells = self.download_visible()
assert_expected_size(size)
assert ncells == EXPECTED_NUM_CELLS
class TestServer(FirefoxTestBase):
def test_server_config(self, tmpdir):
self.start_jupyter(tmpdir, "server")
with urlopen(CONFIG_URL) as r:
assert json.load(r) == {
"repoid": "https://github.com/manics/jupyter-offlinenotebook",
"binder_repo_label": "GitHub",
"binder_ref_url": (
"https://github.com/manics/jupyter-offlinenotebook/tree/main"
),
"binder_persistent_url": "http://localhost/v2/gh/repo",
}