Skip to content

Commit 5d21830

Browse files
committed
Tests: Set global div class names within a test
We need to check the jupyter version to set the globals, the version is only available when running a notebooki. The easiest way is do it within a test. Opened issue #51 to implement this cleaner.
1 parent dd5767a commit 5d21830

File tree

2 files changed

+64
-45
lines changed

2 files changed

+64
-45
lines changed

tests/conftest.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urllib.parse import urljoin
55

66
import pytest
7+
from packaging.version import Version
78
from selenium.common.exceptions import StaleElementReferenceException
89
from selenium.webdriver.common.by import By
910
from selenium.webdriver.support import expected_conditions
@@ -17,7 +18,7 @@
1718
JUPYTER_VERSION = None
1819

1920

20-
def get_jupyter_version() -> str:
21+
def get_jupyter_version() -> Version:
2122
"""
2223
Function so we can update the jupyter version during initialization
2324
and use it in other files
@@ -45,7 +46,7 @@ def notebook_service():
4546
["jupyter", f"{JUPYTER_TYPE}", "--version"]
4647
)
4748
# convert to string
48-
JUPYTER_VERSION = jupyter_version.decode().replace("\n", "")
49+
JUPYTER_VERSION = Version(jupyter_version.decode().replace("\n", ""))
4950

5051
jupyter_process = subprocess.Popen(
5152
[
@@ -106,7 +107,7 @@ def _selenium_driver(nb_path):
106107

107108
# jupyter lab < 4
108109
if JUPYTER_TYPE == "lab":
109-
if get_jupyter_version() < "4.0.0":
110+
if get_jupyter_version() < Version("4.0.0"):
110111
restart_kernel_button_class_name = (
111112
"bp3-button.bp3-minimal.jp-ToolbarButtonComponent.minimal.jp-Button"
112113
)
@@ -116,7 +117,7 @@ def _selenium_driver(nb_path):
116117
else:
117118
raise ValueError("jupyter lab > 4.0.0 is not supported.")
118119
elif JUPYTER_TYPE == "notebook":
119-
if get_jupyter_version() < "7.0.0":
120+
if get_jupyter_version() < Version("7.0.0"):
120121
restart_kernel_button_class_name = "btn.btn-default"
121122
restart_kernel_button_title_attribute = (
122123
"restart the kernel, then re-run the whole notebook (with dialog)"
@@ -169,15 +170,15 @@ def _selenium_driver(nb_path):
169170
# -------------------------------
170171

171172
if JUPYTER_TYPE == "lab":
172-
if get_jupyter_version() < "4.0.0":
173+
if get_jupyter_version() < Version("4.0.0"):
173174
restart_button_class_name = (
174175
"jp-Dialog-button.jp-mod-accept.jp-mod-warn.jp-mod-styled"
175176
)
176177
restart_button_text = "Restart"
177178
else:
178179
raise ValueError("jupyter lab > 4.0.0 is not supported.")
179180
elif JUPYTER_TYPE == "notebook":
180-
if get_jupyter_version() < "7.0.0":
181+
if get_jupyter_version() < Version("7.0.0"):
181182
restart_button_class_name = "btn.btn-default.btn-sm.btn-danger"
182183
restart_button_text = "Restart and Run All Cells"
183184
else:

tests/test_widgets.py

+57-39
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import requests
1010
from imageio.v3 import imread
11+
from packaging.version import Version
1112
from selenium.webdriver.common.by import By
1213
from selenium.webdriver.common.keys import Keys
1314
from selenium.webdriver.remote.webelement import WebElement
@@ -41,45 +42,6 @@ def crop_const_color_borders(image: np.ndarray, const_color: int = 255):
4142
return image[i1:i2, j1:j2, :]
4243

4344

44-
if JUPYTER_TYPE == "notebook":
45-
BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button"
46-
OUTPUT_CLASS_NAME = "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
47-
TEXT_INPUT_CLASS_NAME = "widget-input"
48-
CODE_MIRROR_CLASS_NAME = "CodeMirror-code"
49-
MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container"
50-
CUE_BOX_CLASS_NAME = (
51-
"lm-Widget.lm-Panel.jupyter-widgets.widget-container"
52-
".widget-box.widget-vbox.scwidget-cue-box"
53-
)
54-
elif JUPYTER_TYPE == "lab":
55-
BUTTON_CLASS_NAME = (
56-
"lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button"
57-
)
58-
OUTPUT_CLASS_NAME = (
59-
"lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
60-
)
61-
TEXT_INPUT_CLASS_NAME = "widget-input"
62-
CODE_MIRROR_CLASS_NAME = "CodeMirror-code"
63-
64-
MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container"
65-
CUE_BOX_CLASS_NAME = (
66-
"lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets."
67-
"widget-container.widget-box.widget-vbox.scwidget-cue-box"
68-
)
69-
else:
70-
raise ValueError(
71-
f"Tests do not support jupyter type {JUPYTER_TYPE!r}. Please use 'notebook' or"
72-
" 'lab'."
73-
)
74-
75-
CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue"
76-
77-
RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button"
78-
CUED_RESET_CUE_BUTTON_CLASS_NAME = (
79-
f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue"
80-
)
81-
82-
8345
def cue_box_class_name(cue_type: str, cued: bool):
8446
class_name = CUED_CUE_BOX_CLASS_NAME if cued else CUE_BOX_CLASS_NAME
8547
if cue_type is None:
@@ -156,6 +118,62 @@ def test_notebook_running(notebook_service):
156118
assert response.status_code == 200
157119

158120

121+
def test_setup_globals():
122+
from .conftest import JUPYTER_VERSION
123+
124+
# black formats this into one line which causes an error in the linter.
125+
# fmt: off
126+
global BUTTON_CLASS_NAME, OUTPUT_CLASS_NAME, TEXT_INPUT_CLASS_NAME, \
127+
CODE_MIRROR_CLASS_NAME, MATPLOTLIB_CANVAS_CLASS_NAME, CUE_BOX_CLASS_NAME
128+
global CUED_CUE_BOX_CLASS_NAME, RESET_CUE_BUTTON_CLASS_NAME, \
129+
CUED_RESET_CUE_BUTTON_CLASS_NAME
130+
# fmt: on
131+
132+
if JUPYTER_TYPE == "notebook" and JUPYTER_VERSION >= Version("7.0.0"):
133+
BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button"
134+
OUTPUT_CLASS_NAME = (
135+
"lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
136+
)
137+
TEXT_INPUT_CLASS_NAME = "widget-input"
138+
CODE_MIRROR_CLASS_NAME = "cm-content"
139+
MATPLOTLIB_CANVAS_CLASS_NAME = (
140+
"jupyter-widgets.jupyter-matplotlib-canvas-container"
141+
)
142+
CUE_BOX_CLASS_NAME = (
143+
"lm-Widget.lm-Panel.jupyter-widgets.widget-container"
144+
".widget-box.widget-vbox.scwidget-cue-box"
145+
)
146+
elif JUPYTER_TYPE == "lab" and JUPYTER_VERSION < Version("4.0.0"):
147+
BUTTON_CLASS_NAME = (
148+
"lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button"
149+
)
150+
OUTPUT_CLASS_NAME = (
151+
"lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
152+
)
153+
TEXT_INPUT_CLASS_NAME = "widget-input"
154+
CODE_MIRROR_CLASS_NAME = "cm-content"
155+
156+
MATPLOTLIB_CANVAS_CLASS_NAME = (
157+
"jupyter-widgets.jupyter-matplotlib-canvas-container"
158+
)
159+
CUE_BOX_CLASS_NAME = (
160+
"lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets."
161+
"widget-container.widget-box.widget-vbox.scwidget-cue-box"
162+
)
163+
else:
164+
raise ValueError(
165+
f"Tests do not support jupyter type {JUPYTER_TYPE!r} for version"
166+
f"{JUPYTER_VERSION!r}."
167+
)
168+
169+
CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue"
170+
171+
RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button"
172+
CUED_RESET_CUE_BUTTON_CLASS_NAME = (
173+
f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue"
174+
)
175+
176+
159177
def test_privacy_policy(selenium_driver):
160178
"""
161179
The first time jupyter lab is started on a fresh installation a privacy popup

0 commit comments

Comments
 (0)