Skip to content

Commit

Permalink
Tests: Set global div class names within a test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agoscinski committed Jul 3, 2024
1 parent dd5767a commit 5d21830
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 45 deletions.
13 changes: 7 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urljoin

import pytest
from packaging.version import Version
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
Expand All @@ -17,7 +18,7 @@
JUPYTER_VERSION = None


def get_jupyter_version() -> str:
def get_jupyter_version() -> Version:
"""
Function so we can update the jupyter version during initialization
and use it in other files
Expand Down Expand Up @@ -45,7 +46,7 @@ def notebook_service():
["jupyter", f"{JUPYTER_TYPE}", "--version"]
)
# convert to string
JUPYTER_VERSION = jupyter_version.decode().replace("\n", "")
JUPYTER_VERSION = Version(jupyter_version.decode().replace("\n", ""))

jupyter_process = subprocess.Popen(
[
Expand Down Expand Up @@ -106,7 +107,7 @@ def _selenium_driver(nb_path):

# jupyter lab < 4
if JUPYTER_TYPE == "lab":
if get_jupyter_version() < "4.0.0":
if get_jupyter_version() < Version("4.0.0"):
restart_kernel_button_class_name = (
"bp3-button.bp3-minimal.jp-ToolbarButtonComponent.minimal.jp-Button"
)
Expand All @@ -116,7 +117,7 @@ def _selenium_driver(nb_path):
else:
raise ValueError("jupyter lab > 4.0.0 is not supported.")
elif JUPYTER_TYPE == "notebook":
if get_jupyter_version() < "7.0.0":
if get_jupyter_version() < Version("7.0.0"):
restart_kernel_button_class_name = "btn.btn-default"
restart_kernel_button_title_attribute = (
"restart the kernel, then re-run the whole notebook (with dialog)"
Expand Down Expand Up @@ -169,15 +170,15 @@ def _selenium_driver(nb_path):
# -------------------------------

if JUPYTER_TYPE == "lab":
if get_jupyter_version() < "4.0.0":
if get_jupyter_version() < Version("4.0.0"):
restart_button_class_name = (
"jp-Dialog-button.jp-mod-accept.jp-mod-warn.jp-mod-styled"
)
restart_button_text = "Restart"
else:
raise ValueError("jupyter lab > 4.0.0 is not supported.")
elif JUPYTER_TYPE == "notebook":
if get_jupyter_version() < "7.0.0":
if get_jupyter_version() < Version("7.0.0"):
restart_button_class_name = "btn.btn-default.btn-sm.btn-danger"
restart_button_text = "Restart and Run All Cells"
else:
Expand Down
96 changes: 57 additions & 39 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
import requests
from imageio.v3 import imread
from packaging.version import Version
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
Expand Down Expand Up @@ -41,45 +42,6 @@ def crop_const_color_borders(image: np.ndarray, const_color: int = 255):
return image[i1:i2, j1:j2, :]


if JUPYTER_TYPE == "notebook":
BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button"
OUTPUT_CLASS_NAME = "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
TEXT_INPUT_CLASS_NAME = "widget-input"
CODE_MIRROR_CLASS_NAME = "CodeMirror-code"
MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container"
CUE_BOX_CLASS_NAME = (
"lm-Widget.lm-Panel.jupyter-widgets.widget-container"
".widget-box.widget-vbox.scwidget-cue-box"
)
elif JUPYTER_TYPE == "lab":
BUTTON_CLASS_NAME = (
"lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button"
)
OUTPUT_CLASS_NAME = (
"lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
)
TEXT_INPUT_CLASS_NAME = "widget-input"
CODE_MIRROR_CLASS_NAME = "CodeMirror-code"

MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container"
CUE_BOX_CLASS_NAME = (
"lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets."
"widget-container.widget-box.widget-vbox.scwidget-cue-box"
)
else:
raise ValueError(
f"Tests do not support jupyter type {JUPYTER_TYPE!r}. Please use 'notebook' or"
" 'lab'."
)

CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue"

RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button"
CUED_RESET_CUE_BUTTON_CLASS_NAME = (
f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue"
)


def cue_box_class_name(cue_type: str, cued: bool):
class_name = CUED_CUE_BOX_CLASS_NAME if cued else CUE_BOX_CLASS_NAME
if cue_type is None:
Expand Down Expand Up @@ -156,6 +118,62 @@ def test_notebook_running(notebook_service):
assert response.status_code == 200


def test_setup_globals():
from .conftest import JUPYTER_VERSION

# black formats this into one line which causes an error in the linter.
# fmt: off
global BUTTON_CLASS_NAME, OUTPUT_CLASS_NAME, TEXT_INPUT_CLASS_NAME, \
CODE_MIRROR_CLASS_NAME, MATPLOTLIB_CANVAS_CLASS_NAME, CUE_BOX_CLASS_NAME
global CUED_CUE_BOX_CLASS_NAME, RESET_CUE_BUTTON_CLASS_NAME, \
CUED_RESET_CUE_BUTTON_CLASS_NAME
# fmt: on

if JUPYTER_TYPE == "notebook" and JUPYTER_VERSION >= Version("7.0.0"):
BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button"
OUTPUT_CLASS_NAME = (
"lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
)
TEXT_INPUT_CLASS_NAME = "widget-input"
CODE_MIRROR_CLASS_NAME = "cm-content"
MATPLOTLIB_CANVAS_CLASS_NAME = (
"jupyter-widgets.jupyter-matplotlib-canvas-container"
)
CUE_BOX_CLASS_NAME = (
"lm-Widget.lm-Panel.jupyter-widgets.widget-container"
".widget-box.widget-vbox.scwidget-cue-box"
)
elif JUPYTER_TYPE == "lab" and JUPYTER_VERSION < Version("4.0.0"):
BUTTON_CLASS_NAME = (
"lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button"
)
OUTPUT_CLASS_NAME = (
"lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output"
)
TEXT_INPUT_CLASS_NAME = "widget-input"
CODE_MIRROR_CLASS_NAME = "cm-content"

MATPLOTLIB_CANVAS_CLASS_NAME = (
"jupyter-widgets.jupyter-matplotlib-canvas-container"
)
CUE_BOX_CLASS_NAME = (
"lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets."
"widget-container.widget-box.widget-vbox.scwidget-cue-box"
)
else:
raise ValueError(
f"Tests do not support jupyter type {JUPYTER_TYPE!r} for version"
f"{JUPYTER_VERSION!r}."
)

CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue"

RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button"
CUED_RESET_CUE_BUTTON_CLASS_NAME = (
f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue"
)


def test_privacy_policy(selenium_driver):
"""
The first time jupyter lab is started on a fresh installation a privacy popup
Expand Down

0 comments on commit 5d21830

Please sign in to comment.